Nginx 源码编译安装

134 阅读2分钟

参考资料

官方编译文档

编译参数介绍

Nginx 主要编译参数分为几类

  1. 路径相关参数,确定 Nginx 相关文件的安装位置,以及各种配置文件,模块文件等的位置
  2. 模块相关参数,确定 Nginx 安装模块

从官方编译文档可以看出,一般路径参数都有一个于--prefix相关的默认参数,因此如果不是有特殊需求,一般只用配置--prefix这一个参数就行,其他路径采用与此相关的默认路径

--prefix=/home/lotuscc/lotuscc-software_install/nginx

其中 Nginx 模块有两种链接方式,或者编译方式

  • 一种静态链接,模块直接编译进 Nginx 执行文件
  • 一种动态链接,模块编译成动态共享库,需要使用的时候再加载

对于官方模块,静态链接采用 --with-xxx 参数,动态链接采用 --with-xxx==dynamic 参数

对于第三方模块,静态链接采用 --add-module=PATH 参数,动态链接采用 --add-dynamic-module=PATH 参数

注意不是所有的模块都支持动态链接

./configure --help | grep dynamic

--with-http_xslt_module=dynamic    enable dynamic ngx_http_xslt_module
--with-http_image_filter_module=dynamic
                                 enable dynamic ngx_http_image_filter_module
--with-http_geoip_module=dynamic   enable dynamic ngx_http_geoip_module
--with-http_perl_module=dynamic    enable dynamic ngx_http_perl_module
--with-mail=dynamic                enable dynamic POP3/IMAP4/SMTP proxy module
--with-stream=dynamic              enable dynamic TCP/UDP proxy module
--with-stream_geoip_module=dynamic enable dynamic ngx_stream_geoip_module
--add-dynamic-module=PATH          enable dynamic external module
--with-compat                      dynamic modules compatibility

详细编译步骤

获取源码

wget https://nginx.org/download/nginx-1.23.1.tar.gz

解压

tar -zxvf nginx-1.23.1.tar.gz

配置编译参数

我的编译参数主要如下,主要配置了prefix路径,以及部分模块

其中部分模块采用静态编译,部分模块采用动态编译,同时添加了两个第三方模块

nginx-dav-ext-module

njs

./configure \
--prefix=/home/lotuscc/lotuscc-software_install/nginx \
--with-cc-opt='-g -O2 -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2' \
--with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -fPIC' \
--with-debug \
--with-compat \
--with-pcre-jit \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_realip_module \
--with-http_auth_request_module \
--with-http_v2_module \
--with-http_dav_module \
--with-http_slice_module \
--with-threads \
--with-http_addition_module \
--with-http_geoip_module=dynamic \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_image_filter_module=dynamic \
--with-http_sub_module \
--with-http_xslt_module=dynamic \
--with-stream \
--with-stream_ssl_module \
--with-stream_ssl_preread_module \
--with-mail=dynamic \
--with-mail_ssl_module \
--add-module=/home/lotuscc/lotuscc-software_sources/nginx-dav-ext-module \
--add-module=/home/lotuscc/lotuscc-software_sources/njs/nginx

配置总结如下:

Configuration summary
  + using threads
  + using system PCRE2 library
  + using system OpenSSL library
  + using system zlib library

  nginx path prefix: "/home/lotuscc/lotuscc-software_install/nginx"
  nginx binary file: "/home/lotuscc/lotuscc-software_install/nginx/sbin/nginx"
  nginx modules path: "/home/lotuscc/lotuscc-software_install/nginx/modules"
  nginx configuration prefix: "/home/lotuscc/lotuscc-software_install/nginx/conf"
  nginx configuration file: "/home/lotuscc/lotuscc-software_install/nginx/conf/nginx.conf"
  nginx pid file: "/home/lotuscc/lotuscc-software_install/nginx/logs/nginx.pid"
  nginx error log file: "/home/lotuscc/lotuscc-software_install/nginx/logs/error.log"
  nginx http access log file: "/home/lotuscc/lotuscc-software_install/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

编译安装

make -j4 && make install

错误相关

编译过程中可能会遇到缺少相关库的错误,一般直接安装相关库就行了