二、OpenCloudOS Server 9 系统 安装 Nginx

OpenCloudOS Server 9 安装 Nginx 完整指南

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


安装流程概览

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

第一步:安装编译依赖

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

第二步:下载 Nginx 源码

bash 复制代码
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

bash 复制代码
./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 服务单元(自动启动)

bash 复制代码
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

加载并启用自动启动:

bash 复制代码
# 重载 systemd 配置
systemctl daemon-reload

# 启动 Nginx
systemctl start nginx

# 设置开机自动启动
systemctl enable nginx

# 查看运行状态
systemctl status nginx

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

bash 复制代码
# 放行 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 访问,或自定义内容:

bash 复制代码
# 查看默认 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 服务单元文件