• 安装

    • 下载wget https://nginx.org/download/nginx-1.22.1.tar.gz

    • 解压tar zxvf nginx-1.22.1.tar.gz

    • 进入目录nginx-1.22.1

    • 安装gcc依赖yum install gcc -y

    • 编译参数./configure --prefix=/usr/local/nginx

      --prefix设置安装目录

    • 安装make && make install

    • 安装完成检查是否存在/usr/local/nginx目录

    • 进入到nginx主目录后,进入sbin目录,启动nginx

      image-20230223154042347

    • 查看是否运行成功

      image-20230223154112441

    • 安装时出现的问题

      • ./configure: error: the HTTP rewrite module requires the PCRE library.
        You can either disable the module by using --without-http_rewrite_module
        option, or install the PCRE library into the system, or build the PCRE library
        statically from the source with nginx by using --with-pcre=<path> option.

        解决办法:安装pcre

        sudo yum install pcre-devel #RHEL/CentOS
        sudo apt-get install libpcre3-dev #Ubuntu/Debian

        参考文章:Nginx configure error: the HTTP rewrite module requires the PCRE library

      • ./configure: error: the HTTP gzip module requires the zlib library.
        You can either disable the module by using --without-http_gzip_module
        option, or install the zlib library into the system, or build the zlib library
        statically from the source with nginx by using --with-zlib=<path> option.

        解决办法

        sudo yum install -y zlib zlib-devel
      • 启动了但是无法连接

        解决办法:关闭防火墙或者放行端口

        systemctl stop firewalld
        firewall-cmd --zone=public --add-port=80/tcp --permanent
  • 启动Nginx

    • nginx 启动
    • nginx -s stop 快速停止
    • nginx -s quit 停止,正在进行的连接不受影响
    • nginx -s reload 重新加载配置
  • 系统文件配置

    vim /usr/lib/systemd/system/nginx.service
    [Unit]
    Description=nginx - web server
    After=network.target remote-fs.target nss-lookup.target
    
    [Service]
    Type=forking
    PIDFile=/usr/local/nginx/logs/nginx.pid
    ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
    ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
    ExecReload=/usr/local/nginx/sbin/nginx -s reload
    ExecStop=/usr/local/nginx/sbin/nginx -s stop
    ExecQuit=/usr/local/nginx/sbin/nginx -s quit
    PrivateTmp=true
    
    [Install]
    WantedBy=multi-user.target
    systemctl daemon-reload #重新加载配置