Ubuntu 20.04 安装 Nginx 详细操作文档

Ubuntu 20.04 安装 Nginx 详细操作文档

环境信息

  • 系统:Ubuntu 20.04.6 LTS (Focal Fossa)
  • 目标:安装 nginx 并支持 HTTP/HTTPS
  • 现有 nginx 路径:/usr/local/nginx
  • 要求:保留原有 nginx.conf 配置文件

目录

  • 前置准备
  • [方案一:OpenSSL 3.x + Nginx 1.26.2(推荐)](#方案一:OpenSSL 3.x + Nginx 1.26.2(推荐))
  • [方案二:Nginx 1.24.0(兼容系统 OpenSSL)](#方案二:Nginx 1.24.0(兼容系统 OpenSSL))
  • [SSL/HTTPS 配置](#SSL/HTTPS 配置)
  • [Systemd 服务配置](#Systemd 服务配置)
  • 常用命令
  • 故障排除

前置准备

1. 备份现有配置

bash 复制代码
# 创建备份目录
sudo mkdir -p /backup/nginx_$(date +%Y%m%d)

# 备份现有 nginx 配置文件
sudo cp -r /usr/local/nginx/conf /backup/nginx_$(date +%Y%m%d)/

# 备份 SSL 证书(如果有)
sudo cp -r /usr/local/nginx/ssl /backup/nginx_$(date +%Y%m%d)/ 2>/dev/null || echo "无SSL目录"

# 确认备份成功
ls -la /backup/nginx_$(date +%Y%m%d)/

2. 停止现有 nginx 服务

bash 复制代码
# 查看 nginx 进程
ps aux | grep nginx

# 优雅停止 nginx
sudo /usr/local/nginx/sbin/nginx -s quit

# 如果无法正常停止,强制停止
sudo pkill -9 nginx

# 确认 nginx 已停止
ps aux | grep nginx

3. 安装编译依赖

bash 复制代码
# 更新包列表
sudo apt update

# 安装编译所需依赖
sudo apt install -y build-essential \
    libpcre3 libpcre3-dev \
    zlib1g zlib1g-dev \
    libssl-dev \
    libgd-dev \
    libgeoip-dev \
    wget

方案一:OpenSSL 3.x + Nginx 1.26.2(推荐)

说明:Nginx 1.26.2 需要 OpenSSL 3.0+ 版本,Ubuntu 20.04 自带 OpenSSL 1.1.1,需要先编译安装 OpenSSL 3.x

步骤 1:下载并编译 OpenSSL 3.0

bash 复制代码
cd /tmp

# 下载 OpenSSL 3.0.14
# 主地址
wget https://www.openssl.org/source/openssl-3.0.14.tar.gz

# 备用地址(如果上面下载慢)
# wget https://mirrors.huaweicloud.com/openssl/openssl-3.0.14/openssl-3.0.14.tar.gz

# 解压
tar -zxvf openssl-3.0.14.tar.gz
cd openssl-3.0.14

# 配置(安装到 /usr/local/openssl3)
./config --prefix=/usr/local/openssl3 --openssldir=/usr/local/openssl3 shared zlib

# 编译(耗时约 3-5 分钟)
make -j$(nproc)

# 安装
sudo make install

步骤 2:下载 Nginx 1.26.2 源码

bash 复制代码
cd /tmp

# 下载 nginx 1.26.2 源码
wget https://nginx.org/download/nginx-1.26.2.tar.gz

# 解压
tar -zxvf nginx-1.26.2.tar.gz

# 进入源码目录
cd nginx-1.26.2

步骤 3:配置编译参数

bash 复制代码
./configure \
    --prefix=/usr/local/nginx \
    --sbin-path=/usr/local/nginx/sbin/nginx \
    --conf-path=/usr/local/nginx/conf/nginx.conf \
    --pid-path=/usr/local/nginx/logs/nginx.pid \
    --lock-path=/usr/local/nginx/logs/nginx.lock \
    --error-log-path=/usr/local/nginx/logs/error.log \
    --http-log-path=/usr/local/nginx/logs/access.log \
    --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_random_index_module \
    --with-http_secure_link_module \
    --with-http_stub_status_module \
    --with-http_auth_request_module \
    --with-http_slice_module \
    --with-stream \
    --with-stream_ssl_module \
    --with-stream_realip_module \
    --with-stream_ssl_preread_module \
    --with-threads \
    --with-file-aio \
    --with-openssl=/tmp/openssl-3.0.14

步骤 4:编译安装

bash 复制代码
# 编译
make -j$(nproc)

# 备份旧的 nginx 二进制文件(安装时会自动执行)
# sudo mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old

# 安装(不会覆盖已存在的 nginx.conf)
sudo make install

步骤 5:验证安装

bash 复制代码
# 查看版本
/usr/local/nginx/sbin/nginx -v
# 输出:nginx version: nginx/1.26.2

# 查看编译信息(确认包含 OpenSSL 3.0)
/usr/local/nginx/sbin/nginx -V

# 测试配置文件
sudo /usr/local/nginx/sbin/nginx -t

方案二:Nginx 1.24.0(兼容系统 OpenSSL)

说明:如果不想编译 OpenSSL,可以使用 Nginx 1.24.0,它兼容 Ubuntu 20.04 自带的 OpenSSL 1.1.1

步骤 1:下载 Nginx 1.24.0 源码

bash 复制代码
cd /tmp

# 下载 nginx 1.24.0
wget https://nginx.org/download/nginx-1.24.0.tar.gz

# 解压
tar -zxvf nginx-1.24.0.tar.gz

# 进入源码目录
cd nginx-1.24.0

步骤 2:配置编译参数

bash 复制代码
./configure \
    --prefix=/usr/local/nginx \
    --sbin-path=/usr/local/nginx/sbin/nginx \
    --conf-path=/usr/local/nginx/conf/nginx.conf \
    --pid-path=/usr/local/nginx/logs/nginx.pid \
    --lock-path=/usr/local/nginx/logs/nginx.lock \
    --error-log-path=/usr/local/nginx/logs/error.log \
    --http-log-path=/usr/local/nginx/logs/access.log \
    --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_random_index_module \
    --with-http_secure_link_module \
    --with-http_stub_status_module \
    --with-http_auth_request_module \
    --with-http_slice_module \
    --with-stream \
    --with-stream_ssl_module \
    --with-stream_realip_module \
    --with-stream_ssl_preread_module \
    --with-threads \
    --with-file-aio

步骤 3:编译安装

bash 复制代码
# 编译
make -j$(nproc)

# 安装
sudo make install

步骤 4:验证安装

bash 复制代码
# 查看版本
/usr/local/nginx/sbin/nginx -v
# 输出:nginx version: nginx/1.24.0

# 测试配置文件
sudo /usr/local/nginx/sbin/nginx -t

SSL/HTTPS 配置

1. 创建 SSL 证书目录

bash 复制代码
sudo mkdir -p /usr/local/nginx/ssl
sudo chmod 700 /usr/local/nginx/ssl

2. 生成自签名证书(测试环境)

bash 复制代码
sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
    -keyout /usr/local/nginx/ssl/server.key \
    -out /usr/local/nginx/ssl/server.crt \
    -subj "/C=CN/ST=Beijing/L=Beijing/O=Company/OU=IT/CN=example.com"

# 设置证书权限
sudo chmod 600 /usr/local/nginx/ssl/server.key
sudo chmod 644 /usr/local/nginx/ssl/server.crt

3. 使用正式 SSL 证书

bash 复制代码
# 复制你的证书文件
sudo cp your_certificate.crt /usr/local/nginx/ssl/server.crt
sudo cp your_private.key /usr/local/nginx/ssl/server.key

# 如果有证书链,需要合并
# cat your_certificate.crt intermediate.crt > /usr/local/nginx/ssl/server.crt

# 设置权限
sudo chmod 600 /usr/local/nginx/ssl/server.key
sudo chmod 644 /usr/local/nginx/ssl/server.crt

4. nginx.conf 配置示例

nginx 复制代码
worker_processes  auto;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    # HTTP 服务器 - 80 端口
    server {
        listen       80;
        server_name  example.com www.example.com;

        # 可选:重定向到 HTTPS
        # return 301 https://$server_name$request_uri;

        root   html;
        index  index.html index.htm;

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

    # HTTPS 服务器 - 443 端口
    server {
        listen       443 ssl http2;
        server_name  example.com www.example.com;

        # SSL 证书配置
        ssl_certificate      /usr/local/nginx/ssl/server.crt;
        ssl_certificate_key  /usr/local/nginx/ssl/server.key;

        # SSL 安全配置
        ssl_session_timeout  5m;
        ssl_session_cache    shared:SSL:10m;
        ssl_protocols        TLSv1.2 TLSv1.3;
        ssl_ciphers          ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;
        ssl_prefer_server_ciphers on;

        # HSTS(可选)
        # add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;

        root   html;
        index  index.html index.htm;

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

Systemd 服务配置

1. 创建服务文件

bash 复制代码
sudo tee /etc/systemd/system/nginx.service << 'EOF'
[Unit]
Description=The NGINX HTTP and reverse proxy server
Documentation=http://nginx.org/en/docs/
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.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=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF

2. 启用服务

bash 复制代码
# 重载 systemd 配置
sudo systemctl daemon-reload

# 设置开机自启
sudo systemctl enable nginx

# 启动 nginx
sudo systemctl start nginx

# 查看状态
sudo systemctl status nginx

常用命令

操作 命令
启动 nginx sudo systemctl start nginxsudo /usr/local/nginx/sbin/nginx
停止 nginx sudo systemctl stop nginxsudo /usr/local/nginx/sbin/nginx -s stop
重载配置 sudo systemctl reload nginxsudo /usr/local/nginx/sbin/nginx -s reload
测试配置 sudo /usr/local/nginx/sbin/nginx -t
查看状态 sudo systemctl status nginx
查看版本 /usr/local/nginx/sbin/nginx -v
查看编译信息 /usr/local/nginx/sbin/nginx -V
查看错误日志 tail -f /usr/local/nginx/logs/error.log
查看访问日志 tail -f /usr/local/nginx/logs/access.log

防火墙配置

bash 复制代码
# UFW 防火墙
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw reload

# 或 iptables
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT

清理临时文件

bash 复制代码
# 安装完成后清理(确认 nginx 运行正常后执行)
rm -rf /tmp/nginx-1.26.2*
rm -rf /tmp/nginx-1.24.0*
rm -rf /tmp/openssl-3.0.14*

# 删除旧的 nginx 二进制文件
sudo rm -f /usr/local/nginx/sbin/nginx.old

故障排除

问题 解决方案
undefined reference to SSL_xxx OpenSSL 版本太低,需要安装 OpenSSL 3.x(方案一)或使用 nginx 1.24.0(方案二)
端口被占用 sudo lsof -i :80sudo lsof -i :443 查看占用进程
配置文件语法错误 sudo /usr/local/nginx/sbin/nginx -t 查看具体错误
SSL 证书问题 检查证书路径和权限,key 文件权限应为 600
400 Bad Request 用 HTTP 访问了 HTTPS 端口,检查访问地址是否正确
权限不足 确保 nginx 以 root 启动或配置正确的 user

方案对比

对比项 方案一(nginx 1.26.2) 方案二(nginx 1.24.0)
nginx 版本 1.26.2(最新) 1.24.0(稳定)
OpenSSL 要求 需要 3.0+(需额外编译) 兼容系统自带 1.1.1
安装难度 稍复杂 简单
编译时间 较长(含 OpenSSL) 较短
新特性支持 更多 够用
推荐场景 需要最新特性 追求稳定省事
相关推荐
感谢地心引力1 小时前
【Chrome-Edge-Firefox】浏览器插件开发
前端·chrome·edge·firefox
Cincoze-Johnny1 小时前
Linux系统-应用问题全面剖析Ⅳ:德承工控机MD-3000在Ubuntu操作系统下[TPM功能]设置教程
linux·运维·ubuntu
qq_296544651 小时前
安卓版Google(谷歌地球),安卓谷歌(Google)地图,谷歌翻译,谷歌(Chrome)浏览器,手机版Edge,浏览器等安卓版浏览器下载
前端·chrome
红袜子i1 小时前
解决 Ubuntu 中 apt-get update 因架构配置混乱导致的更新失败问题
linux·ubuntu·架构
HIT_Weston1 小时前
50、【Ubuntu】【Gitlab】拉出内网 Web 服务:http.server 单/多线程分析(二)
前端·ubuntu·gitlab
zengshitang5202 小时前
ACRN 实战应用:在一台电脑上同时安装Windows10、Ubuntu22.04、Ubuntu PREEMPT_RT实时系统并流畅运行
linux·运维·ubuntu
kong79069282 小时前
Nginx反向代理和负载均衡
nginx·负载均衡·反向代理
vortex52 小时前
Ubuntu 虚拟机配置静态 IP
linux·tcp/ip·ubuntu
p***32352 小时前
Nginx 配置前端后端服务
运维·前端·nginx