yum -y install openssl-devel pcre-devel zlib-devel
rpm -ivh https://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
此源提供穩定版(Stable)與主線版,預設啟用 Stable。
yum -y install nginx
# 檢查版本
nginx -v
# nginx version: nginx/1.x.x
# 檢查配置語法
nginx -t
# nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
# nginx: configuration file /etc/nginx/nginx.conf test is ok
# 啟動服務
service nginx start
# 或 systemctl start nginx (CentOS 7+)
# 設定開機自啟
chkconfig nginx on
# 或 systemctl enable nginx
# iptables (CentOS 6)
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
service iptables save
# firewalld (CentOS 7+)
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
訪問 https://<伺服器IP>/,應顯示「Welcome to nginx!」預設頁面。
| 路徑 | 說明 |
|---|---|
/etc/nginx/nginx.conf | 主配置檔 |
/etc/nginx/conf.d/ | 虛擬主機配置目錄(建議每站一檔) |
/usr/share/nginx/html/ | 預設 Web 根目錄 |
/var/log/nginx/ | 日誌目錄 |
/etc/logrotate.d/nginx | 日誌輪轉配置 |
/etc/sysconfig/nginx | 啟動參數(如 NGINX_OPTIONS) |
yum clean all && yum makecache。nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use):埠 80 被佔用(Apache/其他進程),netstat -tlnp | grep :80 找出並停用。/usr/share/nginx/html 擁有者為 nginx:nginx,目錄 755、檔案 644。三條指令搞定:yum install 依賴 → rpm 官方源 → yum install nginx。官方源提供及時安全更新、標準化目錄結構、systemd 整合,是生產環境部署 Nginx 的推薦方式。