Nginx Install & Configuration

1. Environment

2. Install

  • gcc 安装

    yum install -y gcc-c++
    
  • PCRE pcre-devel 安装

    yum install -y pcre pcre-devel
    
  • zlib 安装

    yum install -y zlib zlib-devel
    
  • OpenSSL 安装

    yum install -y openssl openssl-devel
    
  • 将安装包通过SFTP上传至Centos7 服务器中指定目录,这里放在/opt目录下

    cd /opt
    ls -l
    
  • 创建安装目录

    mkdir -p /usr/local/nginx/
    
  • 解压至安装目录

    tar -zxvf nginx-1.18.0.tar.gz -C /usr/local/nginx/
    
  • 进入目录

    cd /usr/local/nginx/nginx-1.18.0
    
  • 执行编译安装命令

    ./configure
    make
    make install
    
  • 进入Nginx目录

    cd /usr/local/nginx/
    
  • 配置nginx开机启动

    vi /lib/systemd/system/nginx.service
    
  • 写入以下内容

    [Unit]
    Description=nginx 
    After=network.target 
    [Service]
    Type=forking
    ExecStart=/usr/local/nginx/sbin/nginx
    ExecReload=/usr/local/nginx/sbin/nginx reload
    ExecStop=/usr/local/nginx/sbin/nginx quit
    PrivateTmp=true 
    [Install] 
    WantedBy=multi-user.target
    
  • 添加到开机启动任务

    systemctl daemon-reload
    systemctl start nginx
    systemctl enable nginx
    
  • 如果出现错误可以使用如下命令查看日志

    sudo journalctl -u nginx.service
    

3. 安装Https模块(已安装过Nginx)

  • 进入目录

    cd /usr/local/nginx/nginx-1.18.0
    
  • 查看nginx原有的模块

    /usr/local/nginx/sbin/nginx -V
    
  • 执行编译安装命令

    ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
    
  • 配置完成后,运行命令

    make
    
  • 将刚刚编译好的nginx覆盖掉原有的nginx(这个时候nginx要停止状态)

    cp ./objs/nginx /usr/local/nginx/sbin/
    
  • 启动nginx,仍可以通过命令查看是否已经加入成功

    /usr/local/nginx/sbin/nginx -V
    

4. Configuration 防火墙

  • 查看Nginx使用端口80

    netstat -lnpt
    
  • 开放端口,这里使用80端口

    firewall-cmd --permanent --zone=public --add-port=80/tcp
    
  • 重启防火墙

    firewall-cmd --reload
    
  • 查看防火墙,已开放端口列表

    firewall-cmd --list-ports