nginx部署教程

从截图来看,你的服务器是 x86_64 架构的 Linux,且 whereis nginx 显示配置文件在 /etc/nginx/,说明之前可能安装过 nginx 但二进制文件不在 PATH 中,或者已被卸载。

以下是针对你这台 x86_64 Linux 服务器的 Nginx 官方下载链接部署教程


一、官方下载链接

1. 源码包下载(最新稳定版)

bash 复制代码
wget https://nginx.org/download/nginx-1.30.0.tar.gz

这是截至目前的最新稳定版(Stable version),生产环境推荐使用稳定版。

2. 预编译包(推荐,通过官方仓库安装)

如果你使用的是 RHEL/CentOS/AlmaLinux/Rocky Linux 系列:

bash 复制代码
# 创建官方 nginx 仓库
sudo tee /etc/yum.repos.d/nginx.repo << 'EOF'
[nginx-stable]
name=nginx stable repo
baseurl=https://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=https://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
EOF

# 安装
sudo yum install nginx

如果你使用的是 Ubuntu/Debian 系列:

bash 复制代码
# 安装依赖
sudo apt update
sudo apt install curl gnupg2 ca-certificates lsb-release

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

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

# 设置仓库优先级
echo -e "Package: *\nPin: origin nginx.org\nPin: release o=nginx\nPin-Priority: 900\n" | sudo tee /etc/apt/preferences.d/99nginx

# 安装
sudo apt update
sudo apt install nginx

二、源码编译部署教程(最灵活的方式)

如果你的系统包管理器不可用,或者需要自定义模块,推荐源码编译安装:

步骤 1:安装编译依赖

RHEL/CentOS 系列:

bash 复制代码
sudo yum groupinstall "Development Tools"
sudo yum install pcre pcre-devel zlib zlib-devel openssl openssl-devel

Ubuntu/Debian 系列:

bash 复制代码
sudo apt update
sudo apt install build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev

步骤 2:下载并解压源码

bash 复制代码
cd /usr/local/src
wget https://nginx.org/download/nginx-1.30.0.tar.gz
tar -zxvf nginx-1.30.0.tar.gz
cd nginx-1.30.0

步骤 3:配置编译参数

bash 复制代码
./configure \
    --prefix=/usr/local/nginx \
    --with-http_ssl_module \
    --with-http_v2_module \
    --with-http_gzip_static_module \
    --with-http_stub_status_module \
    --with-pcre \
    --with-zlib \
    --with-openssl

常用模块说明:

  • --with-http_ssl_module:HTTPS 支持
  • --with-http_v2_module:HTTP/2 支持
  • --with-http_gzip_static_module:Gzip 静态压缩
  • --with-http_stub_status_module:Nginx 状态监控

如果报错:就找报错原因

bash 复制代码
cd /data/cm/nginx-1.30.0

./configure \
    --prefix=/usr/local/nginx \
    --with-http_ssl_module \
    --with-http_v2_module \
    --with-http_gzip_static_module \
    --with-http_stub_status_module \
    --with-pcre \
    --with-zlib \
    --with-openssl
bash 复制代码
cd /data/cm/nginx-1.30.0

./configure \
    --prefix=/usr/local/nginx \
    --with-http_ssl_module \
    --with-http_v2_module \
    --with-http_gzip_static_module \
    --with-http_stub_status_module \
    --with-pcre=/data/cm/pcre2-10.45 \
    --with-zlib=/data/cm/zlib-1.3.1 \
    --with-openssl=/data/cm/openssl-3.5.2

步骤 4:编译并安装

bash 复制代码
make -j$(nproc)
sudo make install

步骤 5:创建软链接(方便全局调用)

bash 复制代码
sudo ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/nginx

步骤 6:验证安装

bash 复制代码
nginx -v
# 输出类似:nginx version: nginx/1.30.0
bash 复制代码
# 启动 nginx
/usr/local/nginx/sbin/nginx

# 停止 nginx(快速停止)
/usr/local/nginx/sbin/nginx -s stop

# 优雅停止(处理完当前请求再停)
/usr/local/nginx/sbin/nginx -s quit

# 重载配置(修改配置后无需重启)
/usr/local/nginx/sbin/nginx -s reload

# 重新打开日志文件(日志切割时用)
/usr/local/nginx/sbin/nginx -s reopen



