NGINX 源码编译安装与基础使用

NGINX 是一款高性能的 Web 服务器,也常用于反向代理、负载均衡、静态资源托管和 HTTPS 终端接入。本文整理了一份比较完整的 NGINX 源码编译安装方案,覆盖依赖准备、源码下载、编译参数配置、systemd 服务创建、启动验证以及常见问题说明。

说明:本文以 NGINX 1.24.0 版本为例,同时保留了 1.22.x 的通用安装思路。不同版本的下载地址和部分参数可能略有差异,实际使用时请根据需要调整。

1. 环境准备

Ubuntu / Debian

1
2
sudo apt update
sudo apt install -y build-essential wget curl ca-certificates

CentOS / Rocky / RHEL

1
2
sudo dnf update -y
sudo dnf groupinstall -y "Development Tools"

2. 创建工作目录

1
2
mkdir -p ~/nginx
cd ~/nginx

3. 下载 Nginx 与依赖包

1
2
3
4
wget https://nginx.org/download/nginx-1.24.0.tar.gz
wget https://www.openssl.org/source/openssl-1.1.1f.tar.gz
wget https://www.zlib.net/zlib-1.3.tar.gz
wget https://nchc.dl.sourceforge.net/project/pcre/pcre/8.45/pcre-8.45.tar.gz

4. 解压源码

1
2
3
4
5
6
cd ~/nginx

tar -zxvf nginx-1.24.0.tar.gz
tar -zxvf openssl-1.1.1f.tar.gz
tar -zxvf zlib-1.3.tar.gz
tar -zxvf pcre-8.45.tar.gz

5. 编译安装

5.1 配置编译参数

进入 NGINX 源码目录:

1
cd ~/nginx/nginx-1.24.0

执行编译配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
./configure \
--prefix=/usr \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/run/nginx.pid \
--lock-path=/run/nginx.lock \
--user=nginx \
--group=nginx \
--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 \
--with-select_module \
--with-poll_module \
--with-threads \
--with-file-aio \
--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_auth_request_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_degradation_module \
--with-http_slice_module \
--with-http_stub_status_module \
--with-mail=dynamic \
--with-mail_ssl_module \
--with-stream \
--with-stream_ssl_module \
--with-stream_realip_module \
--with-stream_ssl_preread_module \
--with-compat \
--with-pcre=../pcre-8.45 \
--with-pcre-jit \
--with-zlib=../zlib-1.3 \
--with-openssl=../openssl-1.1.1f \
--with-openssl-opt=no-nextprotoneg \
--with-debug

5.2 编译与安装

1
2
make -j$(nproc)
sudo make install

安装完成后,NGINX 的目录布局会更接近通过 apt 安装的方式:

  • 二进制:/usr/sbin/nginx
  • 配置文件:/etc/nginx/nginx.conf
  • 日志:/var/log/nginx/error.log/var/log/nginx/access.log
  • PID 文件:/run/nginx.pid
  • 运行缓存目录:/var/cache/nginx/

这也是更符合 Linux 发行版习惯的目录结构。

6. 创建运行用户与标准目录

为了安全起见,建议创建一个专用用户来运行 NGINX:

1
sudo useradd -r -s /sbin/nologin nginx

创建标准目录并授予权限:

1
2
3
sudo mkdir -p /var/cache/nginx /var/log/nginx /etc/nginx/conf.d /var/www/html
sudo chown -R nginx:nginx /var/cache/nginx /var/log/nginx
sudo chown -R root:root /etc/nginx

说明:这样可以让配置文件和日志更接近系统包管理器默认的布局。

7. 创建 systemd 服务

创建服务文件:

1
sudo vim /etc/systemd/system/nginx.service

写入以下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
WorkingDirectory=/etc/nginx
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -q -g 'daemon on; master_process on;'
ExecStart=/usr/sbin/nginx -g 'daemon on; master_process on;'
ExecReload=/usr/sbin/nginx -s reload
ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid
TimeoutStopSec=5
KillMode=mixed

[Install]
WantedBy=multi-user.target

重新加载 systemd:

1
sudo systemctl daemon-reload

8. 启动与验证

8.1 启动服务

1
sudo systemctl start nginx.service

8.2 查看状态

1
sudo systemctl status nginx.service

8.3 重启服务

1
sudo systemctl restart nginx.service

8.4 停止服务

1
sudo systemctl stop nginx.service

8.5 设置开机自启

1
sudo systemctl enable nginx.service

9. 验证是否可用

测试默认页面:

1
curl http://127.0.0.1/

如果配置正确,应该能够看到 NGINX 默认欢迎页面。

也可以检查监听端口:

1
ss -lntp | grep 80

10. 基础配置示例

NGINX 默认配置文件通常在:

1
/etc/nginx/nginx.conf

10.1 一个基础静态站点示例

推荐将站点根目录放在 /var/www/html

1
2
3
4
5
6
7
8
9
10
11
server {
listen 80;
server_name example.com;

root /var/www/html;
index index.html;

location / {
try_files $uri $uri/ =404;
}
}

10.2 一个简单的反向代理示例

1
2
3
4
5
6
7
8
9
10
11
server {
listen 80;
server_name app.example.com;

location / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

修改配置后,执行:

1
2
sudo /usr/sbin/nginx -t
sudo systemctl reload nginx.service

11. 常见问题与排查

1)启动失败

先检查配置语法:

1
sudo /usr/sbin/nginx -t

如果有错误,按提示修复配置文件,再重启服务。

2)端口被占用

检查是否已有其他服务占用了 80 或 443 端口:

1
ss -lntp | grep ':80\|:443'

3)无默认页面输出

确认:

  • 配置文件中 server 块是否正确
  • root 参数指向的目录是否存在
  • index 文件是否存在

4)配置文件改动后不生效

执行:

1
2
sudo /usr/sbin/nginx -t
sudo systemctl reload nginx.service

12. 进阶建议

  • 如果需要支持 HTTPS,建议配置 SSL 证书并在 server 块中开启 listen 443 ssl
  • 对生产环境建议启用 gziplimit_connlimit_req 等优化配置。
  • 可以将站点配置拆分到 conf.d/*.conf 中,便于维护。
  • 对于大流量场景,建议结合 upstreamkeepalive、负载均衡等配置进一步提升稳定性。

13. 总结

如果你的目标是让 Nginx 的目录布局尽量接近通过 apt 安装后的标准方式,那么建议使用下面这套路径:

  • 二进制:/usr/sbin/nginx
  • 配置:/etc/nginx/nginx.conf
  • 站点配置:/etc/nginx/conf.d/*.conf
  • 日志:/var/log/nginx/*.log
  • 运行状态:/run/nginx.pid
  • 静态站点目录:/var/www/html

这样做的好处是:

  1. 与系统默认安装方式保持一致
  2. 更容易维护和排查问题
  3. 更接近生产环境中常见的 Nginx 部署方式

如果你希望,我还可以继续帮你把这篇文档再细化成一份“适合直接复制执行的完整安装脚本版”。


NGINX 源码编译安装与基础使用
https://jgq12138.github.io/2023/06/19/linux_install_nginx/
作者
JGQ12138
发布于
2023年6月19日
许可协议