加油
努力

在华为云EulerOS 2.0标准版64位系统上如何安装和配置Nginx?

在华为云 EulerOS 2.0(标准版,64位)上安装和配置 Nginx,需特别注意:EulerOS 2.0 基于 CentOS 7 兼容内核,但官方软件源默认不提供 nginx 主包(仅含少量基础模块或旧版 nginx-all-modules),且不推荐直接使用 EPEL 源(存在兼容性风险)。华为官方推荐方式是通过 华为云镜像站提供的 EulerOS 官方 nginx 包编译安装(更可控、安全)。以下是经过验证的最佳实践方案


✅ 推荐方案:使用华为云官方 EulerOS 2.0 nginx RPM 包(最稳妥)

步骤 1:确认系统信息

cat /etc/euleros-release
# 应输出类似:EulerOS Linux Server 2.0 (SPx) 或 EulerOS 2.0 (SP3/SP5)
uname -r
# 确保为 x86_64 内核

⚠️ 注意:EulerOS 2.0 SP3 及之后版本已内置较新 nginx(如 1.16+),SP2 可能需手动安装。

步骤 2:配置华为云官方软件源(确保启用 base 和 updates)

# 备份原 repo 配置(可选)
sudo cp -r /etc/yum.repos.d/{,backup_}euler*

# 确认 euler.repo 已启用(尤其 [EulerOS-2.0] 和 [EulerOS-2.0-updates])
sudo sed -i '/^[.*]$/!b; /updates/!b; s/enabled=0/enabled=1/' /etc/yum.repos.d/euler*.repo
sudo yum clean all && sudo yum makecache

步骤 3:搜索并安装 nginx(华为云官方维护包)

# 查看可用 nginx 相关包(华为云提供 nginx-mainline 或 nginx-stable)
yum list available | grep -i nginx

# ✅ 推荐安装(以 SP5 为例,实际包名可能略有差异,请按输出选择):
sudo yum install -y nginx

# 若提示未找到,尝试启用华为云 extras 源(部分版本需要):
sudo yum-config-manager --enable EulerOS-2.0-extras
sudo yum install -y nginx

🔍 常见包名示例(华为云镜像站提供):

  • nginx-1.16.1-10.h21.eulerosv2r9.x86_64(稳定版)
  • nginx-mainline-1.21.6-1.h21.eulerosv2r9.x86_64(主线版,需额外源)

步骤 4:验证安装并启动服务

# 检查版本(应显示华为定制信息)
nginx -v
# 示例输出:nginx version: nginx/1.16.1 (EulerOS)

# 检查配置语法
sudo nginx -t

# 启用开机自启并启动
sudo systemctl enable nginx
sudo systemctl start nginx

# 检查状态
sudo systemctl status nginx

步骤 5:开放防火墙(若启用 firewalld)

sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload

步骤 6:验证访问

curl -I http://localhost
# 应返回 HTTP/1.1 200 OK

在浏览器中访问 http://<您的ECS公网IP>,应看到 "Welcome to nginx on EulerOS!" 页面(华为定制欢迎页)。


🛠️ 备选方案:源码编译安装(适用于需最新版或定制模块)

✅ 优势:完全可控、支持 TLS 1.3、Brotli、动态模块等
❗ 要求:安装开发工具链

# 1. 安装依赖
sudo yum groupinstall -y "Development Tools"
sudo yum install -y pcre-devel zlib-devel openssl-devel gcc-c++

# 2. 创建工作目录
mkdir -p ~/nginx-build && cd ~/nginx-build

# 3. 下载稳定版源码(以 1.24.0 为例,替换为最新版)
wget https://nginx.org/download/nginx-1.24.0.tar.gz
tar -zxvf nginx-1.24.0.tar.gz
cd nginx-1.24.0

# 4. 配置(启用常用模块,禁用不必要模块)
./configure 
  --prefix=/usr/local/nginx 
  --sbin-path=/usr/local/nginx/sbin/nginx 
  --conf-path=/etc/nginx/nginx.conf 
  --pid-path=/var/run/nginx.pid 
  --lock-path=/var/run/nginx.lock 
  --error-log-path=/var/log/nginx/error.log 
  --http-log-path=/var/log/nginx/access.log 
  --with-http_ssl_module 
  --with-http_v2_module 
  --with-http_realip_module 
  --with-http_stub_status_module 
  --with-http_gzip_static_module 
  --with-pcre 
  --with-file-aio 
  --with-threads 
  --with-compat

# 5. 编译安装
make -j$(nproc)
sudo make install

# 6. 创建 systemd 服务文件(/etc/systemd/system/nginx.service)
sudo tee /etc/systemd/system/nginx.service > /dev/null << 'EOF'
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
Restart=on-failure
RestartSec=5
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF

# 7. 启用服务
sudo systemctl daemon-reload
sudo systemctl enable nginx
sudo systemctl start nginx

✅ 验证:sudo /usr/local/nginx/sbin/nginx -t & curl http://localhost


📌 关键配置建议(/etc/nginx/nginx.conf

# 在 http {} 块中添加(提升安全性与性能)
include /etc/nginx/conf.d/*.conf;

# 推荐优化项(追加到 http 块末尾):
sendfile            on;
tcp_nopush          on;
tcp_nodelay         on;
keepalive_timeout   65;
types_hash_max_size 4096;

# 启用 Gzip 压缩
gzip                on;
gzip_vary           on;
gzip_min_length     1024;
gzip_types          text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

# 安全头(可选,放在 server 块中)
add_header X-Frame-Options "DENY" always;
add_header X-Content-Type-Options "nosniff" always;
add_header X-XSS-Protection "1; mode=block" always;

🚫 常见问题排查

现象 解决方案
nginx: command not found 检查是否安装成功:rpm -qa | grep nginx;若编译安装,确认 PATH 是否包含 /usr/local/nginx/sbin
Failed to start nginx.service: Unit not found 检查 service 文件路径是否正确,执行 systemctl daemon-reload
Address already in use: bind() sudo ss -tulpn | grep :80 查进程,或检查是否已运行 Apache/其他 Web 服务
403 Forbidden 检查 /usr/share/nginx/html/ 权限:sudo chown -R root:root /usr/share/nginx/html;确认 nginx.confuser nginx; 存在且用户存在

✅ 最终验证清单

  • [ ] nginx -v 输出版本号(含 EulerOS 标识)
  • [ ] systemctl status nginx 显示 active (running)
  • [ ] curl -I http://localhost 返回 200 OK
  • [ ] 浏览器访问公网 IP 显示欢迎页
  • [ ] 防火墙/安全组放行 80/443 端口

如需进一步配置(HTTPS、反向X_X、负载均衡、Lua 扩展等),可告知具体场景,我可提供对应 server 块配置模板。

是否需要我为你生成一个 生产环境推荐的最小化 nginx.conf + conf.d/default.conf 示例

云服务器