编译Nginx源码(windows10及centos7)
创始人
2024-04-25 04:52:59
0

我是为了更新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 nginxrpm -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
```

相关内容

热门资讯

安卓系统源码怎么打开,并可能需... 你有没有想过,安卓系统的源码就像是一扇神秘的门,隐藏着无数的技术秘密?想要打开这扇门,你得掌握一些小...
安卓8.0系统体验视频,智能革... 你有没有听说安卓8.0系统最近可是火得一塌糊涂啊!作为一个紧跟科技潮流的数码达人,我当然要来给你好好...
宣传系统漫画app安卓,探索安... 亲爱的读者们,你是否曾在某个午后,百无聊赖地打开手机,想要寻找一些轻松愉悦的读物?今天,我要给你介绍...
鸿蒙替换安卓系统吗,开启智能生... 你知道吗?最近科技圈里可是炸开了锅,因为华为的新操作系统鸿蒙系统,据说要大举进军手机市场,替换掉安卓...
手机安卓系统深度清理,解锁手机... 手机里的东西是不是越来越多,感觉就像一个装满了杂物的储物柜?别急,今天就来教你一招——手机安卓系统深...
安卓上的windows系统,融... 你有没有想过,在安卓手机上也能体验到Windows系统的魅力呢?没错,这就是今天我要跟你分享的神奇故...
安卓系统焦点变化事件,Andr... 你知道吗?在安卓系统的世界里,最近发生了一件超级有趣的事情——焦点变化事件。这可不是什么小打小闹,它...
一加系统安卓降级,轻松还原经典... 你有没有想过,你的手机系统升级后,突然发现某些功能变得不那么顺心了?别急,今天就来聊聊一加系统安卓降...
日本最好的安卓系统,体验非凡 亲爱的读者们,你是否曾想过,在遥远的东方,有一个国家,他们的智能手机系统独具特色,让人眼前一亮?没错...
荣耀安卓11 系统证书,保障安... 你知道吗?最近手机圈里可是炸开了锅,荣耀安卓11系统证书成了大家热议的话题。这不,我就迫不及待地来和...
安卓手机开机升级系统,体验流畅... 你有没有发现,每次你的安卓手机开机,总会有那么一刹那,屏幕上跳出一个升级系统的提示?是不是觉得这就像...
真正的安卓系统手机,安卓系统手... 你有没有想过,为什么有些人对安卓系统手机情有独钟?是不是觉得市面上的安卓手机千篇一律,缺乏个性?别急...
安卓怎么用定位系统,轻松实现精... 你有没有想过,手机里的定位系统竟然这么神奇?它不仅能帮你找到回家的路,还能在茫茫人海中找到你的好友。...
安卓的哪个系统流畅,探索新一代... 你有没有想过,为什么你的安卓手机有时候像蜗牛一样慢吞吞的,而别人的手机却像风一样快?今天,就让我带你...
安卓系统解锁工具下载,畅享自由 你是不是也和我一样,对安卓系统的解锁工具感兴趣呢?想象你的手机被锁住了,无论是忘记密码还是想尝试新的...
谷歌退出安卓系统停用,停用背后... 你知道吗?最近有个大新闻在科技圈里炸开了锅!谷歌竟然宣布要退出安卓系统,这可真是让人大跌眼镜啊!想象...
安卓系统卡顿修复,轻松提升手机... 手机用久了是不是感觉有点卡卡的呢?别急,今天就来给你支几招,让你的安卓手机重焕生机,告别卡顿的烦恼!...
安卓系统停用怎么解除,轻松恢复... 你是不是也遇到了安卓系统停用的问题,急得像热锅上的蚂蚁?别急,今天就来给你详细解析怎么解除这个让人头...
最初始的安卓系统,技术演进与产... 亲爱的读者,你是否曾好奇过,那个如今无处不在的安卓系统,它的诞生之初是怎样的呢?今天,就让我们一起穿...
patchwall系统和安卓系... 你有没有发现,手机上的界面越来越个性化了?没错,这就是科技的魅力所在。今天,咱们就来聊聊两个在个性化...