270 条记录
准备
源码包:
/service/nginx/nginx-1.30.5.tar.gz
源码目录:
/service/nginx/src/nginx-1.30.5/
安装目录:
/service/nginx/nginx-1.30.5/
在 Debian 13 上除了基础编译工具,还需要 OpenSSL、PCRE2、zlib、GD、Perl 等开发库。
apt update && apt install -y \
build-essential \
ca-certificates \
libpcre2-dev \
zlib1g-dev \
libssl-dev \
libgd-dev \
libperl-dev \
perl
创建专用 nginx 用户
groupadd --system nginx 2>/dev/null || true
useradd --system \
--gid nginx \
--no-create-home \
--shell /usr/sbin/nologin \
nginx 2>/dev/null || true
清理之前的源码并解压源码
rm -rf /service/nginx/src/nginx-1.30.5
rm -rf /service/nginx/nginx-1.30.5
mkdir -p /service/nginx/src
tar -xzf /service/nginx/nginx-1.30.5.tar.gz \
-C /service/nginx/src
编译安装
cd /service/nginx/src/nginx-1.30.5
make clean 2>/dev/null || true
./configure \
--prefix=/service/nginx/nginx-1.30.5 \
--user=nginx \
--group=nginx \
--pid-path=/service/nginx/nginx-1.30.5/nginx.pid \
--with-compat \
--with-file-aio \
--with-threads \
--with-pcre \
--with-http_addition_module \
--with-http_auth_request_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_image_filter_module \
--with-http_mp4_module \
--with-http_perl_module \
--with-http_random_index_module \
--with-http_realip_module \
--with-http_secure_link_module \
--with-http_slice_module \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_sub_module \
--with-http_v2_module \
--with-stream \
--with-stream_ssl_module \
--with-stream_ssl_preread_module \
--with-cc-opt='-O2 -pipe -march=native -mtune=native -fstack-protector-strong -fPIC' \
--with-ld-opt='-Wl,-z,relro -Wl,-z,now'
make -j"$(nproc)"
make install
/service/nginx/nginx-1.30.5/sbin/nginx -V
/service/nginx/nginx-1.30.5/sbin/nginx -t
如果是简单的反向代理服务器,可以这样编译:
cd /service/nginx/src/nginx-1.30.5
./configure \
--prefix=/service/nginx/nginx-1.30.5 \
--user=nginx \
--group=nginx \
--pid-path=/service/nginx/nginx-1.30.5/nginx.pid \
--with-compat \
--with-file-aio \
--with-threads \
--with-pcre \
--with-http_auth_request_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_realip_module \
--with-http_secure_link_module \
--with-http_slice_module \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_v2_module \
--with-stream \
--with-stream_ssl_module \
--with-stream_ssl_preread_module \
--with-cc-opt='-O2 -pipe -march=native -mtune=native -fstack-protector-strong -fPIC' \
--with-ld-opt='-Wl,-z,relro -Wl,-z,now'
make -j"$(nproc)"
make install
正常的话目录应该是:
/service/nginx/
├── nginx-1.30.5.tar.gz
├── src/
│ └── nginx-1.30.5/
│ ├── auto/
│ ├── conf/
│ ├── src/
│ ├── objs/
│ └── ...
└── nginx-1.30.5/
├── conf/
│ └── nginx.conf
├── html/
├── logs/
└── sbin/
└── nginx
单元文件:
cat >/etc/systemd/system/nginx.service <<'EOF'
[Unit]
Description=NGINX HTTP and reverse proxy server
Documentation=https://nginx.org/en/docs/
After=network-online.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/service/nginx/nginx-1.30.5/nginx.pid
ExecStartPre=/service/nginx/nginx-1.30.5/sbin/nginx -t -c /service/nginx/nginx-1.30.5/conf/nginx.conf
ExecStart=/service/nginx/nginx-1.30.5/sbin/nginx -c /service/nginx/nginx-1.30.5/conf/nginx.conf
ExecReload=/service/nginx/nginx-1.30.5/sbin/nginx -s reload
ExecStop=/service/nginx/nginx-1.30.5/sbin/nginx -s quit
Restart=on-failure
RestartSec=3
LimitNOFILE=65535
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now nginx
systemctl status nginx
systemctl status nginx --no-pager -l
journalctl -u nginx -n 100 --no-pager
systemctl start nginx
systemctl stop nginx
systemctl restart nginx
systemctl reload nginx
systemctl status nginx