nginx源码编译安装(详解)

nginx编译安装

编译安装大致步骤:

nginx源码编译安装(详解)

安装步骤:

  1. 官网下载合适的版本,建议选择稳定版本。 官网地址:https://nginx.org wget https://nginx.org/download/nginx-1.20.1.tar.gz
  2. 解压后,进入源码目录
cd /opt
tar -zxvf nginx-1.20.1.tar.gz
cd nginx-1.20.1
  1. 源码文件各目录解释
[root@localhost nginx-1.20.1]# ll
总用量 788
drwxr-xr-x 6 zdsoftro zdsoftro    326 10月 18 15:44 auto
-rw-r--r-- 1 zdsoftro zdsoftro 311503 5月  25 20:35 CHANGES
-rw-r--r-- 1 zdsoftro zdsoftro 475396 5月  25 20:35 CHANGES.ru
drwxr-xr-x 2 zdsoftro zdsoftro    168 10月 18 15:44 conf
-rwxr-xr-x 1 zdsoftro zdsoftro   2590 5月  25 20:35 configure
drwxr-xr-x 4 zdsoftro zdsoftro     72 10月 18 15:44 contrib
drwxr-xr-x 2 zdsoftro zdsoftro     40 10月 18 15:44 html
-rw-r--r-- 1 zdsoftro zdsoftro   1397 5月  25 20:35 LICENSE
drwxr-xr-x 2 zdsoftro zdsoftro     21 10月 18 15:44 man
-rw-r--r-- 1 zdsoftro zdsoftro     49 5月  25 20:35 README
drwxr-xr-x 9 zdsoftro zdsoftro     91 10月 18 15:44 src
  • auto目录:用于编译时的文件,以及相关lib库,编译时对对操作系统的判断等,都是为了辅助./configure命令执行的辅助文件。
  • CHANGES文件:就是当前版本的说明信息,比如新增的功能,修复的bug,变更的功能等
  • CHANGES.ru文件:作者是俄罗斯人,生成了一份俄罗斯语言的CHANGE文件
  • conf目录:是nginx编译安装后的默认配置文件或者示列文件,安装时会拷贝到安装的文件夹里面。
  • configure文件:编译安装前的预备执行文件。
  • contrib目录:该目录是为了方便vim编码nginx的配置文件时候,颜色突出显示,可以将该目录拷贝到自己的~/.vim目录下面 cp -rf contrib/vim/* ~/.vim/ 这样vim打开nginx配置文件就有突出的颜色显示。
  • html目录:编译安装的默认的2个标准web页面,安装后会自动拷贝到nginx的安装目录下的html下。
  • man目录:nginx命令的帮助文档,linux上可以使用man命令查看帮助,
  • src:nginx的源码文件
  • 开始编译,执行./configure 备注:nginx使用yum源安装,可能导致业务上很多需要的功能模块没有开启,还是按需自己编译比较合适。 ./configure --help #查看./configure 支持哪些参数

nginx源码编译安装(详解)

nginx源码编译安装(详解)

我演示了如何使用默认参数安装:

[En]

I demonstrate using the default parameters to install:

./configure --prefix=/home/zdsoft/nginx

没有报错代表安装完成。

备注:日常生产环境使用nginx,编译模块按照nginx官方yum安装的模块,基本能满足95%以上的生产需求。yum安装模块如下,可自行参考:

生产环境使用
./configure --prefix=/opt/xxx/xxx --user=zdsoft --group=zdsoft --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-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'
  1. ./configure执行完成后,会生成很多中间文件,放在objs目录下面
  2. 在当前命令执行make命令 make #没有报错代表执行成功 备注:这个时候,如果是第一次安装,下一步可以执行 make install命令,如果是升级,就不能执行install命令。 这个时候, 需要把objs目录下生成nginx二进制文件拷贝到原老版本的nginx目录下
  3. 执行 make install 安装命令 make install #执行安装命令,第一次安装可以执行,如果是升级,谨慎执行。
    make执行完成后生成的中间件文件,都会放在objs/src目录下面
[root@localhost src]# ll
总用量 8
drwxr-xr-x 2 root root 4096 10月 18 17:15 core
drwxr-xr-x 3 root root  191 10月 18 17:15 event
drwxr-xr-x 4 root root 4096 10月 18 17:15 http
drwxr-xr-x 2 root root    6 10月 18 16:37 mail
drwxr-xr-x 2 root root    6 10月 18 16:37 misc
drwxr-xr-x 4 root root   31 10月 18 16:37 os
drwxr-xr-x 2 root root    6 10月 18 16:37 stream
[root@localhost src]# pwd
/opt/nginx-1.20.1/objs/src
[root@localhost src]#
  1. 安装完成,安装目录为:/home/zdsoft/nginx/
nginx -v  #查看nginx版本
nginx -V  #查看nginx编译参数,如果没有额外的参数,只会显示configure arguments: --prefix=/home/zdsoft/nginx
#这情况下,代表是默认安装,可以查看源码目录auto/options 文件。默认安装了哪些模块,哪些没有安装。
  1. nginx进程启动
/home/zdsoft/nginx/sbin/nginx  -c /home/zdsoft/nginx/conf/nginx.conf

遇到的问题

  1. ./configure: error: the HTTP rewrite module requires the PCRE library. 解决: yum -y install pcre-devel
  2. ./configure: error: the HTTP gzip module requires the zlib library. 解决: yum install -y zlib-devel

Original: https://www.cnblogs.com/penngke/p/15421521.html
Author: penngke
Title: nginx源码编译安装(详解)

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/524443/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球