Skip to content

源码编译安装nginx

本篇文章介绍通过源码编译方式安装nginx

实战:源码编译安装Nginx-2024.12.30(测试成功)

image-20241220072429064

目录

[toc]

环境

bash
centos7.61810nginx-1.14.2.tar.gz

源码

nginx-1.14.2.tar.gz

image-20241231072116226

前提

bash
#准备下环境(检查系统是否安装了如下包,如果没,直接安装)yuminstall-ygcc-c++opensslopenssl-develpcrepcre-develzlibzlib-devel-y#如果你用的是CentOS,那么搭环境非常简单,安装这些包后,make就能准备好nginx编译的基本环境

1、下载源码包

来到nginx官网,下载源码包:

image-20241220072710602

image-20241220073540256

image-20241220073625711

bash
wgethttps:[root@localhost ~]# wget https:--2024-12-2007:47:44--https:Resolvingnginx.org(nginx.org)... 3.125.197.172,52.58.199.22,2a05:d014:5c0:2601::6,...Connectingtonginx.org(nginx.org)|3.125.197.172|:443...connected.HTTPrequestsent,awaitingresponse...200OKLength:1015384(992K) [application/octet-stream]Savingto:‘nginx-1.14.2.tar.gz’100%[==============================================================================================>]1,015,384685KB/sin1.4s2024-12-2007:47:47(685 KB/s) - ‘nginx-1.14.2.tar.gz’ saved [1015384/1015384][root@localhost ~]# ll nginx-1.14.2.tar.gz -h-rw-r--r--.1rootroot992KDec42018nginx-1.14.2.tar.gz

2、解压

bash
[root@localhost ~]# tar xf nginx-1.14.2.tar.gz [root@localhost ~]# cd nginx-1.14.2[root@localhost nginx-1.14.2]# lltotal732drwxr-xr-x.6100110014096Dec2007:49auto-rw-r--r--.110011001288742Dec42018CHANGES-rw-r--r--.110011001440121Dec42018CHANGES.rudrwxr-xr-x.210011001168Dec2007:49conf#示例脚本-rwxr-xr-x.1100110012502Dec42018configuredrwxr-xr-x.41001100172Dec2007:49contribdrwxr-xr-x.21001100140Dec2007:49html-rw-r--r--.1100110011397Dec42018LICENSEdrwxr-xr-x.21001100121Dec2007:49man-rw-r--r--.11001100149Dec42018READMEdrwxr-xr-x.91001100191Dec2007:49src

image-20241220075031935

3、源码编译安装

bash
[root@localhost nginx-1.14.2]# mkdir -p /home/geek/nginx[root@localhost nginx-1.14.2]# ./configure --prefix=/home/geek/nginx#configure,它会引用auto目录下的一系列bash脚本

image-20241230071655420

🍊执行make编译

bash
[root@localhost nginx-1.14.2]# make

image-20241230071854681

生成了nginx的目标文件:

image-20241230071945695

🍊安装

bash
[root@localhost nginx-1.14.2]# make install

image-20241230072115421

来到nginx安装目录查看下:

bash
[root@localhost nginx-1.14.2]# cd [root@localhost ~]# cd /home/geek/nginx/[root@localhost nginx]# lltotal4drwxr-xr-x.2rootroot4096Dec3007:21confdrwxr-xr-x.2rootroot40Dec3007:21htmldrwxr-xr-x.2rootroot6Dec3007:21logsdrwxr-xr-x.2rootroot19Dec3007:21sbin[root@localhost nginx]# ll sbin/total3660-rwxr-xr-x.1rootroot3746424Dec3007:21nginx[root@localhost nginx]# ll conf/total68-rw-r--r--.1rootroot1077Dec3007:21fastcgi.conf-rw-r--r--.1rootroot1077Dec3007:21fastcgi.conf.default-rw-r--r--.1rootroot1007Dec3007:21fastcgi_params-rw-r--r--.1rootroot1007Dec3007:21fastcgi_params.default-rw-r--r--.1rootroot2837Dec3007:21koi-utf-rw-r--r--.1rootroot2223Dec3007:21koi-win-rw-r--r--.1rootroot5170Dec3007:21mime.types-rw-r--r--.1rootroot5170Dec3007:21mime.types.default-rw-r--r--.1rootroot2656Dec3007:21nginx.conf-rw-r--r--.1rootroot2656Dec3007:21nginx.conf.default-rw-r--r--.1rootroot636Dec3007:21scgi_params-rw-r--r--.1rootroot636Dec3007:21scgi_params.default-rw-r--r--.1rootroot664Dec3007:21uwsgi_params-rw-r--r--.1rootroot664Dec3007:21uwsgi_params.default-rw-r--r--.1rootroot3610Dec3007:21win-utf[root@localhost nginx]#

