安装

2023/8/27

# 安装

  1. 更新软件包缓存

    在开始安装之前,更新软件包缓存以确保你使用的是最新的软件信息:

    sudo yum update
    
  2. 安装 EPEL 软件包源

    EPEL 软件包源包含了很多额外的软件包,包括 Nginx。使用以下命令安装 EPEL 软件包源:

    sudo yum install epel-release
    
  3. 安装 Nginx

    3.1 可选设置ningx安装源:

    sudo vim /etc/yum.repos.d/nginx.repo
     [nginx]
    name=nginx repo
    baseurl=http://nginx.org/packages/centos/7/$basearch/
    gpgcheck=0
    enabled=1
    

    3.2 使用以下命令安装 Nginx:

    sudo yum install nginx
    

安装过程中可能会询问是否继续,输入 Y 并按回车键确认。

  1. 启动 Nginx 服务

    安装完成后,使用以下命令启动 Nginx 服务:

    sudo systemctl start nginx
    
  2. 设置开机自启动

    让 Nginx 在系统启动时自动运行:

    sudo systemctl enable nginx
    
  3. 配置防火墙规则

    如果你的系统使用了防火墙(firewall),需要允许 HTTP(80 端口)和 HTTPS(443 端口)的访问。你可以运行以下命令来设置防火墙规则:

    sudo firewall-cmd --permanent --add-service=http
    sudo firewall-cmd --permanent --add-service=https
    sudo firewall-cmd --reload
    

    6.1 开放某个端口

    sudo firewall-cmd --add-port=80/tcp --permanent
    

    6.2 重启防火墙

    firewall-cmd --reload
    
  4. 验证安装

    打开网页浏览器,输入服务器的 IP 地址或域名,你应该能够看到 Nginx 欢迎页面。

# 常用命令

  1. 启动 Nginx

    sudo systemctl start nginx
    

或者,如果你是通过编译安装的方式,可以进入 Nginx 可执行文件目录,然后运行:

   sudo ./sbin/nginx
  1. 停止 Nginx

    sudo systemctl stop nginx
    

或者,如果你是通过编译安装的方式,可以运行:

   sudo ./sbin/nginx -s stop
  1. 重新加载 Nginx 配置

    在不停止 Nginx 服务的情况下重新加载配置文件,使新的配置生效:

    sudo systemctl reload nginx
    

或者,如果你是通过编译安装的方式,可以运行:

   sudo ./sbin/nginx -s reload
  1. 查看 Nginx 状态

    查看 Nginx 服务的状态以及是否正在运行:

    sudo systemctl status nginx
    
  2. 设置 Nginx 开机自启动

    让 Nginx 在系统启动时自动运行:

    sudo systemctl enable nginx
    
  3. 停用 Nginx 开机自启动

    移除 Nginx 的开机自启动设置:

    sudo systemctl disable nginx
    
  4. 测试 Nginx 配置是否正确

    在修改了 Nginx 配置文件后,可以使用以下命令检查配置是否有语法错误:

    sudo nginx -t
    
  5. 查看 Nginx 错误日志

    查看 Nginx 的错误日志,通常位于 /var/log/nginx/error.log

    sudo tail -f /var/log/nginx/error.log
    

