重新编译nginx

最近服务器上发现了/var/tmp/nginx目录下有东西被linux服务器定时清理掉了,导致前端页面访问服务器接口的时候报错了。现在要对nginx目录重新编译一遍,重新指定http-client-body-temp-path, http-proxy-temp-path, http-fastcgi-temp-path, http-uwsgi-temp-path几个编译参数

重新编译

首先要找到当初源码编译的时候的编译安装命令。如:

1
2
3
4
5
6
7
8
9
10
11
./configure 
--prefix=/usr/local/nginx
--pid-path=/var/nginx/run/nginx.pid
--lock-path=/var/nginx/lock/nginx.lock
--with-http_ssl_module
--with-http_stub_status_module
--with-http_gzip_static_module
--http-client-body-temp-path=/var/nginx/tmp/nginx/client
--http-proxy-temp-path=/var/nginx/tmp/nginx/proxy
--http-fastcgi-temp-path=/var/nginx/tmp/nginx/fastcgi
--http-uwsgi-temp-path=/var/nginx/tmp/nginx/uwsgi

这里也提醒了我们,在服务器上安装任何东西的时候,都要做好安装笔记,防止后面的时候需要用到的时候丢失!!!
这里忘了也没有关系,使用nginx -V命令中能够查看到当初的编译命令配置

进入nginx源码包

如果源码包被清理掉了,那就重新下载新的源码包。
查看nginx版本

1
2
3
4
5
6
7
[root@izbp1j3sk6yysftuzun199z sbin]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.9.9
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-file-aio --with-http_realip_module
[root@izbp1j3sk6yysftuzun199z sbin]#

使用wget http://nginx.org/download/nginx-1.9.9.tar.gz下载

进入nginx源码包,将要编译的参数修改后,新增的参数追加在后面

1
2
3
4
5
6
7
8
9
10
11
./configure 
--prefix=/usr/local/nginx
--pid-path=/var/nginx/run/nginx.pid
--lock-path=/var/nginx/lock/nginx.lock
--with-http_ssl_module
--with-http_stub_status_module
--with-http_gzip_static_module
--http-client-body-temp-path=/usr/local/nginx/temp/nginx/client
--http-proxy-temp-path=/usr/local/nginx/temp/nginx/proxy
--http-fastcgi-temp-path=/usr/local/nginx/temp/nginx/fastcgi
--http-uwsgi-temp-path=/usr/local/nginx/temp/nginx/uwsgi

再执行make命令

注意:千万不要执行make install,否则就覆盖安装了

1
[root@izbp1j3sk6yysftuzun199z nginx-1.9.9]# make

make执行完成之后,源码目录会生成一个objs目录,里面有个nginx可执行文件,这个就是我们需要的新版本程序了。接下来我们就可以准备将新版本的nginx替换掉老版本的nginx了。

1
2
3
4
5
6
[root@izbp1j3sk6yysftuzun199z nginx-1.9.9]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE Makefile man objs README src
[root@izbp1j3sk6yysftuzun199z nginx-1.9.9]# cd objs/
[root@izbp1j3sk6yysftuzun199z objs]# ls
autoconf.err Makefile nginx nginx.8 ngx_auto_config.h ngx_auto_headers.h ngx_modules.c ngx_modules.o src
[root@izbp1j3sk6yysftuzun199z objs]#
备份老版本的nginx

先将老版本的nginx进行备份,防止出现了问题,好进行复原操作。

1
[root@izbp1j3sk6yysftuzun199z objs]# cp /usr/local/nginx/sbin/nginx /usr/local/nginx/nginx.bak

覆盖旧的nginx

1
[root@izbp1j3sk6yysftuzun199z objs]# cp ./nginx /usr/local/nginx/sbin/nginx

如果提示文本文件忙的错误,建议以下命令:

1
2
3
4
5
6
[root@izbp1j3sk6yysftuzun199z objs]# cp ./nginx /usr/local/nginx/sbin/nginx
cp:是否覆盖"/usr/local/nginx/sbin/nginx"yes
cp: 无法创建普通文件"/usr/local/nginx/sbin/nginx": 文本文件忙
[root@izbp1j3sk6yysftuzun199z objs]# cp -rfp ./nginx /usr/local/nginx/sbin/nginx
cp:是否覆盖"/usr/local/nginx/sbin/nginx"yes
[root@izbp1j3sk6yysftuzun199z objs]#

测试新的nginx程序是否正确

1
2
3
4
5
6
7
8
9
10
[root@izbp1j3sk6yysftuzun199z nginx]# ./sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@izbp1j3sk6yysftuzun199z nginx]# ./sbin/nginx -V
nginx version: nginx/1.9.9
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --pid-path=/var/nginx/run/nginx.pid --lock-path=/var/nginx/lock/nginx.lock --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --http-client-body-temp-path=/usr/local/nginx/temp/nginx/client --http-proxy-temp-path=/usr/local/nginx/temp/nginx/proxy --http-fastcgi-temp-path=/usr/local/nginx/temp/nginx/fastcgi --http-uwsgi-temp-path=/usr/local/nginx/temp/nginx/uwsgi
[root@izbp1j3sk6yysftuzun199z nginx]#

使用nginx -s reload顺滑重新启动nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@iZbp1h1kfgutxc4i1si300Z nginx]# ps -ef|grep nginx
root 2089 30724 0 17:13 pts/5 00:00:00 grep --color=auto nginx
root 3649 1 0 2月18 ? 00:00:00 nginx: master process ./sbin/nginx -c ./conf/nginx.conf
root 6556 3649 0 6月17 ? 00:00:12 nginx: worker process
[root@iZbp1h1kfgutxc4i1si300Z nginx]# ./sbin/nginx -s reload
[root@iZbp1h1kfgutxc4i1si300Z nginx]# ps -ef|grep nginx
root 2095 3649 0 17:13 ? 00:00:00 nginx: worker process
root 2102 30724 0 17:13 pts/5 00:00:00 grep --color=auto nginx
root 3649 1 0 2月18 ? 00:00:00 nginx: master process ./sbin/nginx -c ./conf/nginx.conf
root 6556 3649 0 6月17 ? 00:00:12 nginx: worker process is shutting down
[root@iZbp1h1kfgutxc4i1si300Z nginx]# ps -ef|grep nginx
root 2095 3649 0 17:13 ? 00:00:00 nginx: worker process
root 2114 30724 0 17:14 pts/5 00:00:00 grep --color=auto nginx
root 3649 1 0 2月18 ? 00:00:00 nginx: master process ./sbin/nginx -c ./conf/nginx.conf
root 6556 3649 0 6月17 ? 00:00:12 nginx: worker process is shutting down
[root@iZbp1h1kfgutxc4i1si300Z nginx]# ps -ef|grep nginx
root 2095 3649 0 17:13 ? 00:00:00 nginx: worker process
root 2191 30724 0 17:15 pts/5 00:00:00 grep --color=auto nginx
root 3649 1 0 2月18 ? 00:00:00 nginx: master process ./sbin/nginx -c ./conf/nginx.conf

参考文章:

  1. https://www.cnblogs.com/effortsing/p/10012359.html
  2. https://blog.csdn.net/li_adou/article/details/78468451