怎样编译安装Nginx

2019年10月28日 | 分类: 【技术】

【环境】

在 CentOS 7 / CentOS 8 / Ubuntu 18.4 编译安装 Nginx 。
也可以通过RPM包快速安装。但不推荐,因为新模块的安装依然需要从源码编译。

参考:https://amon.org/nginx1220

【安装】

安装依赖 :

yum install -y pcre pcre-devel zlib zlib-devel

下载 Nginx 源码包:

下载:http://nginx.org/download/

cd /root && wget http://nginx.org/download/nginx-1.25.3.tar.gz && tar zxf nginx-1.25.3.tar.gz

配置 Nginx 支持 HTTP/2 :

参考:https://amon.org/nginx-http2

1. 在编译 Nginx时加入 –with-http_v2_module
2. nginx.conf 中设置:listen 443 ssl http2;

下载 OpenSSL 源码包:

参考:https://amon.org/openssl

注意:Nginx屡次报错找不到OpenSSL。原来并发指定OpenSSL的安装路径,而是OpenSSL的源码路径;因此应该将Openssl源码包解压缩到Nginx的源码目录下处理。
参考:https://zhidao.baidu.com/question/1515545728986614340.html

查看当前 OpenSSL 版本:

openssl version

如果是 OpenSSL 1.1.1g :

cd /root/nginx-1.25.3 && wget https://www.openssl.org/source/openssl-1.1.1g.tar.gz && tar zxvf openssl-1.1.1g.tar.gz

添加 nginx 用户组 和 用户 nginx :

/usr/sbin/groupadd -f nginx && /usr/sbin/useradd -g nginx nginx

下载 modsecurity 源码包 :

先编译 libmodsecurity 。

参考:https://amon.org/modsecurity

下载源码到 nginx 源代码目录:

cd /root/nginx-1.25.3 && git clone --depth 1 https://github.com/SpiderLabs/ModSecurity-nginx.git

下载 headers-more-nginx-module 源码包 :

下载源码到 nginx 源代码目录:

cd /root/nginx-1.25.3 && git clone --depth 1 https://github.com/openresty/headers-more-nginx-module.git

编译安装 Nginx :

CentOS 8 环境(OpenSSL 1.1.1g):

cd /root/nginx-1.25.3 && ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-http_v2_module  --with-openssl=./openssl-1.1.1g  --add-dynamic-module=./ModSecurity-nginx --add-dynamic-module=./headers-more-nginx-module --without-pcre2
make && make install

添加 nginx 到系统命令:

echo -e '\n\nexport PATH=/usr/local/nginx/sbin:$PATH\n' >> /etc/profile && source /etc/profile

检查 Nginx 版本:

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

CentOS 8 环境下输出:

nginx version: nginx/1.25.3
built by gcc 8.5.0 20210514 (Red Hat 8.5.0-4) (GCC)
built with OpenSSL 1.1.1g  21 Apr 2020
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-http_v2_module --with-openssl=./openssl-1.1.1g --add-dynamic-module=./ModSecurity-nginx --without-pcre2

可以看到 TLS SNI support 已经启用。

配置 Nginx 应用服务器:

配置路径:/usr/local/nginx/conf/nginx.conf

参考:https://amon.org/nginx-conf

源码路径:/usr/local/nginx/html

添加 nginx 对程序目录有执行权限:

chown -R nginx:nginx /usr/local/nginx/html

启动 Nginx :

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

如果有修改配置文件,先检查正确性:

/usr/local/nginx/sbin/nginx -t

重启 Nginx :

/usr/local/nginx/sbin/nginx -s reload

在浏览器中打开:https://stripe.wiki/ ,发现http正常跳转到https,而且支持h2。

设置nginx开机自启动:

参考:https://amon.org/nginx-init

Nginx服务设置:dos2unix 转换/设置执行权限/注册成服务/设置开机启动

dos2unix /etc/init.d/nginx && chmod a+x /etc/init.d/nginx && chkconfig --add nginx && chkconfig nginx on

查看版本:

nginx -v

输出:

nginx version: nginx/1.25.3

【命令】

service nginx start
service nginx stop
service nginx restart
service nginx status
service nginx reload

【测试】

查看端口:

netstat -tulpn | grep nginx

输出:

tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      11351/nginx: master

是否可以访问网站,然鹅不能,是因为 firewalld 需要进一步设置,先关闭:

service firewalld stop

然后在浏览器中访问 IP :

Welcome to nginx!

【配置】

配置:Nginx使用cdn后获取用户真实IP

参考:https://www.quji.org/archives/5566

一个站点使用的是Nginx,但是配上cdn以后显示的用户IP是cdn的节点IP,然后我网上找了一下,发现可以解决这个问题,特此记录。

