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

相关推荐
hbugs0019 小时前
PNETLab vs EVE-NG Pro 流量洞察功能底层架构技术白皮书
网络·架构·eve-ng·bpf·流量洞察
RisunJan9 小时前
Linux命令-route(查看/修改 IP 路由表)
linux·运维
不会C语言的男孩10 小时前
TCP通信可视化工具,含下载地址
服务器·网络·安全
五五六六052410 小时前
2026盘古石杯初赛
网络
玖玥拾10 小时前
C# 语言进阶(十三)网络 DLL 库分层设计
服务器·开发语言·网络·网络协议·c#
大师兄666810 小时前
HarmonyOS7 HTTP 网络请求封装:统一拦截器实战
网络·网络协议·http·arkui·harmonyos7·实战讲解
qetfw10 小时前
CentOS 7 搭建 DNS 主从服务
linux·运维·centos·dns·bind
hbugs00111 小时前
BPF 表达式检查器:一款免费开源的 Wireshark 捕获过滤器语法检查与可视化构建工具
网络·测试工具·开源·eve-ng·bpf
韩曙亮11 小时前
【Flutter】iOS 网络权限设置 ② ( 配置 iOS 平台可使用 HTTP 访问 | ATS 网络安全强制规范 )
网络·flutter·http·ios·https·网络权限·ats
柠石榴11 小时前
远程连接校内服务器 ssh
运维·服务器·ssh