openEuler 安装 Nginx 指南
概述
本文档提供在 openEuler 系统上安装、配置和管理 Nginx Web 服务器的完整指南,包含多种安装方法和常见问题解决方案。
安装方法
方法一:使用 yum/dnf 包管理器安装
1. 更新系统包
sudo dnf update -y
2. 安装 Nginx
sudo dnf install nginx -y
3. 启动并设置开机自启
# 启动 nginx 服务
sudo systemctl start nginx
# 设置开机自启动
sudo systemctl enable nginx
# 检查服务状态
sudo systemctl status nginx
4. 验证安装
# 检查 nginx 版本
nginx -v
# 查看详细编译信息
nginx -V
方法二:从源码编译安装(获取最新版本)
1. 安装编译依赖
sudo dnf install -y gcc gcc-c++ make pcre-devel zlib-devel openssl-devel wget tar
2. 下载 Nginx 源码
# 获取最新版本(请访问 https://nginx.org/en/download.html 确认最新版本号)
wget https://nginx.org/download/nginx-1.24.0.tar.gz
tar -zxvf nginx-1.24.0.tar.gz
cd nginx-1.24.0
3. 编译安装
./configure --prefix=/usr/local/nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_stub_status_module \
--with-stream
make
sudo make install
4. 配置系统服务
创建 systemd 服务文件:
sudo vim /etc/systemd/system/nginx.service
添加以下内容:
[Unit]
Description=nginx - high performance web server
Documentation=https://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
[Install]
WantedBy=multi-user.target
5. 启动服务
sudo systemctl daemon-reload
sudo systemctl start nginx
sudo systemctl enable nginx
配置防火墙
1. 开放 HTTP/HTTPS 端口
# 如果使用 firewalld
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
# 或直接开放端口
sudo firewall-cmd --permanent --add-port=80/tcp
sudo firewall-cmd --permanent --add-port=443/tcp
sudo firewall-cmd --reload
2. 检查端口监听
sudo ss -tulpn | grep nginx
常用命令
服务管理
# 启动服务
sudo systemctl start nginx
# 停止服务
sudo systemctl stop nginx
# 重启服务
sudo systemctl restart nginx
# 平滑重载配置
sudo systemctl reload nginx
# 检查配置语法
sudo nginx -t
# 查看运行状态
sudo systemctl status nginx
配置文件路径
包管理器安装:
- 主配置文件:
/etc/nginx/nginx.conf - 站点配置目录:
/etc/nginx/conf.d/ - 默认站点目录:
/usr/share/nginx/html/ - 日志文件目录:
/var/log/nginx/
源码编译安装:
- 主配置文件:
/usr/local/nginx/conf/nginx.conf - 日志文件目录:
/usr/local/nginx/logs/
验证安装是否成功
1. 浏览器访问
在浏览器中访问服务器的 IP 地址或域名:
http://your-server-ip
2. 命令行测试
curl -I http://localhost
预期输出:
HTTP/1.1 200 OK
Server: nginx/1.24.0
卸载 Nginx
包管理器安装的卸载方法
sudo dnf remove nginx
sudo rm -rf /etc/nginx /var/log/nginx
源码编译安装的卸载方法
sudo systemctl stop nginx
sudo systemctl disable nginx
sudo rm -f /etc/systemd/system/nginx.service
sudo systemctl daemon-reload
sudo rm -rf /usr/local/nginx
常见问题解决
1. 端口被占用
# 查看端口占用
sudo netstat -tulpn | grep :80
# 如果被占用,可以修改 nginx 配置文件中的监听端口
2. 权限问题
# 确保 nginx 有正确的权限
sudo chown -R nginx:nginx /var/log/nginx
sudo chmod -R 755 /var/log/nginx
3. SELinux 问题(如果启用)
# 临时关闭 SELinux(重启后恢复)
sudo setenforce 0
# 永久关闭(编辑配置文件)
sudo vim /etc/selinux/config
# 修改为:SELINUX=disabled
安装方法选择建议
- 初学者或快速部署:使用方法一(包管理器)
- 需要特定版本或模块:使用方法二(源码编译)
文档信息
- 创建日期:2025年12月2日
- 适用系统:openEuler
- 软件版本:Nginx 1.24.0(示例版本,请根据需要调整)
- 最后更新:2025年12月2日
文档由 AI 助手根据对话内容整理生成,仅供参考,实际部署时请根据具体情况调整。