在nginx.conf的配置文件http下面加入:

set_real_ip_from 0.0.0.0/0;
real_ip_header X-Forwarded-For;

然后重启nginx。再次查看用户的IP,就变成真实的用户IP。

【排错】

报错:‘ngx_http_headers_in_t’ has no member named ‘cookies’; did you mean ‘cookie’?

make 时报错。

参考:https://trac.nginx.org/nginx/ticket/2358
参考:https://github.com/arut/nginx-dav-ext-module/issues/60

As per the build output, this is a problem in the 3rd party module ngx_http_headers_more
For the record, there are multiple header-releated changes in nginx 1.25.3 internal API
The 3rd party modules you are using need to be adjust to work with nginx 1.25.3.

有待补丁。

报错: error: ‘pcre_malloc’ undeclared

当 make 时,报错:

make[1]: *** [objs/Makefile:1419: objs/addon/src/ngx_http_modsecurity_module.o] Error 1
make[1]: Leaving directory '/root/nginx-1.25.3'
make: *** [Makefile:10: build] Error 2

参考:https://github.com/SpiderLabs/ModSecurity-nginx/issues/264

long story short – use –without-pcre2 configure argument when building connector with nginx after 1.21.5

方法:在 configure 时添加 –without-pcre2

报错:An error occurred. Sorry, the page you are looking for is currently unavailable.

查看 nginx 日志,发现:failed (2: No such file or directory)

仔细查看,原来网站目录名出现拼写错误。

报错:connect() failed (111: Connection refused)

当 LNMP 各项配置OK的时候,打开 IP ,发现页面报错:502 Bad Gateway

查看日志:/usr/local/nginx/logs/error.log

 connect() failed (111: Connection refused) while connecting to upstream ... upstream: "fastcgi://127.0.0.1:9000"...

参考:https://www.cnblogs.com/xbxxf/p/9132163.html

方法:因为 Nginx 和 PHP 有两种链接方式

第一种(源码编译方式)

fastcgi_pass 127.0.0.1:9000;

另一种(迅速安装方式)

fastcgi_pass unix:/run/php/php-fpm.sock;

具体使用哪一种方式需要查看 php-fpm 的配置文件:

第一种(源码编译方式):/usr/local/php/etc/php-fpm.d/www.conf

listen = 127.0.0.1:9000

Listen 是端口,就按第一种链接方式。

另一种(迅速安装方式):/etc/php-fpm.d/www.conf

listen = /run/php-fpm/www.sock

必须保持一致。本例是第二种链接方式,修正后测试无误。

参考:https://blog.csdn.net/LY_Dengle/article/details/69568652

报错:the “ssl” directive is deprecated, use the “listen … ssl

参考:https://www.bbsmax.com/A/nAJvbjnGJr/

新版nginx采用新的方式进行监听https请求,解决方法:
1. listen 443 ssl;
2. 删除ssl配置 ssl on

报错:headers_more_filter_module.so” is not binary compatible

参考:https://blog.csdn.net/qq_35011172/article/details/88873861

原因:第三方模块的编译中包含的签名和使用的nignx不一致。

办法:先通过 nginx -V 命令得到当前配置的configure配置,在复制所有的配置命令。添加到:
./configure [“你的nignx -V 得到的配置参数”] –add-dynamic-module=/etc/pan1/fastdfs-nginx-mo,重新生成第三方模块。

参考:https://amon.org/headers-more-nginx-module

造成这个问题的原因大致有两个:

1. php-fpm没有运行
执行如下命令查看是否启动了php-fpm,如果没有则启动你的php-fpm即可。
用命令 netstat -ant | grep 9000 查看php-fpm进程,没有运行则启动。

2. 可能是php-fpm队列满了
php-fpm.conf 配置文件 pm.max_children 修改大一点,然后重启 php-fpm 并观察日志情况。

报错:nginx emerg fastcgi_index directive is duplicate

这个错误的意思是您在 Nginx 配置文件中使用了重复的 fastcgi_index 指令。
fastcgi_index 指令用于指定 FastCGI 的索引文件。在 Nginx 配置中,您只需要指定一次 fastcgi_index 指令,但如果您在配置文件的不同位置重复指定了该指令,就会出现这个错误。
要解决这个问题,您需要打开 Nginx 配置文件,查找并删除重复的 fastcgi_index 指令。通常,最好只在 location 块中设置 fastcgi_index 指令,以确保它只在需要时才被使用。
如果您仍然无法解决这个问题,可以尝试在 Nginx 配置文件中搜索所有的 fastcgi_index 指令,并查看它们是否存在重复。一旦您找到了重复的指令,只需将其中一个删除即可。

参考:https://juejin.cn/s/nginx%20emerg%20fastcgi_index%20directive%20is%20duplicate