在rocky中安装nginx
Easul Lv6

由于在 rocky 上直接编译 nginx 老报错,所以可以尝试使用如下方式来安装 nginx 。

折叠代码块BASH 复制代码
1
2
sudo dnf update
sudo dnf install -y nginx

安装完之后,很可能在反向代理服务时会报502,那么这个可能是开启了 SELinux,可尝试使用如下命令来解决问题(参考了这个文章)。

折叠代码块BASH 复制代码
1
setsebool -P httpd_can_network_connect true

如果提示 [emerg] 1862962#1862962: bind() to 0.0.0.0:12355 failed (13: Permission denied)
那么可尝试如下方法

折叠代码块BASH 复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 临时关闭 SELinux
setenforce 0

# 如果生效的话,那么就需要使用如下方法永久关闭 SELinux
# 修改如下文件
vi /etc/selinux/config

# 将如下行改为 disabled
# enforcing SELinux 策略被强制执行。
# permissive SELinux 策略不强制执行,仅作告警。
# disabled 完全关闭SELinux。
SELINUX=enforcing

# 修改后重启系统
reboot
# 查看 SELinux 是否被禁用
getenforce

一些必要操作

折叠代码块BASH 复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
# 设置nginx开机自启动
sudo systemctl enable nginx
# 重启nginx
sudo systemctl restart nginx
# 开启端口
# 永久开放443端口
firewall-cmd --zone=public --add-port=443/tcp --permanent
# 永久关闭443端口
firewall-cmd --zone=public --remove-port=443/tcp --permanent
# 查看端口是否开启
firewall-cmd --query-port=443/tcp
# 重启防火墙
firewall-cmd --reload

默认安装了nginx后,相关路径解释如下

折叠代码块YAML 复制代码
1
2
3
4
/usr/sbin/nginx: nginx路径
/etc/nginx: 配置文件目录
/var/log/nginx/access.log: 访问日志路径
/var/log/nginx/error.log: 错误日志路径

其他使用可参考该篇文章

相关问题

  • nginx直接静态托管除/usr/share/nginx/html下的目录,均报permission denied
    暂未解决,直接将静态项目的目录放到 /usr/share/nginx/html 下即可。

  • 相关安装样例模板
    可参考 这篇文章

 评论
1 评论
Anonymous Chrome 132.0.0.0 Windows 10/11
2025-01-23回复

Wow, this guide really saved me! I was stuck for hours trying to figure out why nginx kept throwing 502 errors after installing it on Rocky. The SELinux fix with setsebool and the explanation about disabling it permanently were absolute gold—I had no idea that was the issue. Also, the step-by-step for opening ports and restarting nginx was super easy to follow. The linked article on Install Nginx on Rocky Linux 9 was so helpful too—honestly, it covered all the little details I was missing. I love this guide and really appreciate how simple you made everything. Thank you so much!

Easul 博主 Chrome 96.0.4664.104 Android 10
2025-05-05回复

I’m delighted to know that my blog has been helpful in resolving your problem. Please accept my apologies for not being able to check the comments regularly. Isn’t the journey towards success truly enjoyable? happy

Powered By Valine
v1.5.2