HCEOS2.0安装Nginx
Nginx 关键信息梳理(HCEOS 2.0 系统)
1. Nginx 关键目录与配置
| 项目 | 路径 | 说明 |
|---|---|---|
| 主配置文件 | /etc/nginx/nginx.conf |
核心配置文件,包含全局和 http 块设置 |
| 默认站点配置 | /etc/nginx/conf.d/default.conf |
默认 server 块配置(监听端口、域名等) |
| 日志目录 | /mnt/sdc/nginx/logs/ |
存放 access.log 和 error.log |
| PID 文件 | /run/nginx.pid |
记录 Nginx 主进程的 PID |
| 端口 | 8080 | |
| 模块目录 | /usr/share/nginx/modules/ |
动态模块配置文件存放位置 |
2. Nginx 服务管理命令
| 命令 | 作用 |
|---|---|
sudo systemctl start nginx |
启动 Nginx |
sudo systemctl stop nginx |
停止 Nginx |
sudo systemctl restart nginx |
重启 Nginx(强制重新加载配置) |
sudo systemctl reload nginx |
平滑重载配置(不中断服务) |
sudo systemctl enable nginx |
设置开机自启 |
sudo systemctl status nginx |
查看运行状态 |
sudo nginx -t |
测试配置文件语法是否正确 |
3. 完整安装步骤(HCEOS 2.0 系统)
步骤 1:安装 Nginx
# 1. 安装 Nginx
sudo yum install -y nginx
步骤 2:配置日志目录
# 创建日志目录并授权
sudo mkdir -p /mnt/sdc/nginx/logs
sudo chown -R nginx:nginx /mnt/sdc/nginx
步骤 3:修改配置文件
编辑 /etc/nginx/nginx.conf(包管理器安装)或 /usr/local/nginx/conf/nginx.conf(源码安装):
user nginx;
worker_processes auto;
error_log /mnt/sdc/nginx/logs/error.log; # 全局错误日志
http {
access_log /mnt/sdc/nginx/logs/access.log; # HTTP 访问日志
error_log /mnt/sdc/nginx/logs/error.log; # HTTP 错误日志(可省略,因全局已定义)
server {
listen 8080; # 监听 8080 端口
root /usr/share/nginx/html;
# 其他 server 配置...
}
}
步骤 4:测试并启动 Nginx
# 检查配置语法
sudo nginx -t
# 启动 Nginx(根据安装方式选择)
sudo systemctl start nginx # 包管理器安装
# 设置开机自启(仅 systemd 系统)
sudo systemctl enable nginx
步骤 5:验证服务
# 检查端口监听
ss -tuln | grep 8080
# 查看日志是否生成
tail -f /mnt/sdc/nginx/logs/access.log
# 测试访问
curl -I http://localhost:8080
4. 补充说明
-
日志轮替(Log Rotation):
如果需要自动切割日志,可配置logrotate:sudo vim /etc/logrotate.d/nginx添加以下内容:
/mnt/sdc/nginx/logs/*.log { daily missingok rotate 7 compress delaycompress notifempty create 0640 nginx nginx sharedscripts postrotate /bin/kill -USR1 $(cat /run/nginx.pid 2>/dev/null) 2>/dev/null || true endscript } -
SELinux 问题(如果启用):
sudo semanage fcontext -a -t httpd_log_t "/mnt/sdc/nginx/logs(/.*)?" sudo restorecon -Rv /mnt/sdc/nginx
总结
- 安装方式:优先尝试
yum install nginx,失败后改用源码编译。 - 关键路径:配置文件(
/etc/nginx/nginx.conf)、日志(/mnt/sdc/nginx/logs/)。 - 管理命令:
systemctl或手动启停(源码安装时)。 - 验证方法:检查端口、日志、curl 测试。

浙公网安备 33010602011771号