linux编译安装nginx

本文升级过程,适用于大部分nginx编译版本

./configure --prefix=/usr/local/nginx --sbin-path=/usr/sbin/nginx 
--conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log 
--http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid 
--lock-path=/var/lock/nginx.lock --user=nginx --group=nginx --with-http_ssl_module 
--with-http_stub_status_module --with-http_gzip_static_module --http-client-body-temp-path=/var/tmp/nginx/client/ 
--http-proxy-temp-path=/var/tmp/nginx/proxy/ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ 
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --with-pcre=/usr/local/pcre-8.44 
--with-zlib=/usr/local/zlib-1.2.11 --with-openssl=/usr/local/openssl-1.1.1i

常用编译选项说明
nginx大部分常用模块,编译时./configure –help以–without开头的都默认安装。

–prefix=PATH : 指定nginx的安装目录。默认 /usr/local/nginx
–conf-path=PATH : 设置nginx.conf配置文件的路径。nginx允许使用不同的配置文件启动,通过命令行中的-c选项。默认为prefix/conf/nginx.conf
–user=name: 设置nginx工作进程的用户。安装完成后,可以随时在nginx.conf配置文件更改user指令。默认的用户名是nobody。–group=name类似
–with-pcre : 设置PCRE库的源码路径,如果已通过yum方式安装,使用–with-pcre自动找到库文件。使用–with-pcre=PATH时,需要从PCRE网站下载pcre库的源码(版本4.4 – 8.30)并解压,剩下的就交给Nginx的./configure和make来完成。perl正则表达式使用在location指令和 ngx_http_rewrite_module模块中。
–with-zlib=PATH : 指定 zlib(版本1.1.3 – 1.2.5)的源码解压目录。在默认就启用的网络传输压缩模块ngx_http_gzip_module时需要使用zlib 。
–with-http_ssl_module : 使用https协议模块。默认情况下,该模块没有被构建。前提是openssl与openssl-devel已安装
–with-http_stub_status_module : 用来监控 Nginx 的当前状态
–with-http_realip_module : 通过这个模块允许我们改变客户端请求头中客户端IP地址值(例如X-Real-IP 或 X-Forwarded-For),意义在于能够使得后台服务器记录原始客户端的IP地址
–add-module=PATH : 添加第三方外部模块,如nginx-sticky-module-ng或缓存模块。每次添加新的模块都要重新编译(Tengine可以在新加入module时无需重新编译)

步骤:
1、下载nginx源码文件,本次使用nginx-1.19.5

2、解压nginx的tar包:tar xf nginx-1.19.5.tar.gz

3、进入nginx目录./configure,命令如上所示

4、创建以上相关目录

  mkdir /usr/local/nginx 

  mkdir /usr/sbin/nginx  mkdir /etc/nginx

  mkdir /var/log/nginx   mkdir /var/run/nginx

5、进行编译make,如果编译报错,出现Error字样,试着降低pcre版本,编译正常则提示make[1]: Leaving directory `/usr/local/nginx-1.19.5',此时已经编译成功

6、进行安装make install
7、/usr/sbin/nginx -c /usr/local/nginx/conf/nginx.conf 如果nginx启动不了,报[emerg]: getpwnam(“nginx”) failed,可能是没有安装nginx用户导致的无法启动
在nginx源码目录执行useradd -s /sbin/nologin -M nginx,然后执行id nginx

8、找到nginx相关目录,使用whereis nginx,到/usr/sbin目录下执行./nginx再次启动nginx,如果提示80端口被占用,则到/etc/nginx目录修改默认的监听端口
有时,80端口被占用,是由于Apache2服务的原因,解决方案sudo service apache2 stop,如果存在旧的nginx,需要关闭旧的nginx,否则也会提示端口被占用

9、nginx -s reload 的时候报错,nginx: [error] invalid PID number "" in "/run/nginx.pid",说明旧的nginx还在进程里运行,但是nginx.pid记录的pid与进程里的nginx的pid不一致。
ps -ef|grep nginx,看到ngiinx真的还在进程里,这样的话,需要关闭原本所有旧的nginx进程。关闭了旧nginx,重启nginx安装才算完成。打开nginx:nginx -c /etc/nginx/nginx.conf,
复制旧配置,到/etc/nginx目录,加载nginx配置,nginx -s reload。从这里,可以反思,nginx升级过程,只要不关闭nginx进程,服务器内的项目都可以不受影响。

10、nginx升级需要pcre源码、openssl源码、zlib源码,可以都解压到/usr/local目录底下,通过命令(--with-pcre=/usr/local/pcre-8.44 --with-zlib=/usr/local/zlib-1.2.11
--with-openssl=/usr/local/openssl-1.1.1i)来指定。

11、提示 ./configure: error: C compiler cc is not found,apt-get install gcc,apt-get install build-essential

12、在任意路径启动nginx方法,执行文件软链接到/usr/local/sbin底下,ln -s /usr/sbin/nginx/nginx /usr/local/sbin/nginx

 

问题1: 

服务器重启之后,执行 nginx -t 是OK的,然而在执行 nginx -s reload 的时候报错

nginx: [error] invalid PID number "" in "/run/nginx.pid"

解决方法:

需要先执行

ps -ef|grep nginx kill掉所有nginx进程

nginx -c /etc/nginx/nginx.conf

nginx.conf文件的路径可以从nginx -t的返回中找到。

nginx -s reload

问题2:make过程中报错,nginx configure: error: Invalid C++ compiler or C++ compiler flags

解决:yum install gcc-c++

问题3:make失败,因为./configure下那些路径不存在,比如/usr/sbin/没有nginx目录,/etc底下没有nginx目录

问题4:执行./nginx -c /etc/nginx/nginx.conf

报错nginx: [emerg] mkdir() "/var/tmp/nginx/client/" failed (2: No such file or directory)

解决:创建目录/var/tmp/nginx/client/

 

参考:
https://www.runoob.com/w3cnote/nginx-install-and-config.html
https://www.cnblogs.com/cl-rr/p/11447231.html
https://blog.51cto.com/favccxx/1620159
https://www.cnblogs.com/zrbfree/p/6419043.html
https://blog.csdn.net/hu_feng903/article/details/80297821

posted @ 2021-12-18 15:18  摩斯  阅读(935)  评论(1编辑  收藏  举报