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;
        }
    }
}

相关推荐
SelectDB1 天前
Litefuse 开源并推出单进程轻量模式,25 秒就能跑起来的 Agent 可观测与评估平台
运维·后端·自动化运维
XIAOHEZIcode2 天前
Linux系统鼠标偏移常见原因以及修复方案
linux·运维·游戏
用户0328472220703 天前
如何搭建本地yum源(上)
运维
ping某4 天前
为什么 Nginx 明明监听了 80,转发后端时却用了 4xxxx 端口?
后端·nginx
大树886 天前
金刚石散热越强,管路越先见顶
大数据·运维·服务器·人工智能·ai
摇滚侠6 天前
Linux CentOS7 rpm 安装 MySQL 5.7
linux·运维·mysql
霸道流氓气质6 天前
领域驱动设计(DDD)在 Spring Boot 微服务中的实践指南
运维·spring boot·微服务
Inhand陈工6 天前
基于台达PLC与映翰通IG502的智慧水产养殖精准投喂与远程运维解决方案
运维·人工智能·物联网·阿里云·信息与通信
网络研究院6 天前
2026年网络安全
网络·安全·法律·法规·趋势·发展
酣大智6 天前
ARP代理--工作原理
运维·网络·arp·arp代理