OpenCloudOS Server 9 安装 Nginx 完整指南

安装目录:/usr/local/nginx,配置 systemd 自动启动,通过 IP 访问 index.html


安装流程概览

复制代码

|---|-----------------------------------------------------|
| | 安装依赖 → 下载源码 → 编译安装 → 创建 systemd 服务 → 配置防火墙 → 验证访问 |


第一步:安装编译依赖

复制代码

|---|----------------------------------------------------------------------------------------------|
| | dnf install -y gcc gcc-c++ make pcre pcre-devel zlib zlib-devel openssl openssl-devel wget |


第二步:下载 Nginx 源码

复制代码

|---|------------------------------------------------------|
| | cd /usr/local/src |
| | wget http://nginx.org/download/nginx-1.26.2.tar.gz |
| | tar -zxvf nginx-1.26.2.tar.gz |
| | cd nginx-1.26.2 |

如需最新稳定版,可到 nginx.org/download 查看版本号并替换上方链接。


第三步:编译并安装到 /usr/local/nginx

复制代码

|---|----------------------------------|
| | ./configure \ |
| | --prefix=/usr/local/nginx \ |
| | --with-http_ssl_module \ |
| | --with-http_v2_module \ |
| | --with-http_gzip_static_module |
| | |
| | make -j$(nproc) |
| | make install |

安装完成后目录结构:

路径 说明
/usr/local/nginx/sbin/nginx 可执行文件
/usr/local/nginx/conf/nginx.conf 主配置文件
/usr/local/nginx/html/ 网站根目录
/usr/local/nginx/logs/ 日志目录

第四步:创建 systemd 服务单元(自动启动)

复制代码

|---|----------------------------------------------------|
| | cat > /etc/systemd/system/nginx.service << 'EOF' |
| | [Unit] |
| | Description=Nginx HTTP Server |
| | After=network.target |
| | |
| | [Service] |
| | Type=forking |
| | PIDFile=/usr/local/nginx/logs/nginx.pid |
| | ExecStartPre=/usr/local/nginx/sbin/nginx -t |
| | ExecStart=/usr/local/nginx/sbin/nginx |
| | ExecReload=/bin/kill -s HUP $MAINPID |
| | ExecStop=/bin/kill -s QUIT $MAINPID |
| | PrivateTmp=true |
| | |
| | [Install] |
| | WantedBy=multi-user.target |
| | EOF |

加载并启用自动启动:

复制代码

|---|---------------------------|
| | # 重载 systemd 配置 |
| | systemctl daemon-reload |
| | |
| | # 启动 Nginx |
| | systemctl start nginx |
| | |
| | # 设置开机自动启动 |
| | systemctl enable nginx |
| | |
| | # 查看运行状态 |
| | systemctl status nginx |


第五步:配置防火墙放行 80 端口

复制代码

|---|------------------------------------------------|
| | # 放行 HTTP 80 端口 |
| | firewall-cmd --permanent --add-service=http |
| | |
| | # 如果同时需要 HTTPS 443 |
| | firewall-cmd --permanent --add-service=https |
| | |
| | # 重载防火墙规则 |
| | firewall-cmd --reload |
| | |
| | # 确认规则生效 |
| | firewall-cmd --list-all |


第六步:验证 index.html 页面

Nginx 默认已在 /usr/local/nginx/html/index.html 生成欢迎页面,可直接通过 IP 访问,或自定义内容:

复制代码

|---|---------------------------------------------------------------|
| | # 查看默认 index.html |
| | cat /usr/local/nginx/html/index.html |
| | |
| | # 自定义首页(可选) |
| | cat > /usr/local/nginx/html/index.html << 'EOF' |
| | <!DOCTYPE html> |
| | <html> |
| | <head><meta charset="UTF-8"><title>My Server</title></head> |
| | <body> |
| | <h1>Nginx 运行正常</h1> |
| | <p>OpenCloudOS 9 + Nginx /usr/local/nginx</p> |
| | </body> |
| | </html> |
| | EOF |

浏览器访问:

复制代码

|---|------------------|
| | http://你的服务器IP |


常用管理命令汇总

操作 命令
启动 systemctl start nginx
停止 systemctl stop nginx
重启 systemctl restart nginx
重载配置 systemctl reload nginx
查看状态 systemctl status nginx
测试配置语法 /usr/local/nginx/sbin/nginx -t
查看版本 /usr/local/nginx/sbin/nginx -v

关键路径一览

路径 说明
/usr/local/nginx/sbin/nginx 可执行文件
/usr/local/nginx/conf/nginx.conf 主配置文件
/usr/local/nginx/html/ 网站根目录
/usr/local/nginx/logs/ 日志目录
/etc/systemd/system/nginx.service 服务单元文件
相关推荐
Emily156853598703 小时前
Emerson 1C31129G03 Analog Input Module
java·开发语言·前端·plc·emerson·1c31129g03·input module
To_OC8 小时前
面试被问了三回三栏布局,这次我终于把 BFC 那层窗户纸捅破了
前端·css·面试
xixiaoyunya8 小时前
内网穿透工具选型分析:从 ngrok 的局限性看国内开发场景下的替代方案
网络
To_OC8 小时前
啃完 TS 工具类型我发现:Pick 和 Omit 原来就是一层窗户纸
前端·面试·typescript
FreeTinker8 小时前
远程查看的技术路径与工程选型分析
网络
Jimmy_jimi9 小时前
利用nginx代理完成url重定位
运维·nginx
风月说与山鬼10 小时前
三、大括号语法
前端·react.js
kyriewen10 小时前
我把最常踩的8个CORS跨域报错整理了一遍——第8个去年还不存在
前端·javascript
嵌入式阿蔡10 小时前
传感器_03:电机驱动——让 GPIO 当指挥官,别让 GPIO 当电源
网络·stm32·单片机·嵌入式硬件·嵌入式实时数据库