igozhang

——

    Ubuntu 20.04 编译部署 Nginx 1.18.0 手册

    适用系统:Ubuntu 20.04 LTS(x86_64)

    1. 环境准备

    sudo apt update
    sudo apt upgrade -y
    

    确认系统版本:

    lsb_release -a
    uname -m
    

    2. 安装编译依赖

    Nginx 源码编译所需依赖(含 SSL、PCRE、zlib):

    sudo apt install -y \
      build-essential \
      libpcre3 \
      libpcre3-dev \
      zlib1g \
      zlib1g-dev \
      libssl-dev \
      libgd-dev \
      libxml2 \
      libxml2-dev \
      libxslt1-dev \
      libgeoip-dev \
      wget \
      tar \
      curl
    

    验证依赖是否安装成功:

    dpkg -l | grep -E 'build-essential|libpcre3-dev|zlib1g-dev|libssl-dev'
    gcc --version
    

    3. 下载 Nginx 1.18.0 源码

    官方下载地址:

    文件 地址
    源码包 https://nginx.org/download/nginx-1.18.0.tar.gz
    备用 HTTP http://nginx.org/download/nginx-1.18.0.tar.gz
    PGP 签名 https://nginx.org/download/nginx-1.18.0.tar.gz.asc
    export NGINX_VERSION=1.18.0
    export NGINX_TAR=nginx-${NGINX_VERSION}.tar.gz
    export NGINX_URL=https://nginx.org/download/${NGINX_TAR}
    
    cd /usr/local/src
    sudo wget -c ${NGINX_URL} -O ${NGINX_TAR}
    

    可选:校验 SHA256(官方发布页可查):

    # 官方 SHA256 可在 https://nginx.org/en/download.html 对应版本处查看
    sha256sum ${NGINX_TAR}
    

    解压:

    sudo tar -zxvf ${NGINX_TAR}
    cd nginx-${NGINX_VERSION}
    

    4. 编译安装

    4.1 创建运行用户

    sudo groupadd -r nginx 2>/dev/null || true
    sudo useradd -r -g nginx -s /sbin/nologin -d /var/cache/nginx nginx 2>/dev/null || true
    

    4.2 配置编译参数

    sudo ./configure \
      --prefix=/usr/local/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 \
      --lock-path=/var/run/nginx.lock \
      --http-client-body-temp-path=/var/cache/nginx/client_temp \
      --http-proxy-temp-path=/var/cache/nginx/proxy_temp \
      --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
      --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
      --http-scgi-temp-path=/var/cache/nginx/scgi_temp \
      --user=nginx \
      --group=nginx \
      --with-http_ssl_module \
      --with-http_v2_module \
      --with-http_realip_module \
      --with-http_gzip_static_module \
      --with-http_stub_status_module \
      --with-file-aio \
      --with-threads \
      --with-stream \
      --with-stream_ssl_module
    

    ./configure 报错缺少某库,按提示补装对应 -dev 包后重新执行。

    4.3 编译并安装

    sudo make -j$(nproc)
    sudo make install
    

    5. 创建目录与 systemd 服务

    sudo mkdir -p /var/log/nginx /var/cache/nginx
    sudo chown -R nginx:nginx /var/log/nginx /var/cache/nginx
    

    创建 systemd 单元:

    sudo tee /etc/systemd/system/nginx.service > /dev/null <<'EOF'
    [Unit]
    Description=Nginx HTTP Server
    After=network-online.target
    Wants=network-online.target
    
    [Service]
    Type=forking
    PIDFile=/var/run/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
    EOF
    
    sudo systemctl daemon-reload
    sudo systemctl enable nginx
    sudo systemctl start nginx
    

    添加命令到 PATH(可选):

    echo 'export PATH=/usr/local/nginx/sbin:$PATH' | sudo tee /etc/profile.d/nginx.sh
    source /etc/profile.d/nginx.sh
    

    6. 验证

    6.1 版本与配置

    /usr/local/nginx/sbin/nginx -v
    /usr/local/nginx/sbin/nginx -V
    /usr/local/nginx/sbin/nginx -t
    

    预期输出示例:

    nginx version: nginx/1.18.0
    nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
    

    6.2 服务状态

    sudo systemctl status nginx
    ss -lntp | grep nginx
    

    6.3 HTTP 访问测试

    默认监听 80 端口,本机访问:

    curl -I http://127.0.0.1
    

    预期包含:

    HTTP/1.1 200 OK
    Server: nginx/1.18.0
    

    浏览器访问:http://<服务器IP>/,应看到 Nginx 默认欢迎页。

    6.4 开机自启验证

    sudo systemctl is-enabled nginx
    

    预期输出:enabled

    7. 常用运维命令

    # 重载配置(不中断服务)
    sudo /usr/local/nginx/sbin/nginx -s reload
    
    # 停止 / 启动
    sudo systemctl stop nginx
    sudo systemctl start nginx
    
    # 查看错误日志
    sudo tail -f /var/log/nginx/error.log
    
    # 查看访问日志
    sudo tail -f /var/log/nginx/access.log
    

    8. 卸载(如需)

    sudo systemctl stop nginx
    sudo systemctl disable nginx
    sudo rm -f /etc/systemd/system/nginx.service
    sudo systemctl daemon-reload
    sudo rm -rf /usr/local/nginx
    sudo rm -rf /var/log/nginx /var/cache/nginx
    

    9. 常见问题

    问题 处理
    80 端口被占用 sudo ss -lntp | grep :80,停止冲突服务或修改 nginx.conf 监听端口
    configure: error: SSL modules require the OpenSSL library sudo apt install -y libssl-dev
    configure: error: the HTTP rewrite module requires the PCRE library sudo apt install -y libpcre3 libpcre3-dev
    启动报权限错误 检查 /var/log/nginx/var/cache/nginx 目录属主是否为 nginx

    MP3