Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> Linux 教學

CentOS 6.5 使用 Yum 官方源安裝 Nginx 完整步驟

編輯:Linux 教學

為什麼選擇 Yum 安裝?

相比源碼編譯,Yum 安裝優勢明顯:依賴自動解決、版本管理統一、升級維護簡單、配置文件規範化。

步驟 1:添加 Nginx 官方 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

步驟 2:安裝 Nginx

yum install nginx -y

自動解決依賴(如 pcre、zlib、openssl)並安裝最新穩定版。

步驟 3:啟動與驗證

# 啟動服務
/etc/init.d/nginx start
# 或 service nginx start

# 設置開機自啟
chkconfig nginx on

# 驗證進程與端口
ps aux | grep nginx
netstat -ntlp | grep :80

瀏覽器訪問 https://服務器IP 應顯示「Welcome to nginx!」頁面。

步驟 4:防火牆放行 80 端口(若無法訪問)

# 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

常見問題

  • 端口衝突:Apache 佔用 80 → 停止 httpd 或修改 Nginx 端口
  • SELinux 阻擋:setsebool -P httpd_can_network_connect 1 或調整標簽
  • 源不可用:檢查網絡、嘗試鏡像源(如阿里云、清華源)

升級與維護

# 檢查可用更新
yum check-update nginx

# 升級到最新版
yum update nginx -y

# 重載配置生效
service nginx reload
Copyright © Windows教程網 All Rights Reserved