编译Nginx源码(windows10及centos7)
我是为了更新nginx的openssl,所以才重新编译,版本为nginx-1.22.1及openssl-1.1.1s,不适用openssl3.x
1 windows10
1.1 工具
主要工具是vs2017(其他版本也可)、MSYS2、Perl,ActivePerl或Strawberry,我使用的是后者,pcre、zlib
我用的都是目前最新版的,这里面是安装openssl模组才需要的
参考文章
nginx官网教程
Microsoft Visual C compiler. Microsoft Visual Studio® 8 and 10 are known to work.
MSYS or MSYS2.
Perl, if you want to build OpenSSL® and nginx with SSL support. For example ActivePerl or Strawberry Perl.
Mercurial client.
PCRE, zlib and OpenSSL libraries sources.
1.2 下载nginx源码及openssl-1.1.1s
注意:Nginx源码一定要到这个地址http://hg.nginx.org/nginx
我下载的是当前最新的稳定版本,你们也可以根据需要下载其他版本。release-1.22.1 tag stable-1.22进去下载。
openssl-1.1.1s
编译过程
编译过程简单,也没什么大坑,有些小问题百度就可以,参考文章
其中configure参数,建议使用官网对应版本的参数,我下面这个是1.22.1的,加了openssl
注意这个参数,objs/lib是下载解压openssl的位置
–with-openssl=objs/lib/openssl-1.1.1s
./configure --prefix=/usr/local/nginx --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_stub_status_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_slice_module --with-pcre --with-mail --with-stream --with-stream_ssl_module --with-stream_realip_module --with-http_ssl_module --with-openssl=objs/lib/openssl-1.1.1s
编译好之后,下载官网最新的nginx-1.20.2.zip,把我们编译的nginx.exe替换进去,这样就得到一个自己编译完整的nginx版本安装包。用命令nginx -t和nginx -V进行检验和参考版本。
2 centos7
windows编译后是个完整安装包,替换配置文件就好,对服务影响不大,centos的话,默认安装的文件夹分布太散了。因此为了尽量不影响业务,采用的方法是编译相同版本的nginx,服务器上面替换执行文件即可。
2.1 安装相关依赖
这里我要编译自己的openssl版本,如果不是得安装,参数后面加上openssl openssl-devel
yum -y install gcc gcc-c++ zlib zlib-devel pcre-devel gcc gcc-c++
2.2 解压nginx-1.22.1.tar.gz及openssl-1.1.1s.tar.gz
我的操作是在opt文件下完成的
tar -zxvf nginx-1.22.1.tar.gz
mkdir -p nginx-1.22.1/objs/lib && tar -zxvf openssl-1.1.1s.tar.gz -C ./nginx-1.22.1/objs/lib
2.3 编译安装
进入nginx目录
cd nginx-1.22.1/
2.3.1 执行configure(若报错,查看报错提示,通常是相关依赖未安装)
这里同样是nginx官网的参数
- –prefix安装目录(与现有安装目录不同)
- –with-openssl是openssl解压目录
- module可视情况添加或删除部分
./configure --prefix=/etc/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 --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --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-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-openssl=objs/lib/openssl-1.1.1s --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
进行make编译,如果没有安装过nginx,可以直接make && make install进行编译安装了。如果是直接安装了,后面就不用看了。
make
编译完成后,在opt/nginx-1.22.1/objs文件里就得到了nginx文件,可以下载下来,到其他服务器使用
2.4 更新nginx版本到1.22.1(生产不建议,直接编译相同版本nginx进行替换比较稳妥)
2.4.1 备份相关配置文件,防止意外(重要!!!)
使用yum升级nginx(如果不能更新,就只能rpm重装最新版)
yum update nginx -y
测试检查和重启nginx
# /usr/sbin/nginx -c /etc/nginx/nginx.conf #如果未启动则先启动
nginx -t && nginx -s reload # 重启
ps -ef | grep nginx # 查看nginx进程
2.4.2 (有问题)备份好配置文件,卸载nginx并重装1.22.1版本
yum remove nginx
rpm -Uvh http://nginx.org/packages/centos/7/x86_64/RPMS/nginx-1.22.1-1.el7.ngx.x86_64.rpm
2.5 替换nginx文件并启动
2.5.1 上传相对应版本的nginx到opt文件夹内并赋予权限
chmod 755 /opt/nginx
2.5.2 备份和替换nginx文件
mv /usr/sbin/nginx /usr/sbin/nginx.bak && mv /opt/nginx /usr/sbin/nginx
2.5.3 测试校验并启动
# /usr/sbin/nginx -c /etc/nginx/nginx.conf #如果未启动则先启动
nginx -t && nginx -s reload # 重启
1.4 (有问题)还原备份文件
rm -rf /usr/sbin/nginx && mv /usr/sbin/nginx.bak /usr/sbin/nginx
THE END
1.20.1版本的参数
./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=dynamic --with-stream_ssl_module --with-stream_ssl_preread_module --with-openssl=objs/lib/openssl-1.1.1s --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 -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie
```