安装完成。

结束。

FAQ

介绍下目录结构

image-20241220075143155

让nginx的conf文件使用vim打开后能够语法高亮

提示

小技巧:让nginx的conf文件能够语法高亮。

contrib目录是用来让vim打开配置文件时里代码能高亮显示:

默认用vim打开nginx配置文件是没高亮显示的:

bash
[root@localhost nginx-1.14.2]# vim conf/nginx.conf

image-20241220124334777

拷贝内容:

bash
[root@localhost nginx-1.14.2]# mkdir ~/.vim[root@localhost nginx-1.14.2]# cp -a contrib/vim/*~/.vim/

再次验证:

bash
[root@localhost nginx-1.14.2]# vim conf/nginx.conf

image-20241220124527271

此时语法就高亮了。

看下config支持哪些参数?

bash
[root@localhost nginx-1.14.2]# ./configure --help|more#编译过程中需要在哪些目录里找--prefix=PATHsetinstallationprefix--sbin-path=PATHsetnginxbinarypathname--modules-path=PATHsetmodulespath#动态模块--conf-path=PATHsetnginx.confpathname--error-log-path=PATHseterrorlogpathname--pid-path=PATHsetnginx.pidpathname--lock-path=PATHsetnginx.lockpathname#acess.log文件放在哪里#如果没有任何变动的话,只需要指定--prefix,其它所有目录都会在prefix目录下自行创建。# --with开头,编译时要启用哪些模块--with-http_ssl_moduleenablengx_http_ssl_module--with-http_v2_moduleenablengx_http_v2_module--with-http_realip_moduleenablengx_http_realip_module--with-http_addition_moduleenablengx_http_addition_module# --without开头,编译时要不启用哪些模块(从默认安装的nginx模块移出)--without-http_charset_moduledisablengx_http_charset_module--without-http_gzip_moduledisablengx_http_gzip_module--without-http_ssi_moduledisablengx_http_ssi_module

安装过程出现的问题

image-20241230070840366

自己的报错了:。。。。

bash
checkingforPCRElibrary...notfoundcheckingforPCRElibraryin/usr/local/...notfoundcheckingforPCRElibraryin/usr/include/pcre/...notfoundcheckingforPCRElibraryin/usr/pkg/...notfoundcheckingforPCRElibraryin/opt/local/...notfound./configure:error:theHTTPrewritemodulerequiresthePCRElibrary.Youcaneitherdisablethemodulebyusing--without-http_rewrite_moduleoption,orinstallthePCRElibraryintothesystem,orbuildthePCRElibrarystaticallyfromthesourcewithnginxbyusing--with-pcre=<path>option.[root@localhost nginx-1.14.2]# echo $?1

安装完成后会生成一个目录:

bash
[root@localhost nginx-1.14.2]# ll objs/total44-rw-r--r--.1rootroot20481Dec3007:06autoconf.err-rw-r--r--.1rootroot5401Dec3007:06ngx_auto_config.h-rw-r--r--.1rootroot657Dec3007:06ngx_auto_headers.h-rw-r--r--.1rootroot5725Dec3007:06ngx_modules.c#主要看这个目录,里面说明了哪些模块最终会被编译到Nginx里去。#include <ngx_config.h>#include <ngx_core.h>[root@localhost nginx-1.14.2]# vim objs/ngx_modules.cexternngx_module_tngx_core_module;externngx_module_tngx_errlog_module;externngx_module_tngx_conf_module;externngx_module_tngx_regex_module;externngx_module_tngx_events_module;externngx_module_tngx_event_core_module;externngx_module_tngx_epoll_module;externngx_module_tngx_http_module;externngx_module_tngx_http_core_module;……

🍊解决报错

