在CentOS 7上为YUM安装的Nginx添加模块及第三方模块stream
写在前面:
❌ 你还在这样算排期吗?
撕日历、数周末、翻放假通知……
项目Deadline总算错?调休上班日漏算?
“明明该完成了,怎么又撞上节假日?”
✨ 现在,一切交给「 微信小程序:排期计算器 」!
引言
本文将详细介绍如何在CentOS 7系统上为通过YUM安装的Nginx添加新模块,特别是支持TCP层转发的stream模块。从Nginx 1.9开始,它支持基于TCP的转发,这对于需要socket通信的应用场景尤为重要。
实现过程
步骤1:查看当前Nginx版本及已编译模块
首先,确认您当前使用的Nginx版本及其配置选项:
nginx -V
这一步可以帮助您了解是否已经包含了所需的模块或是否存在冲突。
步骤2:下载同版本的Nginx源代码
前往Nginx官方网站下载与当前安装版本相同的源代码包,并解压缩:
cd /opt
wget http://nginx.org/download/nginx-1.25.2.tar.gz
tar -zxvf nginx-1.25.2.tar.gz && cd nginx-1.25.2
步骤3:备份原Nginx文件
在进行任何修改之前,请务必做好备份工作:
cp /usr/sbin/nginx{,.bak}
cp -r /etc/nginx{,.bak}
// 以下这个步骤可能不需要,我之前已经有弄过stream了
cp /usr/lib64/nginx/modules/ngx_stream_module.so /usr/lib64/nginx/modules/ngx_stream_module.so.bak
步骤4:准备依赖并重新编译Nginx
在编译前,确保所有必要的依赖项都已安装(这步耗时,请耐心等一会):
yum -y install libxml2 libxml2-devel libxslt-devel gd-devel perl-devel perl-ExtUtils-Embed GeoIP GeoIP-devel GeoIP-data pcre-devel openssl openssl-devel redhat-rpm-config.noarch gperftools gcc zlib zlib-devel
接下来,根据需求配置Nginx。如果您需要动态加载stream模块,则使用--with-stream=dynamic
;否则使用--with-stream
,直接包含该模块(即将stream打包到nginx执行文件里),这里我是直接打包进nginx文件里:
./configure --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-compat --with-debug --with-file-aio --with-google_perftools_module --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_degradation_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_mp4_module --with-http_perl_module=dynamic --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-http_xslt_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream --with-stream_ssl_module --with-stream_ssl_preread_module --with-threads --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E'
PS:这里有必要说下,上面一大串configure是来自nginx -V
命令:
步骤5:编译并验证
完成配置后,执行以下命令进行编译:
make
编译完成后,不要立即执行make install
,而是先进行测试以确保新的Nginx二进制文件没有问题:
/opt/nginx-1.25.2/objs/nginx -t
/opt/nginx-1.25.2/objs/nginx -V
如果测试报错
nginx: [emerg] mkdir() “/var/lib/nginx/tmp/client_body” failed (2: No such file or directory)
执行命令
mkdir -p /var/lib/nginx/tmp/client_body
再验证一下:
/opt/nginx-1.25.2/objs/nginx -t
/opt/nginx-1.25.2/objs/nginx -V
步骤6:替换Nginx文件并重启服务
如果测试无误,可以安全地替换旧的Nginx二进制文件:
make install
// 或执行 (可能出现文件繁忙): cp /opt/nginx-1.25.2/objs/nginx /usr/sbin/
重启nginx
systemctl restart nginx
如果您是动态加载模块(如stream),这里还需要在nginx.conf
中添加如下行来加载模块:
load_module /usr/lib64/nginx/modules/ngx_stream_module.so;
步骤7:检查结果
最后,检查新的Nginx版本和模块状态:
nginx -V
确保--with-stream
或--with-stream=dynamic
出现在输出中,表明模块已成功添加。
常见问题及解决方法
- 缺少依赖:根据错误信息,逐一安装缺失的依赖库。
- 无法覆盖Nginx文件:可能由于文件被占用,建议先停止Nginx服务再进行替换操作。
- 模块未加载:确保在
nginx.conf
中正确加载了动态模块。
nginx编译配置,–with-stream和–with-stream=dynamic,有什么区别
在编译nginx时,–with-stream和–with-stream=dynamic两个选项都是用于启用nginx的stream模块。
- –with-stream
选项会将stream模块静态地编译进nginx中,使其成为nginx的核心模块。这意味着在nginx启动时,stream模块将被加载并一直存在,无法动态加载或卸载。 - –with-stream=dynamic
选项会将stream模块编译为动态模块(.so文件),在nginx启动时不会加载stream模块,而是在需要时动态加载。这样可以减小nginx的二进制文件大小,并且可以根据需要灵活地加载或卸载stream模块。
总的来说,如果你确定需要使用stream模块,并且不会改变加载与卸载它的需求,那么使用–with-stream选项更为简单。而如果你希望动态地加载或卸载stream模块,或者只在特定场景下使用stream模块,那么使用–with-stream=dynamic选项更为合适。
写在最后:
🔥 排期计算器,真实场景救急:
产品经理:“这个需求开发要20个工作日,哪天可以上线?”
🚀 微信小程序:排期计算器,告别手动算到崩溃!