从源码编译 Nginx 相对于 RPM 软件包来说有更多的自定义性,我们可以指定 Nginx 模块的启用与否。

环境约定

CentOS 7.7 1908

安装 Nginx 依赖

  • PCRE 支持正则表达式
  • zlib 支持 Gzip 压缩
  • OpenSSL 支持 HTTPS 协议

安装 PCRE

$ wget https://ftp.pcre.org/pub/pcre/pcre-8.44.tar.gz
$ cd pcre-8.44.tar.gz
$ ./configure
$ make && make install

安装 zlib

$ wget https://www.zlib.net/zlib-1.2.11.tar.gz
$ cd zlib-1.2.11.tar.gz
$ ./configure
$ make && make install

安装 OpenSSL

$ wget https://www.openssl.org/source/openssl-1.1.1d.tar.gz
$ cd openssl-1.1.1d.tar.gz
$ ./config
$ make && make install
$ echo "/usr/local/lib64/" >> /etc/ld.so.conf
$ ldconfig

安装 Nginx

下载并解压最新稳定版本源码

$ wget https://nginx.org/download/nginx-1.16.1.tar.gz
$ tar zxf nginx-1.16.1.tar.gz
$ cd nginx-1.16.1.tar.gz

使用 ./configure 脚本创建 Makefile

$ ./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_gunzip_module \
--with-http_gzip_static_module \
--with-http_realip_module \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_sub_module \
--with-http_v2_module \
--with-stream \
--with-stream_realip_module \
--with-stream_ssl_module \
--with-stream_ssl_preread_module \
--with-pcre=../pcre-8.44 \
--with-openssl=../openssl-1.1.1d \
--with-zlib=../zlib-1.2.11
$ make && make install

输出的 Makefile 信息

Configuration summary
  + using threads
  + using PCRE library: ../pcre-8.44
  + using OpenSSL library: ../openssl-1.1.1d
  + using zlib library: ../zlib-1.2.11

  nginx path prefix: "/etc/nginx"
  nginx binary file: "/usr/sbin/nginx"
  nginx modules path: "/usr/lib64/nginx/modules"
  nginx configuration prefix: "/etc/nginx"
  nginx configuration file: "/etc/nginx/nginx.conf"
  nginx pid file: "/var/run/nginx.pid"
  nginx error log file: "/var/log/nginx/error.log"
  nginx http access log file: "/var/log/nginx/access.log"
  nginx http client request body temporary files: "/var/cache/nginx/client_temp"
  nginx http proxy temporary files: "/var/cache/nginx/proxy_temp"
  nginx http fastcgi temporary files: "/var/cache/nginx/fastcgi_temp"
  nginx http uwsgi temporary files: "/var/cache/nginx/uwsgi_temp"
  nginx http scgi temporary files: "/var/cache/nginx/scgi_temp"

创建 Nginx 用户

$ groupadd nginx
$ useradd -s /sbin/nologin -g nginx nginx -M

创建 Nginx 临时目录 /var/cache/nginx/

$ mkdir -p /var/cache/nginx

创建 Systemd 服务文件,写入下列内容至 /usr/lib/systemd/system/nginx.service

[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID

[Install]
WantedBy=multi-user.target

重载 Systemd 服务脚本并设置 Nginx 开机自启动

$ systemctl daemon-reload
$ systemctl enable nginx