Linux安装nginx

Linux安装nginx

  1. 作者QQ:67065435 QQ群:821635552

  2. 安装前的准备

    yum install \
    vim \
    gcc \
    gcc-c++ \
    wget \
    make \
    libtool \
    automake \
    autoconf \
    -y
    
  3. 安装PCRE库

    cd /root
    wget https://ftp.pcre.org/pub/pcre/pcre-8.44.tar.gz
    tar -zxvf pcre-8.44.tar.gz
    cd pcre-8.44
    ./configure
    make
    make install
    
  4. 安装zlib库

    cd /root
    wget http://zlib.net/zlib-1.2.11.tar.gz
    tar -zxvf zlib-1.2.11.tar.gz
    cd zlib-1.2.11
    ./configure
    make
    make install
    
  5. 安装openssl

    # openssl从1.0.2开始支持http2
    cd /root
    wget https://www.openssl.org/source/openssl-1.0.2u.tar.gz
    tar -zxvf openssl-1.0.2u.tar.gz
    
  6. 安装nginx

    # nginx在1.9.5开始支持http2
    cd /root
    wget http://nginx.org/download/nginx-1.17.9.tar.gz
    tar -zxvf nginx-1.17.9.tar.gz
    cd nginx-1.17.9
    # 安装前小提示:如果要隐藏Web服务名称'nginx',可以使用以下方法,但该方法会导致不兼容certbot免费证书生成工具
    |——————————————————————————————————————
    | vim ./src/core/nginx.h
    | 
    | #define NGINX_VERSION "x.x.x"
    | #define NGINX_VER     "nginx/" NGINX_VERSION
    | # 改为
    | #define NGINX_VERSION "1.0.0"
    | #define NGINX_VER     "FastWeb/" NGINX_VERSION
    | 
    | ESC
    | :wq
    |——————————————————————————————————————
    |——————————————————————————————————————
    | vim ./src/http/ngx_http_header_filter_module.c
    | 
    | ngx_http_server_string[] = "Server: nginx" CRLF;
    | # 改为
    | ngx_http_server_string[] = "Server: FastWeb" CRLF;
    | 
    | ESC
    | :wq
    |——————————————————————————————————————
    |——————————————————————————————————————
    | vim ./src/http/ngx_http_special_response.c
    | 
    nginx
    | # 改为 |

    FastWeb

    | | # 并且 | | static u_char ngx_http_msie_padding[] = | "" CRLF | "" CRLF | "" CRLF | "" CRLF | "" CRLF | "" CRLF; | # 改为 | static u_char ngx_http_msie_padding[] = "" CRLF; | | # 并且 | | static u_char ngx_http_error_tail[] = | "

    nginx

    " CRLF | "" CRLF | "" CRLF; | # 改为 | static u_char ngx_http_error_tail[] = | "

    FastWeb

    " CRLF | "" CRLF | "" CRLF; | | ESC | :wq |—————————————————————————————————————— ./configure \ --prefix=/usr/local/nginx/ \ --with-http_v2_module \ --with-http_ssl_module \ --with-http_realip_module \ --with-http_stub_status_module \ --with-pcre=../pcre-8.44/ \ --with-zlib=../zlib-1.2.11/ \ --with-openssl=../openssl-1.0.2u make make install
  7. 清除iptables规则

    iptables -P INPUT ACCEPT
    iptables -F
    service iptables save
    
  8. 开启firewall规则

    yum install firewalld
    systemctl enable firewalld
    systemctl start firewalld
    firewall-cmd --zone=public --add-port=80/tcp --permanent
    firewall-cmd --zone=public --add-port=443/tcp --permanent
    firewall-cmd --reload
    
  9. 修改nginx配置

    vim /usr/local/nginx/conf/nginx.conf
    
    user                       www;
    worker_processes           auto;
    worker_cpu_affinity        auto;
    pid                        logs/nginx.pid;
    
    events {
        worker_connections     102400;
    }
    
    http {
        server_tokens          off;
        charset                utf-8;
        proxy_hide_header      Server;
        proxy_hide_header      X-Powered-By;
    
        include                mime.types;
        default_type           application/octet-stream;
    
        client_max_body_size   20M;
    
        sendfile               on;
        keepalive_timeout      20;
        client_header_timeout  20;
        client_body_timeout    20;
        send_timeout           20;
    
        gzip                   on;
        gzip_vary              on;
        gzip_comp_level        1;
        gzip_types             text/css application/javascript application/json image/png image/webp image/apng image/jpeg image/x-icon;
    
        log_format  main       '$remote_addr - $remote_user [$time_local] "$request" '
                               '$status $body_bytes_sent "$http_referer" '
                               '"$http_user_agent" "$http_x_forwarded_for"';
        error_log              /www/z_error.log;
        access_log             /www/z_$host.log main;
    
        server {
            listen      80;
            server_name localhost;
            root        /www;
        
            location / {
                index   index.php index.html index.htm;
            }
        
            location ~* \.php {
                include                 fastcgi_params;
                fastcgi_index           index.php;
                fastcgi_pass            127.0.0.1:9000;
                fastcgi_split_path_info ^(.+\.php)(.*)$;
                fastcgi_param           PATH_INFO       $fastcgi_path_info;
                fastcgi_param           SCRIPT_NAME     $fastcgi_script_name;
                fastcgi_param           SCRIPT_FILENAME $document_root$fastcgi_script_name;
            }
        }
    }
    ESC
    :wq
    
  10. 将nginx加入环境变量

    vim /etc/profile
    
    export PATH=$PATH:/usr/local/nginx/bin
    export PATH=$PATH:/usr/local/nginx/sbin
    
    ESC
    :wq
    
    source /etc/profile
    
  11. 如果没有www用户,创建www用户

    useradd www
    
  12. 启动nginx并设置开机启动

    mkdir /www
    chown -R www:www /www
    chown -R www:www /usr/local/nginx
    
    cd /etc/systemd/system
    
    vim nginx.service
    
    [Unit]
    Description=Start nginx on boot.
    After=default.target network.target
    
    [Service]
    User=root
    Group=root
    Type=forking
    ExecStart=/usr/local/nginx/sbin/nginx
    ExecReload=/usr/local/nginx/sbin/nginx -s reload
    ExecStop=/usr/local/nginx/sbin/nginx -s quit
    PrivateTmp=true
    
    [Install]
    WantedBy=multi-user.target
    
    ESC
    :wq
    
    systemctl daemon-reload
    systemctl enable nginx
    systemctl start nginx
    
  13. nginx操作

    /usr/local/nginx/sbin/nginx    启动
    /usr/local/nginx/sbin/nginx -t 检查当前配置错误
    /usr/local/nginx/sbin/nginx -s reload 重载配置
    /usr/local/nginx/sbin/nginx -s reopen 重开日志
    /usr/local/nginx/sbin/nginx -s quit   正常关闭
    /usr/local/nginx/sbin/nginx -s stop   强制关闭
    
  14. 使用fastdfs-nginx-module比直接使用nginx的优势在哪

    FastDFS通过Tracker服务器,将文件放在Storage服务器存储,但
    是同组之间的服务器需要复制文件,有延迟的问题.假设Tracker
    服务器将文件上传到了192.168.1.2,文件ID已经返回客户端,这
    时,后台会将这个文件复制到192.168.1.3,如果复制没有完成,
    客户端就用这个ID在192.168.1.3取文件,肯定会出现错误,这
    个fastdfs-nginx-module可以重定向连接到源服务器取文件,避
    免客户端由于复制延迟的问题,出现错误。
    

你可能感兴趣的:(linux,nginx)