编译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
```

相关内容

热门资讯

安卓系统app签名方案,安全与... 你有没有想过,为什么你的手机上那么多应用都能无缝运行?这其中,安卓系统app签名方案可是功不可没哦!...
安卓系统关闭应用存储,释放手机... 手机里的应用越来越多,存储空间越来越紧张,是不是感觉手机像是个装满杂物的仓库?别急,今天就来教你怎么...
安卓系统的占比,引领移动设备市... 你知道吗?在智能手机的世界里,有一个系统可是占据了半壁江山,那就是安卓系统!想象你手中的手机,是不是...
在线安卓翻译系统实现,便捷跨语... 你有没有想过,在这个信息爆炸的时代,语言不再是沟通的障碍?没错,我要说的是,在线安卓翻译系统正在悄悄...
安卓系统适配键盘丝印,打造个性... 你有没有发现,用安卓手机打字的时候,有时候键盘上的字母会变得模糊不清,甚至有时候还会出现错别字呢?这...
车载安装安卓系统教程,轻松实现... 你有没有想过给你的爱车来个“大变身”?没错,就是给车载系统来个升级,让它从那个老旧的界面跳脱出来,变...
原生安卓系统6.0精简,极致体... 亲爱的手机控们,你是否曾为手机系统臃肿、运行缓慢而烦恼?今天,就让我带你一探究竟,揭秘原生安卓系统6...
安卓系统与嵌入式系统,安卓系统... 你知道吗?在科技的世界里,有一种系统,它就像是个万能的魔法师,既能掌控手机、平板,又能深入到各种智能...
风驰软件安卓系统行吗,引领智能... 你有没有想过,手机上的软件是不是也能像风一样自由驰骋呢?今天,咱们就来聊聊这个话题——风驰软件在安卓...
安卓系统账户哪里查看,轻松查看... 你有没有想过,你的安卓手机里藏着多少秘密?别急,今天就来带你一探究竟,揭秘安卓系统账户的藏身之处!一...
鸿蒙系统和安卓系统跟ios,三... 你知道吗?在智能手机的世界里,有三个小家伙一直在暗中较劲,它们就是鸿蒙系统、安卓系统和iOS。今天,...
安卓系统登苹果账号,体验无缝跨... 你有没有想过,在安卓手机上登录苹果账号,这竟然也能成为一门学问呢?没错,随着科技的发展,跨平台操作变...
安卓系统 投屏 USb,安卓系... 你有没有想过,家里的电视和电脑是不是也能像手机一样,随时随地接上USB设备就能用呢?今天,就让我带你...
索尼平板安装安卓系统,系统升级... 亲爱的读者们,你是否曾为索尼平板电脑的局限性而感到烦恼?想要摆脱原生的系统束缚,体验安卓世界的无限可...
安卓系统的苹果游戏,跨平台体验... 你知道吗?在安卓的世界里,竟然藏着苹果的宝藏!没错,就是那些让人爱不释手的苹果游戏。今天,就让我带你...
安卓系统版本已停用,已停用版本... 你有没有发现,你的安卓手机最近是不是有点儿“老态龙钟”了?别急,让我来给你揭秘为什么你的安卓系统版本...
安卓系统老年拨号界面,关爱长辈... 你有没有发现,随着智能手机的普及,越来越多的老年人也开始尝试使用这些神奇的设备啦!不过,说起安卓系统...
安卓系统如何转换字体,轻松实现... 你有没有发现,手机上的字体有时候看久了眼睛都累了呢?别急,今天就来教你怎么给安卓手机换个新字体,让你...
禁止安卓系统更新运行,安卓系统... 你有没有遇到过这种情况?手机提示更新安卓系统,但你就是不想让它动弹?别急,今天就来聊聊这个让人头疼的...
安卓模拟苹果模拟系统,打造跨平... 你有没有想过,在安卓手机上也能体验到苹果系统的魅力呢?没错,这就是今天我要跟你分享的神奇世界——安卓...