网站首页 > 技术教程 正文
Rocky Linux 9 系统下安装Nginx
大家好,我是星哥,Nginx 凭借其高性能、低资源消耗以及优秀的反向代理能力,已成为 Web 服务部署的主流选择之一。
本文将带你在 Rocky Linux 9 系统下从零开始安装并配置 Nginx 服务,适合初学者和运维爱好者快速上手。
快速安装
gitee(国内):
wget https://gitee.com/funet8/Rocky-Linux-Shell/raw/main/shell/Rocky_Linux_9_Dnf_Install_Nginx.sh
sh Rocky_Linux_9_Dnf_Install_Nginx.sh
github:
wget https://raw.githubusercontent.com/funet8/Rocky-Linux-Shell/refs/heads/main/shell/Rocky_Linux_9_Dnf_Install_Nginx.sh
sh Rocky_Linux_9_Dnf_Install_Nginx.sh
# 主要功能介绍
# 1.dnf安装nginx
# 2.firewall-cmd放开 80和443端口
# 3.nginx配置文件:
# 主配置文件:/data/conf/nginx.conf
# 站点配置文件:/data/conf/sites-available/nginx_*
一、更新系统和安装EPEL
# 更新系统
dnf update -y
# 安装 EPEL 仓库(以防依赖)
dnf install epel-release -y
二、安装 Nginx
启动并设置开机自启
dnf install nginx -y
# 启动并设置开机自启
systemctl start nginx
systemctl enable nginx
三、配置防火墙允许 HTTP 和 HTTPS
# 配置防火墙允许 HTTP 和 HTTPS
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --zone=public --add-port=443/tcp --permanent
firewall-cmd --reload
四、配置Nginx目录和配置文件
#配置文件目录设置
wget -q -O - https://gitee.com/funet8/Rocky-Linux-Shell/raw/main/shell/create_dirs.sh | bash -sh
#移动nginx配置文件
cp -p /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
rm -rf /etc/nginx/nginx.conf
cd /data/conf/
wget https://gitee.com/funet8/Rocky-Linux-Shell/raw/main/shell/nginx.conf
ln -s /data/conf/nginx.conf /etc/nginx/
echo "nginx.conf move success"
#站点配置
cd /data/conf/sites-available/
wget https://gitee.com/funet8/Rocky-Linux-Shell/raw/main/shell/nginx_main.conf
五、创建用户和用户组
创建www用户
并且设置目录权限
#添加www组和www用户
groupadd www
useradd -g www www
#设置目录权限##########################################################################
chown -R www:www /data/wwwroot/web
chown -R www:www /data/conf/sites-available/
# 权限问题会报错 403
chmod 755 -R /data/
# 删除默认站点文件
rm -rf /usr/share/nginx/html/*
echo 'index page' > /usr/share/nginx/html/index.html
chown www.www -R /usr/share/nginx/html/
六、检查Nginx是否启动成功
# 检查是否启动成功
systemctl restart nginx
systemctl status nginx | grep Active
echo "Nginx 安装并启动完成。"
echo "请访问 http://<你的服务器IP> 验证 Nginx 是否运行。"
七、定时任务切割日志
crontab定时每日切割日志
###切割日志
cd /data/conf/shell/
wget https://gitee.com/funet8/Rocky-Linux-Shell/raw/main/shell/nginx_cut_web_log.sh
chmod +x /data/conf/shell/nginx_cut_web_log.sh
echo "00 00 * * * root /data/conf/shell/nginx_cut_web_log.sh" >> /etc/crontab
systemctl restart crond
基本配置文件路径说明
项目路径配置主文件/data/conf/nginx.conf站点配置目录/data/conf/sites-available/nginx_*默认站点文件/usr/share/nginx/html/index.html日志文件/data/wwwroot/log/nginx_access.log /data/wwwroot/log/nginx_error.log
Nginx常用命令
# 重新加载配置
sudo nginx -s reload
# 检查配置是否有误
sudo nginx -t
# 停止服务
sudo systemctl stop nginx
# 重启服务
sudo systemctl restart nginx
至此,您已成功在 Rocky Linux 9 系统中部署了 Nginx,并完成了基本的服务启动与防火墙配置。后续可以继续扩展 HTTPS、反向代理、负载均衡、静态资源优化等进阶配置。
猜你喜欢
- 2025-07-15 Rocky Linux Nginx 自动更新免费 SSL,全流程实战
- 2025-07-15 nginx-1.22.1在linux服务器上的安装
- 2025-07-15 Rocky Linux 9.x 从零安装 Nginx 全流程:源码编译 + dnf 安装方案详解
- 2025-07-15 Linux-Nginx-反向代理篇-02(nginx反向代理apache)
- 2025-07-15 RockyLinux 9快速部署Nginx+HTTPS(基于DNF安装)
你 发表评论:
欢迎- 07-15Rocky Linux Nginx 自动更新免费 SSL,全流程实战
- 07-15nginx-1.22.1在linux服务器上的安装
- 07-15Rocky Linux 9 系统下安装Nginx(在linux中安装nginx)
- 07-15Rocky Linux 9.x 从零安装 Nginx 全流程:源码编译 + dnf 安装方案详解
- 07-15Linux-Nginx-反向代理篇-02(nginx反向代理apache)
- 07-15RockyLinux 9快速部署Nginx+HTTPS(基于DNF安装)
- 07-15Stellar Repair for MySQL:受损 MySQL 数据库的专业恢复工具
- 07-15在CentOS7系统源码安装Nginx+MySQL+PHP+Go
- 最近发表
-
- Rocky Linux Nginx 自动更新免费 SSL,全流程实战
- nginx-1.22.1在linux服务器上的安装
- Rocky Linux 9 系统下安装Nginx(在linux中安装nginx)
- Rocky Linux 9.x 从零安装 Nginx 全流程:源码编译 + dnf 安装方案详解
- Linux-Nginx-反向代理篇-02(nginx反向代理apache)
- RockyLinux 9快速部署Nginx+HTTPS(基于DNF安装)
- Stellar Repair for MySQL:受损 MySQL 数据库的专业恢复工具
- 在CentOS7系统源码安装Nginx+MySQL+PHP+Go
- linux通过yum安装nginx和mysql(linux在线安装nginx)
- Chat to MySQL 最佳实践:MCP Server 服务调用
- 标签列表
-
- 下划线是什么 (87)
- 精美网站 (58)
- qq登录界面 (90)
- nginx 命令 (82)
- nginx .http (73)
- nginx lua (70)
- nginx 重定向 (68)
- Nginx超时 (65)
- nginx 监控 (57)
- odbc (59)
- rar密码破解工具 (62)
- annotation (71)
- 红黑树 (57)
- 智力题 (62)
- php空间申请 (61)
- 按键精灵 注册码 (69)
- 软件测试报告 (59)
- ntcreatefile (64)
- 闪动文字 (56)
- guid (66)
- abap (63)
- mpeg 2 (65)
- column (63)
- dreamweaver教程 (57)
- excel行列转换 (56)
本文暂时没有评论,来添加一个吧(●'◡'●)