bash
checkingforPCRElibrary...notfoundcheckingforPCRElibraryin/usr/local/...notfoundcheckingforPCRElibraryin/usr/include/pcre/...notfoundcheckingforPCRElibraryin/usr/pkg/...notfoundcheckingforPCRElibraryin/opt/local/...notfound./configure:error:theHTTPrewritemodulerequiresthePCRElibrary.Youcaneitherdisablethemodulebyusing--without-http_rewrite_moduleoption,orinstallthePCRElibraryintothesystem,orbuildthePCRElibrarystaticallyfromthesourcewithnginxbyusing--with-pcre=<path>option.[root@localhost nginx-1.14.2]# echo $?1报错了?

是的,根据您提供的信息,报错信息显示在编译 nginx 时找不到 PCRE 库。PCRE(Perl Compatible Regular Expressions)是一个支持正则表达式的库,nginx 的 HTTP rewrite 模块依赖于这个库。

要解决这个问题,您可以按照以下步骤之一进行操作:

  1. 安装 PCRE 库:

    • 如果您使用的是 Linux 系统,可以通过包管理器安装 PCRE 库。例如在 CentOS 系统上,可以使用 yum install pcre pcre-devel命令安装 PCRE 库。
    • 如果您使用的是其他系统,可以根据系统类型自行搜索安装 PCRE 库的方法。
  2. 指定 PCRE 库路径:

    • 如果您已经安装了 PCRE 库,但 nginx 无法找到它,可以尝试使用 --with-pcre=<path>选项来指定 PCRE 库的路径。例如:./configure --with-pcre=/usr/local/pcre
  3. 禁用 HTTP rewrite 模块:

    • 如果您不需要 HTTP rewrite 模块,可以在配置 nginx 时使用 --without-http_rewrite_module选项来禁用该模块。

您可以根据您的需求选择其中一种方法来解决问题。如果需要进一步的帮助或有其他问题,请随时告诉我。祝您顺利解决问题!


自己测试:

bash
yuminstallpcrepcre-devel-y

安装完成,再次编译前检查

bash
[root@localhost nginx-1.14.2]# ./configure --prefix=/home/geek/nginx又报错了:checkingforzliblibrary...notfound./configure:error:theHTTPgzipmodulerequiresthezliblibrary.Youcaneitherdisablethemodulebyusing--without-http_gzip_moduleoption,orinstallthezliblibraryintothesystem,orbuildthezliblibrarystaticallyfromthesourcewithnginxbyusing--with-zlib=<path>option.[root@localhost nginx-1.14.2]#

这次的报错是因为编译 nginx 时找不到 zlib 库。zlib 是一个提供数据压缩和解压功能的库,nginx 的 HTTP gzip 模块需要依赖于 zlib 库。

您可以按照以下步骤中的一种来解决这个问题:

  1. 安装 zlib 库:

    • 如果您使用的是 Linux 系统,您可以通过包管理器安装 zlib 库。例如在 CentOS 系统上,可以使用 yum install zlib zlib-devel命令来安装 zlib 库。
    • 如果您使用的是其他系统,您可以根据系统类型自行搜索安装 zlib 库的方法。
  2. 指定 zlib 库路径:

    • 如果您已经安装了 zlib 库,但 nginx 无法找到它,可以尝试使用 --with-zlib=<path>选项来指定 zlib 库的路径。例如:./configure --with-zlib=/usr/local/zlib
  3. 禁用 HTTP gzip 模块:

    • 如果您不需要 HTTP gzip 模块,可以在配置 nginx 时使用 --without-http_gzip_module选项来禁用该模块。

您可以根据您的需求选择其中一种方法来解决问题。如果需要进一步的帮助或有其他问题,请随时告诉我。祝您顺利解决问题!


再次测试:

bash
yuminstallzlibzlib-devel-y[root@localhost nginx-1.14.2]# ./configure --prefix=/home/geek/nginx

这次终于安装成功了:

image-20241230071655420

🍊执行make编译

bash
[root@localhost nginx-1.14.2]# make

image-20241230071854681

生成了nginx的目标文件:

image-20241230071945695

🍊安装

bash
[root@localhost nginx-1.14.2]# make install

image-20241230072115421

来到nginx安装目录查看下:

