欢迎光临中国护送网
详情描述

Ubuntu 24.04 LTS 中 Nginx 安装、配置与卸载指南

一、安装 Nginx

方案1:安装稳定版(推荐用于生产环境)
# 1. 更新包列表
sudo apt update

# 2. 安装 Nginx 稳定版(来自 Ubuntu 官方仓库)
sudo apt install nginx -y

# 3. 验证安装
nginx -v
# 应显示类似:nginx version: nginx/1.24.0
方案2:安装主线版(最新特性)
# 1. 添加 Nginx 官方仓库
sudo apt install curl gnupg2 ca-certificates lsb-release ubuntu-keyring -y

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

# 3. 添加主线版仓库
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] http://nginx.org/packages/mainline/ubuntu $(lsb_release -cs) nginx" | sudo tee /etc/apt/sources.list.d/nginx.list

# 4. 设置仓库优先级(避免与系统仓库冲突)
echo -e "Package: *\nPin: origin nginx.org\nPin-Priority: 900" | sudo tee /etc/apt/preferences.d/99nginx

# 5. 安装 Nginx 主线版
sudo apt update
sudo apt install nginx -y

# 6. 验证版本
nginx -v
# 主线版版本号更高,如:nginx version: nginx/1.25.4
方案3:从源码编译安装(完全自定义)
# 1. 安装编译依赖
sudo apt update
sudo apt install build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev -y

# 2. 下载源码(替换为所需版本)
cd /tmp
wget https://nginx.org/download/nginx-1.24.0.tar.gz
tar -zxvf nginx-1.24.0.tar.gz
cd nginx-1.24.0

# 3. 配置编译选项
./configure \
    --prefix=/usr/local/nginx \
    --sbin-path=/usr/sbin/nginx \
    --conf-path=/etc/nginx/nginx.conf \
    --pid-path=/var/run/nginx.pid \
    --http-log-path=/var/log/nginx/access.log \
    --error-log-path=/var/log/nginx/error.log \
    --with-pcre \
    --with-http_ssl_module \
    --with-http_v2_module \
    --with-http_stub_status_module

# 4. 编译安装
make
sudo make install

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

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/usr/sbin/nginx -s reload
ExecStop=/usr/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF

# 6. 启动服务
sudo systemctl daemon-reload
sudo systemctl enable nginx
sudo systemctl start nginx

二、基本配置与管理

1. 服务管理命令
# 启动服务
sudo systemctl start nginx

# 停止服务
sudo systemctl stop nginx

# 重启服务
sudo systemctl restart nginx

# 重新加载配置(不中断服务)
sudo systemctl reload nginx

# 检查状态
sudo systemctl status nginx

# 开机自启
sudo systemctl enable nginx

# 禁用开机自启
sudo systemctl disable nginx
2. 配置文件结构
/etc/nginx/
├── nginx.conf              # 主配置文件
├── conf.d/                 # 额外配置目录
├── sites-available/        # 可用站点配置
├── sites-enabled/          # 启用的站点(符号链接)
├── modules-available/      # 模块配置
├── modules-enabled/         # 启用的模块
└── snippets/               # 可复用配置片段
3. 创建虚拟主机(站点)
# 1. 创建站点配置文件
sudo nano /etc/nginx/sites-available/example.com

# 添加以下内容:
server {
    listen 80;
    listen [::]:80;

    server_name example.com www.example.com;
    root /var/www/example.com/html;
    index index.html index.htm;

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

    access_log /var/log/nginx/example.com.access.log;
    error_log /var/log/nginx/example.com.error.log;
}

# 2. 创建网站目录
sudo mkdir -p /var/www/example.com/html
sudo chown -R www-data:www-data /var/www/example.com
sudo chmod -R 755 /var/www

# 3. 创建测试页面
echo "<h1>Welcome to example.com</h1>" | sudo tee /var/www/example.com/html/index.html

# 4. 启用站点(创建符号链接)
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

# 5. 测试配置语法
sudo nginx -t

