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、反向代理、負載均衡、靜態資源優化等進階配置。
特別聲明:以上內容(如有圖片或視頻亦包括在內)為自媒體平臺“網易號”用戶上傳并發布,本平臺僅提供信息存儲服務。
Notice: The content above (including the pictures and videos if any) is uploaded and posted by a user of NetEase Hao, which is a social media platform and only provides information storage services.