Nginx 是一个高性能的 HTTP 和反向代理服务器。以下是编译安装 Nginx 的详细步骤:
# 对于基于 Debian/Ubuntu 的系统
sudo apt update
sudo apt install -y build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev
# 对于基于 RHEL/CentOS 的系统
sudo yum groupinstall -y "Development Tools"
sudo yum install -y pcre pcre-devel zlib zlib-devel openssl openssl-devel
wget http://nginx.org/download/nginx-<version>.tar.gz
tar -zxvf nginx-<version>.tar.gz
cd nginx-<version>
将 <version>
替换为你需要的 Nginx 版本号,如 1.25.3
。
./configure \
--prefix=/usr/local/nginx \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--pid-path=/var/run/nginx.pid \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_stub_status_module \
--with-threads \
--with-file-aio
常用编译选项说明:
- --prefix
: 安装目录
- --with-http_ssl_module
: 启用 HTTPS 支持
- --with-http_v2_module
: 启用 HTTP/2 支持
- --with-http_realip_module
: 获取客户端真实 IP
- --with-http_stub_status_module
: 启用状态监控页面
运行 ./configure --help
查看所有可用选项。
make
sudo make install
nginx -v
创建 systemd 服务文件 /etc/systemd/system/nginx.service
:
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/usr/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
TimeoutStopSec=5
KillMode=mixed
[Install]
WantedBy=multi-user.target
启用并启动服务:
sudo systemctl enable nginx
sudo systemctl start nginx
nginx
或 systemctl start nginx
nginx -s stop
或 systemctl stop nginx
nginx -s reload
或 systemctl reload nginx
nginx -t
如需更高级的配置,可以参考 Nginx 官方文档或使用 nginx -V
查看当前编译参数。