优何软件 首页 软件资讯 Linux 查看内容

Rocky Linux 9 系统下安装Nginx

2025-7-4 09:10| 来自: 优何整理| 发布者: 门户小编


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:// 验证 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、反向代理、负载均衡、静态资源优化等进阶配置。


路过

雷人

握手

鲜花

鸡蛋

最新评论