# 6. 重新加载配置
sudo systemctl reload nginx
4. 启用 HTTPS(使用 Let's Encrypt)
# 1. 安装 Certbot
sudo apt install certbot python3-certbot-nginx -y

# 2. 获取 SSL 证书
sudo certbot --nginx -d example.com -d www.example.com

# 3. 自动续期测试
sudo certbot renew --dry-run
5. 性能调优示例
# 编辑主配置文件
sudo nano /etc/nginx/nginx.conf

# 在 http 块中添加/修改:
http {
    # 基础优化
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    client_max_body_size 100M;

    # 开启 Gzip 压缩
    gzip on;
    gzip_vary on;
    gzip_min_length 1024;
    gzip_types text/plain text/css application/json application/javascript;

    # 连接限制
    limit_conn_zone $binary_remote_addr zone=addr:10m;
    limit_req_zone $binary_remote_addr zone=one:10m rate=10r/s;
}

三、完全卸载 Nginx

方案1:卸载 apt 安装的 Nginx
# 1. 停止服务
sudo systemctl stop nginx
sudo systemctl disable nginx

# 2. 卸载软件包
sudo apt remove --purge nginx nginx-common nginx-core -y

# 3. 移除配置文件和数据
sudo rm -rf /etc/nginx
sudo rm -rf /var/www/html
sudo rm -rf /var/log/nginx

# 4. 清理依赖
sudo apt autoremove -y
sudo apt autoclean

# 5. 删除仓库配置(如果使用官方仓库)
sudo rm -f /etc/apt/sources.list.d/nginx.list
sudo rm -f /usr/share/keyrings/nginx-archive-keyring.gpg
sudo rm -f /etc/apt/preferences.d/99nginx
方案2:卸载源码编译的 Nginx
# 1. 停止服务
sudo systemctl stop nginx
sudo systemctl disable nginx

# 2. 移除服务文件
sudo rm -f /etc/systemd/system/nginx.service
sudo systemctl daemon-reload

# 3. 删除安装文件
sudo rm -f /usr/sbin/nginx
sudo rm -rf /usr/local/nginx

# 4. 删除配置文件
sudo rm -rf /etc/nginx
sudo rm -rf /var/log/nginx

# 5. 删除 PID 文件
sudo rm -f /var/run/nginx.pid
方案3:彻底检查并清除残留
# 查找所有 nginx 相关文件
sudo find / -name "*nginx*" 2>/dev/null

# 检查是否还有进程运行
ps aux | grep nginx

# 强制结束所有 nginx 进程
sudo pkill -9 nginx

# 检查端口占用
sudo netstat -tulpn | grep :80
sudo netstat -tulpn | grep :443

# 删除用户和组(如果存在)
sudo deluser nginx 2>/dev/null
sudo delgroup nginx 2>/dev/null

四、故障排除常用命令

# 检查配置语法
sudo nginx -t

# 查看详细错误
sudo journalctl -u nginx -f

# 查看访问日志
sudo tail -f /var/log/nginx/access.log

# 查看错误日志
sudo tail -f /var/log/nginx/error.log

# 检查监听的端口
sudo ss -tulpn | grep nginx

# 测试网站响应
curl -I http://localhost

五、最佳实践建议

安全建议

  • 定期更新:sudo apt update && sudo apt upgrade nginx
  • 使用防火墙:sudo ufw allow 'Nginx Full'
  • 禁用服务器令牌:在配置中添加 server_tokens off;
  • 限制访问:配置适当的 allow/deny 规则

备份配置

# 备份配置
sudo tar -czf nginx_backup_$(date +%Y%m%d).tar.gz /etc/nginx

# 恢复配置
sudo tar -xzf nginx_backup_YYYYMMDD.tar.gz -C /

监控工具

# 安装状态模块后,添加配置:
location /nginx_status {
    stub_status on;
    access_log off;
    allow 127.0.0.1;
    deny all;
}

选择安装方式时:

  • 生产环境:使用稳定版(方案1)
  • 需要最新功能:使用主线版(方案2)
  • 特定需求定制:使用源码编译(方案3)

按照以上步骤,您可以在 Ubuntu 24.04 上顺利完成 Nginx 的安装、配置和管理。