码上焚香

Yahocen

Linux下编译Nginx

15
2024-09-18

Linux下编译Nginx

在某些离线的内网机器上安装Nginx,没法使用Docker或者软件包管理器直接安装,记录一下离线安装的全过程。

安装基础依赖

编译Nginx所需基本依赖项需要自己下载并安装到对应服务器,如果对应服务器已经存在可以跳过此步骤。

  • 编译工具:GCC 或 Clang 等编译器、GNU make 工具等。

  • PCRE 库:用于支持正则表达式,可从 PCRE 官网下载。

  • zlib 库:用于支持 gzip 压缩,可从 zlib 官网下载。

  • OpenSSL 库:用于支持 SSL/TLS,可从 OpenSSL 官网下载。

如果需要支持更高级的功能,还需要安装其他依赖项,例如:

  • GeoIP 库:用于支持地理位置信息的查询,可从 MaxMind 的 GeoIP2 官网下载。

  • ngx_http_substitutions_filter_module 模块:用于支持响应内容替换,可从 GitHub 上的 ngx_http_substitutions_filter_module 下载。

  • ngx_http_lua_module 模块:用于支持 Lua 脚本,可从 GitHub 上的 ngx_http_lua_module 下载。

下载解压Nginx源代码(跳过)

运行configure脚本

在此处指定需要启用的模块和选项,如果模块和选项不全会导致无法启用ssl等功能,下面是一份和官方Windows版本一致模块编译配置:

./configure --prefix=/data/nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --without-http_charset_module --without-http_geo_module --without-http_split_clients_module --without-http_ssi_module --without-http_userid_module --without-mail_imap_module --without-mail_pop3_module --without-mail_smtp_module --without-stream_access_module --without-stream_geo_module --without-stream_limit_conn_module --without-stream_map_module --without-stream_return_module --without-stream_split_clients_module --without-stream_upstream_hash_module --without-stream_upstream_least_conn_module --without-stream_upstream_zone_module

--prefix=/data/nginx #指定安装目录

编译和安装

make

make install