阿里云轻量服务器安装nginx(不期而遇)

1. 安装所有编译依赖(一次性装全,避免报错)

bash 复制代码
yum install -y gcc gcc-c++ make wget pcre-devel zlib-devel openssl-devel

2. 下载最新稳定版 Nginx 源码(1.26.0 是当前最新稳定版)

bash 复制代码
cd /tmp && wget http://nginx.org/download/nginx-1.26.0.tar.gz -O nginx.tar.gz

3. 解压源码包

bash 复制代码
tar zxf nginx.tar.gz && cd nginx-1.26.0

4. 配置编译参数(启用 SSL,安装到 /usr/local/nginx)

bash 复制代码
./configure --prefix=/usr/local/nginx --with-http_ssl_module

5. 编译并安装(-j 后跟CPU核心数,加速编译,nproc自动识别)

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

6. 创建系统服务(让 Nginx 能被 systemctl 管理)

bash 复制代码
cat > /usr/lib/systemd/system/nginx.service << 'EOF'
[Unit]
Description=Nginx High Performance Web Server
After=network.target remote-fs.target nss-lookup.target

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

[Install]
WantedBy=multi-user.target
EOF

7. 重新加载系统服务配置,启动 Nginx 并设置开机自启

bash 复制代码
systemctl daemon-reload
systemctl start nginx
systemctl enable nginx

8. 验证安装结果

echo "Nginx 版本:"

bash 复制代码
/usr/local/nginx/sbin/nginx -v

echo -e "\nNginx 服务状态:"

bash 复制代码
systemctl status nginx --no-pager

安装后验证访问

本地测试:执行 curl http://127.0.0.1,能返回 Nginx 欢迎页的 HTML 代码,说明服务正常;

公网测试:

登录阿里云轻量应用服务器控制台 → 找到你的实例 → 点击「防火墙」;

添加规则:协议选 TCP,端口填 80,来源填 0.0.0.0/0,保存;

打开浏览器,输入服务器公网 IP(如 http://120.xx.xx.xx),能看到 Nginx 默认欢迎页即为全部成功。

后续常用命令(编译安装版)

bash

运行

启动

systemctl start nginx

停止

systemctl stop nginx

重启

systemctl restart nginx

重新加载配置(不中断服务)

systemctl reload nginx

查看状态

systemctl status nginx

查看 Nginx 配置是否有语法错误

/usr/local/nginx/sbin/nginx -t

修改nginx配置(若依前后分离版本,服务器配置-标准配置)

bash 复制代码
vi /usr/local/nginx/conf/nginx.conf
bash 复制代码
        location / {
            root   /home/dist;
            try_files $uri $uri/ /index.html;
            index  index.html index.htm;
        }

        location /prod-api/ {
            add_header          'Access-Control-Allow-Origin' '*';
            add_header          'Access-Control-Allow-Credentials' 'true';
            add_header          'Access-Control-Allow-Methods' 'OPTIONS, GET, POST';
            proxy_pass          http://localhost:8080/;
            proxy_connect_timeout       300s;
            proxy_send_timeout  300s;
            proxy_read_timeout  300s;
            proxy_set_header    Host             $host;
            proxy_set_header    X-Real-IP        $remote_addr;
            proxy_set_header    X-Forwarded-For  $proxy_add_x_forwarded_for;
        }
bash 复制代码
 systemctl reload nginx
相关推荐
H.莓飛10 小时前
【C++】【STL】stack_OJ题
服务器·c语言·开发语言·数据结构·c++·算法·visual studio
无足鸟ICT13 小时前
【RHCA+】sosreport(收集诊断文档)
linux·运维·服务器
tonydf15 小时前
没完了,Nginx又报新漏洞!
运维·nginx
瑞雪兆丰年兮16 小时前
Linux零基础快速入门-第二章 Linux基础命令
linux·运维·服务器
光电笑映19 小时前
TCP 为什么可靠?从三次握手、滑动窗口到拥塞控制全面拆解(上)
运维·服务器·网络·udp
飞奔的瓜牛19 小时前
Linux 下使用 OpenSSL 生成自签名证书完整指南
运维·服务器·nginx
峥嵘life21 小时前
Android16 热点「网络共享」开关的设计与实现
服务器·网络·microsoft
张小姐的猫1 天前
【基于 WebSocket 的多用户网页五子棋】—— 整合封装服务器模块设计实现1️⃣
服务器·websocket·网络协议
JaguarJack1 天前
NGINX 两处漏洞可导致未经身份验证的远程代码执行(CVE-2026-123633 和 CVE-2026-123007)
后端·nginx·nginx2root
BingoGo1 天前
NGINX 两处漏洞可导致未经身份验证的远程代码执行(CVE-2026-123633 和 CVE-2026-123007)
后端·nginx