CentOS安装编译 nginx 并使用 systemd 进行服务管理

环境

  • OS:CentOS Linux release 7.6.1810 (Core)
  • nginx:1.16.0

安装

  1. 下载源码包

    $ wget http://nginx.org/download/nginx-1.16.0.tar.gz
    
  2. 安装依赖包

    $ yum -y install gcc make pcre pcre-devel openssl openssl-devel zlib zlib-devel
    

    说明:

    gcc:编译器
    make:用于编译
    pcre:让 nginx 可以使用正则表达式,rewrite 模块,core 模块
    openssl:生成证书
    zlib:gzip 模块需要
    
  3. 解压源码包

    $ tar -zxf nginx-1.16.0.tar.gz
    
  4. 编译

    $ cd nginx-1.16.0
    $ ./configure --prefix=/usr/local/nginx --user=bob --group=bob --with-http_ssl_module --with-http_v2_module --with-http_stub_status_module --with-pcre
    

    说明:

    --user=bob:指定运行 nginx 程序的用户
    --group=bob:指定运行 nginx 的用户组
    --prefix=/usr/local/nginx:指定安装路径
    --with-http_ssl_module:支持 https
    --with_http_v2_module:https的快速访问
    --with-http_stub_status_module:nginx 的信息查询
    --with-pcre:地址重写
    
  5. 安装

    $ sudo make && make install
    
  6. 启动校验

    $ /usr/local/nginx/sbin/nginx -V
    nginx version: nginx/1.16.0
    built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) 
    built with OpenSSL 1.0.2k-fips  26 Jan 2017
    TLS SNI support enabled
    configure arguments: --prefix=/usr/local/nginx --user=bob --group=bob --with-http_ssl_module --with-http_v2_module --with-http_stub_status_module --with-pcre
    
  7. /usr/local/bin 目录创建软连接

    $ ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/nginx
    
  8. 启动校验

    $ nginx -V
    nginx version: nginx/1.16.0
    built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) 
    built with OpenSSL 1.0.2k-fips  26 Jan 2017
    TLS SNI support enabled
    configure arguments: --prefix=/usr/local/nginx --user=bob --group=bob --with-http_ssl_module --with-http_v2_module --with-http_stub_status_module --with-pcre
    
  9. 启动

    $ su - root
    [root@test-server]# nginx
    
  10. 测试 nginx 是否启动成功

    $ curl http://127.0.0.1:80
    <!DOCTYPE html>
    <html>
    <head>
    <title>Welcome to nginx!</title>
    <style>
        body {
            width: 35em;
            margin: 0 auto;
            font-family: Tahoma, Verdana, Arial, sans-serif;
        }
    </style>
    </head>
    <body>
    <h1>Welcome to nginx!</h1>
    <p>If you see this page, the nginx web server is successfully installed and
    working. Further configuration is required.</p>
    
    <p>For online documentation and support please refer to
    <a href="http://nginx.org/">nginx.org</a>.<br/>
    Commercial support is available at
    <a href="http://nginx.com/">nginx.com</a>.</p>
    
    <p><em>Thank you for using nginx.</em></p>
    </body>
    </html>
    
    
  11. 将 nginx 配置为系统服务并设置开机自启

    /etc/systemd/system/ 目录下新建名为 nginx.service 的文件,内容如下:

    [Unit]
    Description=nginx server daemon
    Documentation=http://nginx.org/en/docs/
    After=network.target
    
    [Service]
    Type=forking
    PIDFile=/run/nginx.pid
    ExecStartPre=/usr/local/bin/nginx -t
    ExecStart=/usr/local/bin/nginx
    ExecReload=/usr/local/bin/nginx -s reload
    ExecStop=/bin/kill -s QUIT $MAINPID
    PrivateTmp=true
    
    [Install]
    WantedBy=multi-user.target
    

    修改 /urs/local/nginx/conf/nginx.conf 文件,将下面一行内容:

    #pid        run/nginx.pid;
    

    改为

    pid        /run/nginx.pid;
    

    注意

    如果不修改配置文件 nginx.conf ,那么在启动 nginx 的时候会报如下错误:

    systemd[1]: PID file /run/nginx.pid not readable (yet?) after start.

    然后依次执行如下命令:

    $ sudo systemctl daemon-reload          # 重新加载Units
    $ sudo systemctl enable nginx.service   # 将 nginx 服务设置为开机启动
    $ sudo systemctl start nginx.service    # 启动 nginx 服务
    $ sudo systemctl status nginx.service   # 查看 nginx 服务状态
    $ sudo systemctl stop nginx.service     # 停止 nginx 服务
    $ sudo systemctl status nginx.service   # 查看 nginx 服务状态
    $ sudo systemctl restart nginx.service  # 重启 nginx 服务
    $ sudo systemctl status nginx.service   # 查看 nginx 服务状态
    

    注意:

    关于使用 systemd 配置 nginx 服务,请参考 NGINX systemd service file

    若上述命令执行后均未发生错误并且查看 nginx 服务状态时未显示异常信息,则说明配置正确。

到此,即完成了 nginx 的编译安装和初步配置。

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值