🚀 最新 Nginx 安装教程(2025版)

🚀 最新 Nginx 安装教程(2025版)

Nginx 是一款高性能的 HTTP 和反向代理服务器,以其高并发、低内存占用的特点被广泛使用。本教程涵盖主流 Linux 发行版的最新安装方法。

📋 目录

  • Ubuntu/Debian 安装
  • CentOS/RHEL 安装
  • 源码编译安装(最新版)
  • 基础配置与启动
  • 常见问题解决

1️⃣ Ubuntu/Debian 安装

使用官方源安装(推荐)

# 更新软件包列表
sudo apt update

# 安装依赖
sudo apt install -y curl gnupg2 ca-certificates lsb-release ubuntu-keyring

# 导入 Nginx 官方签名密钥
curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor     | sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null

# 添加官方源(稳定版)
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg]     http://nginx.org/packages/ubuntu $(lsb_release -cs) nginx"     | sudo tee /etc/apt/sources.list.d/nginx.list

# 更新并安装
sudo apt update
sudo apt install -y nginx

# 查看版本
nginx -v

2️⃣ CentOS/RHEL 安装

RHEL 9 / CentOS Stream 9

# 安装 EPEL 源
sudo dnf install -y epel-release

# 安装 Nginx
sudo dnf install -y nginx

# 开机自启并启动
sudo systemctl enable nginx
sudo systemctl start nginx

# 检查状态
sudo systemctl status nginx

3️⃣ 源码编译安装(最新版)

如需最新特性或自定义模块,建议源码编译:

# 安装编译依赖
sudo apt install -y build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev

# 下载最新源码(以 1.26.0 为例)
cd /usr/local/src
sudo wget https://nginx.org/download/nginx-1.26.0.tar.gz
sudo tar -zxvf nginx-1.26.0.tar.gz
cd nginx-1.26.0

# 配置编译参数
sudo ./configure     --prefix=/etc/nginx     --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=/var/run/nginx.pid     --lock-path=/var/run/nginx.lock     --with-http_ssl_module     --with-http_v2_module     --with-http_realip_module     --with-http_gzip_static_module

# 编译并安装
sudo make
sudo make install

# 创建 systemd 服务文件
sudo tee /etc/systemd/system/nginx.service > /dev/null << 'EOF'
[Unit]
Description=Nginx HTTP Server
After=network.target

[Service]
Type=forking
ExecStart=/usr/sbin/nginx
ExecReload=/usr/sbin/nginx -s reload
ExecStop=/usr/sbin/nginx -s stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF

sudo systemctl daemon-reload
sudo systemctl enable nginx

4️⃣ 基础配置

常用命令

# 启动/停止/重启
sudo systemctl start nginx
sudo systemctl stop nginx
sudo systemctl restart nginx

# 重载配置(不中断服务)
sudo nginx -s reload

# 测试配置语法
sudo nginx -t

# 查看进程
ps aux | grep nginx

基础配置文件示例

server {
    listen 80;
    server_name example.com;
    root /var/www/html;
    index index.html index.htm;

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

    # 反向代理示例
    location /api/ {
        proxy_pass http://localhost:3000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

5️⃣ 防火墙设置

# UFW (Ubuntu)
sudo ufw allow 'Nginx Full'
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp

# FirewallD (CentOS)
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload

6️⃣ 常见问题

问题 解决方案
80端口被占用 sudo fuser -k 80/tcp
权限拒绝 检查 /var/log/nginx/error.log
配置语法错误 sudo nginx -t 检查具体行号

💡 提示:安装完成后,在浏览器访问服务器 IP,看到 "Welcome to nginx!" 即表示安装成功!

发布时间:2025年2月 | 适用版本:Nginx 1.26+

发表回复

后才能评论