bash
[root@localhost nginx-1.14.2]# cd [root@localhost ~]# cd /home/geek/nginx/[root@localhost nginx]# lltotal4drwxr-xr-x.2rootroot4096Dec3007:21confdrwxr-xr-x.2rootroot40Dec3007:21htmldrwxr-xr-x.2rootroot6Dec3007:21logsdrwxr-xr-x.2rootroot19Dec3007:21sbin[root@localhost nginx]# ll sbin/total3660-rwxr-xr-x.1rootroot3746424Dec3007:21nginx[root@localhost nginx]# ll conf/total68-rw-r--r--.1rootroot1077Dec3007:21fastcgi.conf-rw-r--r--.1rootroot1077Dec3007:21fastcgi.conf.default-rw-r--r--.1rootroot1007Dec3007:21fastcgi_params-rw-r--r--.1rootroot1007Dec3007:21fastcgi_params.default-rw-r--r--.1rootroot2837Dec3007:21koi-utf-rw-r--r--.1rootroot2223Dec3007:21koi-win-rw-r--r--.1rootroot5170Dec3007:21mime.types-rw-r--r--.1rootroot5170Dec3007:21mime.types.default-rw-r--r--.1rootroot2656Dec3007:21nginx.conf-rw-r--r--.1rootroot2656Dec3007:21nginx.conf.default-rw-r--r--.1rootroot636Dec3007:21scgi_params-rw-r--r--.1rootroot636Dec3007:21scgi_params.default-rw-r--r--.1rootroot664Dec3007:21uwsgi_params-rw-r--r--.1rootroot664Dec3007:21uwsgi_params.default-rw-r--r--.1rootroot3610Dec3007:21win-utf[root@localhost nginx]#

安装完成。

结束。

需要提前安装好openssl openssl-devel

bash
我希望nginx未来能支持https,所以选了--with-http_ssl_module,但是出现报错,checkingforOpenSSLlibrary...notfoundcheckingforOpenSSLlibraryin/usr/local/...notfoundcheckingforOpenSSLlibraryin/usr/pkg/...notfoundcheckingforOpenSSLlibraryin/opt/local/...notfound./configure:error:SSLmodulesrequiretheOpenSSLlibrary.Youcaneitherdonotenablethemodules,orinstalltheOpenSSLlibraryintothesystem,orbuildtheOpenSSLlibrarystaticallyfromthesourcewithnginxbyusing--with-openssl=<path>option.你需要安装openssl,如果是centos系统,可以用yuminstall-yopensslopenssl-devel来安装

image-20241230072845241

案例:源码安装编译后生成的目录

(是的呢,我也是一样的现象)

image-20241230073007670

bash
1、make只会在源代码目录时,生成objs目录及目录中的内容。而makeinstall,会把objs目录中的内容,拷贝到--prefix目录中,例如你的/home/geep/nginx。建议你分开执行,执行完一步后观察哪些文件发生了变化。2、对的。当你运行后,特别是指令的不同,nginx会生成新的目录和文件。

案例

老师,我编译出来后的目录只有 client_body_temp fastcgi_temp logs sbin uwsgi_temp conf html proxy_temp scgi_temp这几个,那个modules目录没有,这是什么情况呢?

作者回复:modules目录是放动态模块.so文件的。当你第一次configure加入动态模块后,make install就会新建这个目录。后续更新时,需要自己手动增加这个目录。

案例:指定worker进程的运行用户

指定worker进程的运行用户。用yum安装但很难用yum升级,热升级依赖我们对nginx的高度可控。

image-20241230073155175

案例:彻底卸载编译安装的nginx目录

image-20241230073254612

案例

老师好,升级nginx操作。我新下载的nginx在执行./configure的时候--prefix指定的目录是需要指向旧的nginx所指向的prefix目录还是随便指向一个就行。 在评论区木有找到答案哈哈

作者回复:指向旧的目录

案例

请问Docker中部署NGINX有什么最佳实践可以分享的吗?

作者回复:建议在docker里用bash自己用本课程的方法部署安装Nginx,不要自己使用别人编译好的Nginx,这样无法发挥Nginx的可插拔模块化的功能

关于我

我的博客主旨:

  • 排版美观,语言精炼;
  • 文档即手册,步骤明细,拒绝埋坑,提供源码;
  • 本人实战文档都是亲测成功的,各位小伙伴在实际操作过程中如有什么疑问,可随时联系本人帮您解决问题,让我们一起进步!

🍀 个人网站

image-20241231070836953

🍀 微信二维码

x2675263825 (舍得), qq:2675263825。

image-20230107215114763

🍀 微信公众号

《云原生架构师实战》

image-20230107215126971

🍀 csdn

https:

版权:此文章版权归 One 所有,如有转载,请注明出处!

链接:可点击右上角分享此页面复制文章链接

上次更新时间:

最近更新