hoochrel

Debian13源码安装nginx

原创  linux 2026年09月25日 21:33:26 hoochrel 0评论 10

源码安装 Nginx 的核心优势是可以自定义模块和安装路径,适合需要特定功能(比如第三方模块)的场景。以下是 Debian 13 上的完整步骤。

📦 第一步:安装编译依赖

编译 Nginx 需要编译器、PCRE(正则)、zlib(压缩)和 OpenSSL(HTTPS)的开发库。

bash

sudo apt update
sudo apt install -y build-essential libpcre2-dev zlib1g-dev libssl-dev wget

📥 第二步:下载并解压源码

建议从官网下载稳定版(偶数版本号),例如 1.28.2。你也可以在 nginx.org/download 查看最新版本。

bash

cd /usr/local/src
sudo wget  
sudo tar -zxvf nginx-1.28.2.tar.gzcd nginx-1.28.2

⚙️ 第三步:配置编译参数

这是最关键的一步,./configure 决定了 Nginx 最终具备哪些能力。

bash

./configure \
 --prefix=/usr/local/nginx \
 --user=nginx \
 --group=nginx \
 --sbin-path=/usr/local/nginx/sbin/nginx \
 --conf-path=/usr/local/nginx/conf/nginx.conf \
 --error-log-path=/var/log/nginx/error.log \
 --http-log-path=/var/log/nginx/access.log \
 --pid-path=/var/run/nginx.pid \
 --with-http_ssl_module \
 --with-http_gzip_static_module \
 --with-http_stub_status_module \
 --with-http_v2_module \
 --with-http_v3_module

参数说明:

  • --prefix:指定安装根目录,所有文件默认放在这里。

  • --user/--group:指定工作进程运行的用户,需要提前创建(见下方提示)。

  • --with-http_ssl_module:启用 HTTPS 支持,生产环境必备。

  • --with-http_v2_module:启用 HTTP/2 支持。

  • --with-http_stub_status_module:启用状态页,方便监控。

注意:如果 nginx 用户不存在,先创建它:

bash

sudo useradd -r -s /sbin/nologin nginx

🏗️ 第四步:编译并安装

bash

# 使用多核加速编译(nproc 会返回 CPU 核心数)
make -j$(nproc)
# 安装
sudo make install

✅ 第五步:验证并启动

bash

# 检查配置语法
/usr/local/nginx/sbin/nginx -t
# 启动 Nginx
sudo /usr/local/nginx/sbin/nginx

启动后,在浏览器访问服务器 IP,如果看到 “Welcome to nginx!” 页面就说明成功了。你也可以用 curl http://localhost 测试。

🔧 第六步:配置为 Systemd 服务(推荐)

源码安装不会自动创建服务文件,为了能用 systemctl 管理并开机自启,需要手动创建。

bash

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

填入以下内容:

ini

[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

保存后启用服务:

bash

sudo systemctl daemon-reload
sudo systemctl enable nginx
sudo systemctl start nginx

💡 后续管理

  • 配置文件位置:/usr/local/nginx/conf/nginx.conf。

  • 重载配置:修改配置后,务必先执行 nginx -t 检查语法,无误后再执行 sudo systemctl reload nginx。直接 restart 有短暂断连风险。

  • 端口冲突:如果启动失败并提示 Address already in use,通常是 Apache 或系统自带的 Nginx 占用了 80 端口,可以用 sudo ss -tlnp | grep ':80' 排查。


评论区

face
😀 😃 😄 😁 😆 😅 🤣 😂 🙂 🙃 😉 😊 😇 🥰 😍 🤩 😘 😗 😙 😋 😛 😜 🤪 😝 🤑 🤗 🤭 🤫 🤔 🤐 🤨 😐 😑 😶 😏 😒 🙄 😬 🤥 😌 😔 😪 🤤 😴 😷 🤒 🤕 🤢 🤮 🤧 🥵 🥶 🥴 😵 🤯 🤠 🥳 😎 🤓 🧐 😕 😟 🙁 ☹️ 😮 😯 😲 😳 🥺 😦 😧 😨 😰 😥 😢 😭 😱 😖 😣 😞 😓 😩 😫 😤 😡 😠 🤬 😈 👿 💀 ☠️ ☠ 💩 🤡 👹 👺 👻 👽 👾 🤖 😺 😸 😹 😻 😼 😽 🙀 😿 😾 🙈 🙉 🙊 💋 💌 💘 💝 💖 💗 💓 💞 💕 💟 ❣️ ❣ 💔 ❤️ ❤ 🧡 💛 💚 💙 💜 🖤 💯 💢 💥 💫 💦 💨 🕳️ 🕳 💣 💬 👁️‍🗨️ 👁‍🗨️ 👁️‍🗨 👁‍🗨 🗨️ 🗨 🗯️ 🗯 💭 💤 👋
笑脸icon


选择主题