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

相关内容

热门资讯

安卓系统新消息弹屏,体验升级 你知道吗?最近安卓系统又来了一大波新消息,这可真是让人兴奋不已!想象当你正在专心致志地刷着手机,突然...
刷机原生安卓系统,体验纯净流畅 你有没有想过,你的手机其实可以像新的一样?没错,就是那种刚从店里拿出来,充满活力的感觉。今天,就让我...
平板安卓系统升级安装,轻松实现... 亲爱的平板用户们,是不是最近你的安卓平板突然弹出了系统升级的提示,让你有点小激动又有点小紧张呢?别急...
安卓系统支持杜比吗,杜比音效技... 你有没有想过,你的安卓手机里那些美妙的音乐和电影,是不是也能享受到杜比音效的震撼呢?没错,今天咱们就...
安卓系统有哪些游戏,热门游戏盘... 你有没有发现,手机里的游戏世界简直是个宝藏库啊!尤其是安卓系统,那简直就是游戏爱好者的天堂。今天,就...
游戏苹果转换安卓系统,轻松实现... 你有没有想过,把你的苹果手机上的游戏转到安卓系统上玩呢?这听起来是不是有点像一场说走就走的旅行?别急...
安卓系统更新设置停止,节省流量 亲爱的安卓用户们,你是否也有过这样的经历:每次安卓系统更新后,总是迫不及待地想要体验新功能,却发现更...
安卓系统基础设置在哪,生成个性... 亲爱的手机控们,你是不是在安卓手机上翻来覆去,就是找不到那个神秘的“基础设置”按钮?别急,今天我就来...
安卓实时获得系统时间,安卓系统... 你有没有想过,你的安卓手机是怎么知道现在是什么时间的呢?是不是觉得这事儿很简单,不用多想?其实,这里...
安卓刷英菲克系统,安卓系统升级... 你有没有想过,你的安卓手机或者平板,是不是也能装上英菲克系统呢?听起来是不是有点不可思议?别急,今天...
安卓系统能玩的fc游戏,重温童... 你有没有想过,那些小时候陪伴我们度过无数欢乐时光的FC游戏,现在竟然可以在安卓系统上重温呢?没错,就...
安卓系统外放音量调节,享受个性... 你有没有遇到过这种情况:手机里的音乐或者视频播放得正high,突然音量突然变得超级小,让你差点没听清...
安卓系统升级多少流量,每级升级... 你有没有发现,每次安卓系统一升级,手机里的流量就像被吸走了一样,瞬间感觉不够用呢?别急,今天就来给你...
安卓系统笔记本音量,探索智能音... 你有没有遇到过这种情况:拿着你的安卓系统笔记本,正沉浸在某个精彩的电影或者游戏中,突然音量太小,让你...
华为安卓系统经典型号,引领科技... 你有没有想过,手机的世界里,有些型号就像明星一样,无论时代怎么变迁,都依然闪耀着独特的光芒?今天,就...
55英寸安卓系统,畅享科技与娱... 你有没有想过,家里的电视该升级换代了?别急,今天就来给你好好聊聊55英寸安卓系统的电视,看看它到底有...
全民k系统安卓版下载,一键下载... 你有没有听说最近超级火的全民K系统安卓版下载?没错,就是那个让无数手机用户为之疯狂的系统!今天,我就...
手机中的安卓系统,手机智能生活... 你有没有发现,现在手机里的安卓系统就像是个万能的小助手,无处不在,无所不能呢?它就像是我们生活中的得...
gm8系统是安卓系统吗,安卓生... 你有没有听说过GM8系统?最近这个话题在数码圈里可是挺火的。很多人都在问,GM8系统是安卓系统吗?今...
安卓系统文件非常大,揭秘庞大文... 你有没有发现,最近你的安卓手机越来越卡了?别急,别急,让我来给你揭秘一下这个谜团。没错,就是那个让我...