环境
- OS:CentOS Linux release 7.6.1810 (Core)
- nginx:1.16.0
安装
-
下载源码包
$ wget http://nginx.org/download/nginx-1.16.0.tar.gz
-
安装依赖包
$ yum -y install gcc make pcre pcre-devel openssl openssl-devel zlib zlib-devel
说明:
gcc:编译器 make:用于编译 pcre:让 nginx 可以使用正则表达式,rewrite 模块,core 模块 openssl:生成证书 zlib:gzip 模块需要
-
解压源码包
$ tar -zxf nginx-1.16.0.tar.gz
-
编译
$ 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:地址重写
-
安装
$ sudo make && make install
-
启动校验
$ /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
-
在
/usr/local/bin
目录创建软连接$ ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/nginx
-
启动校验
$ 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
-
启动
$ su - root [root@test-server]# nginx
-
测试 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>
-
将 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 的编译安装和初步配置。