# 检查配置文件语法
/usr/local/nginx/sbin/nginx -t

# 启动
/usr/local/nginx/sbin/nginx

# 查看进程
ps -ef | grep nginx

# 测试 10015 端口
curl -I http://127.0.0.1:10015/

步骤 7:创建 systemd 服务文件

bash 复制代码
sudo tee /etc/systemd/system/nginx.service << 'EOF'
[Unit]
Description=nginx - high performance web server
After=network.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF

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

三、快速验证

bash 复制代码
# 检查 nginx 是否运行
curl -I 127.0.0.1

# 预期输出:
# HTTP/1.1 200 OK
# Server: nginx/1.30.0

四、重要文件路径

如果你按照之前的编译参数(--prefix=/usr/local/nginx)安装,配置文件在:

复制代码
/usr/local/nginx/conf/nginx.conf

相关重要路径一览

用途 路径
主配置文件 /usr/local/nginx/conf/nginx.conf
子配置文件目录 /usr/local/nginx/conf/
默认网站根目录 /usr/local/nginx/html/
访问日志 /usr/local/nginx/logs/access.log
错误日志 /usr/local/nginx/logs/error.log
PID 文件 /usr/local/nginx/logs/nginx.pid
二进制文件 /usr/local/nginx/sbin/nginx

快速查看/编辑配置文件

bash 复制代码
# 查看配置文件
cat /usr/local/nginx/conf/nginx.conf

# 编辑配置文件(用 vi 或 nano)
vi /usr/local/nginx/conf/nginx.conf

# 检查配置文件语法是否正确
nginx -t

# 重载配置(修改后无需重启)
nginx -s reload

如果你之前通过 yum/apt 安装过 nginx

如果系统里还残留着之前的 nginx 安装,配置文件可能在 /etc/nginx/nginx.conf。但按照你当前的编译安装路径,生效的是 /usr/local/nginx/conf/nginx.conf

nginx.conf 配置示例:

bash 复制代码
user  root;
worker_processes  auto;

error_log  /usr/local/nginx/logs/error.log warn;
pid        /usr/local/nginx/logs/nginx.pid;

events {
    worker_connections  1024;
}

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

    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

    upstream cm_backend {
        server 10.222.1.30:3000;
    }

    server {
        listen       10015;
        server_name  _;
        client_max_body_size 1024M;

        location /api/ws/doc-chat {
            proxy_pass http://cm_backend/api/ws/doc-chat;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_read_timeout 86400s;
            proxy_send_timeout 86400s;
        }

        location /api/ws/question/answer {
            proxy_pass http://cm_backend/api/ws/question/answer;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_read_timeout 86400s;
            proxy_send_timeout 86400s;
        }

        location /api/ {
            proxy_pass http://cm_backend;
            proxy_connect_timeout 60s;
            proxy_send_timeout 60s;
            proxy_read_timeout 600s;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
        }

        location / {
            root   /usr/local/nginx/html/smart;
            index  index.html index.htm;
            try_files $uri $uri/ /index.html;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

相关推荐
安全小白wula1 小时前
RCE远程代码/命令执行基础讲解
网络·网络安全·渗透测试·rce·web渗透
b***25111 小时前
电池组PACK自动化生产线:从电芯到成品的精密制造之路
运维·自动化·制造
云边云科技_云网融合1 小时前
AI 网关:企业 AI 时代的 “智能交通枢纽“—— 六大行业典型场景深度解析
大数据·运维·人工智能
24zhgjx-fuhao1 小时前
ISIS:单区域集成ISIS
网络·智能路由器
明夜之约2 小时前
Linux 系统命令使用速查手册
linux·运维
不爱洗脚的小滕2 小时前
【RAG】Milvus 混合检索参数调优:ef / candidate_k / final_k 详解
网络·langchain·milvus·rag
武子康2 小时前
调查研究-153 Cloudflare 能部署网站吗?2026 年完整对比 Vercel / Netlify / 自建服务器
大数据·运维·服务器·人工智能·部署·devops·opc
夜月yeyue2 小时前
KCP 与 UDP 可靠传输
linux·网络·单片机·网络协议·udp·php
一个向上的运维者2 小时前
Docker 自定义网络中容器无法通过宿主机 IP 访问服务的完整排障记录
网络·tcp/ip·docker