# nginx卸载

  1. 停止 Nginx 服务:首先,确保停止正在运行的 Nginx 服务。可以使用以下命令停止 Nginx:

    sudo systemctl stop nginx
    
  2. 删除 Nginx 文件:你可以删除 Nginx 安装目录中的文件。通常情况下,Nginx 可执行文件位于 /usr/local/nginx 或你配置的其他目录中。使用以下命令删除 Nginx 目录:

    sudo rm -r /usr/local/nginx
    
  3. 删除 systemd 服务单元文件:如果你手动创建了 systemd 服务单元文件,也需要删除它。在 /etc/systemd/system 目录下查找名为 nginx.service 的文件,并删除它:

    sudo rm /etc/systemd/system/nginx.service
    
  4. 清理配置文件和日志:Nginx 配置文件和日志通常位于 /usr/local/nginx/conf/usr/local/nginx/logs 或你配置的其他目录中。删除这些目录中的配置文件和日志。

  5. 删除系统用户和组:在编译安装时,可能会创建一个用于运行 Nginx 的系统用户和组。你可以使用以下命令来删除它们:

    sudo userdel nginx     # 用于 Ubuntu/Debian
    sudo userdel www-data  # 用于 CentOS/Red Hat
    
  6. 检查 Nginx 目录是否存在:检查之前安装的 Nginx 目录是否已被删除。通常情况下,Nginx 可执行文件位于 /usr/local/nginx 或你之前设置的其他目录中。运行以下命令来检查目录是否存在:

    ls /usr/local/nginx
    

    如果该目录不存在,表示 Nginx 已经被卸载。

  7. 检查 systemd 服务单元文件:检查 /etc/systemd/system 目录是否仍然有名为 nginx.service 的文件存在。如果之前手动创建了 systemd 服务单元文件,并且该文件仍然存在,可以使用以下命令来删除它:

    sudo rm /etc/systemd/system/nginx.service
    
  8. 检查用户和组:如果之前创建了一个用于运行 Nginx 的系统用户和组,确保已经删除它们:

    sudo userdel nginx     # 对于 Ubuntu/Debian
    sudo userdel www-data  # 对于 CentOS/Red Hat
    
  9. 清理配置文件和日志:如果 Nginx 配置文件和日志目录没有被删除,你可以手动删除它们。通常情况下,配置文件位于 /usr/local/nginx/conf,日志位于 /usr/local/nginx/logs 或你之前设置的其他目录中。

  10. 查看进程状态:运行以下命令来查看是否有正在运行的 Nginx 进程:

    ps aux | grep nginx
    

    如果没有找到任何关于 Nginx 的进程,表示 Nginx 已经被卸载。

​ 有的化就kill

sudo kill -9 <PID>

# nginx代理

代理http协议的服务

server {
    listen 80;
    server_name your_domain_or_server_ip;

    location / {
        proxy_pass http://localhost:8038;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

代理ws协议的服务

server {
    listen 80;
    server_name your_domain_or_server_ip;

    location / {
        proxy_pass http://localhost:8038;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

# 配置图片服务器

在 /usr/share/nginx/html/目录创建文件夹

location /docloudresource {
             alias /usr/share/nginx/html/docloudresource;
			 index  index.html index.htm;
}

比如:docloudresource/image下有一个cc.png图片,通过IP访问:http://192.168.104.128/docloudresource/image/cc.png

完整的修改过后的nginx.conf


user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
    accept_mutex on;
    multi_accept on;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    server_tokens off;
    client_max_body_size 50m;
    send_timeout 10;
    client_body_timeout 10;
    client_header_timeout 10;
    client_header_buffer_size 1k;
    client_body_buffer_size 8k;
    large_client_header_buffers 4 8K;
    fastcgi_hide_header X-Powered-By;
    port_in_redirect off;

    keepalive_timeout  65;

    #gzip  on;
    gzip on;
    gzip_vary on;
    gzip_buffers 16 8k;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript image/jpeg image/gif image/png application/x-protobuf application/octet-stream;

    include /etc/nginx/conf.d/*.conf;

server {  
    listen 80;  
    listen  [::]:80;   
    server_name 192.168.104.128; 
    root /usr/share/nginx/html;  
    index index.html;  
    location / {
        #add_header Access-Control-Max-Age 1800;
        proxy_set_header Host $host;
        proxy_set_header X-Real-Ip $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
        #proxy_set_header Upgrade $http_upgrade;
        #proxy_set_header Connection "upgrade";
        proxy_pass http://192.168.104.128:9001/;
    }

    location /docloudresource {
       alias /usr/share/nginx/html/docloudresource;
       index  index.html index.htm;
    }

}


}

# 路径位置

  • 配置文件目录:/etc/nginx/
  • 默认虚拟主机配置目录:/etc/nginx/conf.d/
  • 主要可执行文件路径:/usr/sbin/nginx
  • Nginx 根目录:/usr/share/nginx/
  • HTML 默认根目录:/usr/share/nginx/html/
  • 日志文件目录:/var/log/nginx/
  • 临时文件目录:/var/lib/nginx/tmp/