相比源碼編譯,Yum 安裝優勢明顯:依賴自動解決、版本管理統一、升級維護簡單、配置文件規範化。
cd /etc/yum.repos.d/
vim nginx.repo
寫入以下內容($releasever 會自動匹配 CentOS 版本):
[nginx]
name=nginx repo
baseurl=https://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
yum install nginx -y
自動解決依賴(如 pcre、zlib、openssl)並安裝最新穩定版。
# 啟動服務
/etc/init.d/nginx start
# 或 service nginx start
# 設置開機自啟
chkconfig nginx on
# 驗證進程與端口
ps aux | grep nginx
netstat -ntlp | grep :80
瀏覽器訪問 https://服務器IP 應顯示「Welcome to nginx!」頁面。
# iptables
iptables -I INPUT -p tcp --dport 80 -j ACCEPT
service iptables save
# 或 firewalld (CentOS 7+)
firewall-cmd --permanent --add-service=http
firewall-cmd --reload
| 項目 | 路徑/命令 |
|---|---|
| 主配置文件 | /etc/nginx/nginx.conf |
| 站點配置目錄 | /etc/nginx/conf.d/ |
| 默認站點根目錄 | /usr/share/nginx/html/ |
| 訪問日誌 | /var/log/nginx/access.log |
| 錯誤日誌 | /var/log/nginx/error.log |
| 啟動服務 | service nginx start |
| 停止服務 | service nginx stop |
| 重載配置 | service nginx reload |
| 測試配置 | nginx -t |
setsebool -P httpd_can_network_connect 1 或調整標簽# 檢查可用更新
yum check-update nginx
# 升級到最新版
yum update nginx -y
# 重載配置生效
service nginx reload