Nginx 编译安装
准备环境与依赖库#
在编译之前先要明白自己的需求, 比如我现在需要 https / http2 和挂多个 HTTPS 域名, 那在编译 Nginx 之前首先就要安装构建工具和 Nginx 依赖的库。
注意:安装libssl-dev 是确保 TLS SNI support enabled 的关键,因为它提供了编译所需的 OpenSSL 开发头文件。下载 Nginx 源码#
我选定的版本是 1.26.3, 前往自己喜欢的目录去拉取对应版本的源码包。
配置编译参数#
接下来将安装路径指向自定义目录, 比如我这里指定的是 /usr/local/nginx 目录。
./configure --prefix=/usr/local/nginx \
--pid-path=/usr/local/nginx/logs/nginx.pid \
--with-http_ssl_module \
--with-http_v2_module \
--with-cc-opt="-I/usr/include" \
--with-ld-opt="-L/usr/lib"
编译/安装/验证#
编译和安装。
检查 Nginx 是否安装成功。
允许免密登录#
修改 ssh 配置文件, 允许公钥登录。
PubkeyAuthentication no → PubkeyAuthentication yes
重启 ssh 服务
开机自启 nginx 服务#
创建 Nginx 的 systemd 服务配置文件。
输入以下内容:
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
重新加载 systemd 配置并启动 Nginx 服务。
查看 Nginx 服务当前的运行状态。
● nginx.service - The NGINX HTTP and reverse proxy server
Loaded: loaded (/etc/systemd/system/nginx.service; disabled; vendor preset: enabled)
Active: active (running) since Thu 2026-07-09 05:39:11 UTC; 5s ago
Process: 20504 ExecStartPre=/usr/local/nginx/sbin/nginx -t (code=exited, status=0/SUCCESS)
Process: 20505 ExecStart=/usr/local/nginx/sbin/nginx (code=exited, status=0/SUCCESS)
Main PID: 20506 (nginx)
Tasks: 2 (limit: 1090)
Memory: 2.5M
CPU: 32ms
CGroup: /system.slice/nginx.service
├─20506 "nginx: master process /usr/local/nginx/sbin/nginx"
└─20507 "nginx: worker process" "" "" "" "" "" ""
Jul 09 05:39:10 hostname systemd[1]: Starting The NGINX HTTP and reverse proxy server...
Jul 09 05:39:10 hostname nginx[PID]: nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
Jul 09 05:39:10 hostname nginx[PID]: nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
设置开机自启
成功提示: