CentOS7 上安裝 Nginx
Nginx 是異步框架的網頁服務器,也可以用作反向代理、負載平衡器和 HTTP 緩存。該軟件由伊戈爾 · 賽索耶夫創建並於 2004 年首次公開發布。2011 年成立同名公司以提供支持。2019 年 3 月 11 日,Nginx 公司被 F5 Networks 以 6.7 億美元收購。Nginx 是免費的開源軟件,根據類 BSD 許可證的條款發佈.
前提條件
請確保你以具有 root 權限的用戶身份登錄, 並且你沒有在端口 80 或 443 上運行 Apache 或任何其他服務。
在 CentOS 上安裝 Nginx
按照以下步驟在 CentOS 服務器上安裝 Nginx:
1.Nginx 包在 EPEL 存儲庫中可用。如果你還沒有安裝 EPEL 存儲庫
> yum install epel-release
2. 通過 yum 命令安裝 Nginx:
> yum install nginx
如果這是你第一次從 EPEL 存儲庫安裝軟件包,yum 可能會提示你導入 EPEL GPG 密鑰:
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
Importing GPG key 0x352C64E5:
Userid : "Fedora EPEL (7) <epel@fedoraproject.org>"
Fingerprint: 91e9 7d7c 4a5e 96f1 7f3e 888f 6a2f aea2 352c 64e5
Package : epel-release-7-noarch (@extras)
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
Is this ok [y/N]:
如果是這種情況,請鍵入
y
並點擊Enter
。
3. 安裝完成後,啓用並啓動 Nginx 服務:
> systemctl enable nginx //開機啓動
> systemctl start nginx //啓動Nginx服務
使用以下命令檢查 Nginx 服務的狀態:
> systemctl status nginx
輸出應如下所示:
4. 防火牆開放端口
使用以下命令打開必要的端口:
> firewall-cmd --permanent --zone=public --add-service=http
> firewall-cmd --permanent --zone=public --add-service=https
> firewall-cmd --reload
5. 驗證 Nginx,瀏覽器輸入http://IP
使用 systemctl 管理 Nginx 服務
你可以像管理 任何其他 systemd 單元一樣管理 Nginx 服務。
-
停止 Nginx 服務
systemctl stop nginx
-
啓動 Nginx 服務
systemctl start nginx
-
要重新啓動 Nginx 的服務
systemctl restart nginx
-
進行一些配置更改後,重新加載 Nginx 服務
systemctl reload nginx
-
Nginx 服務開機啓動
systemctl enable nginx
-
如果要禁用 Nginx 服務開機啓動
systemctl disable nginx
Nginx 配置文件的結構和最佳實踐
-
所有 Nginx 配置文件都位於該
/etc/nginx/
目錄中。 -
主要的 Nginx 配置文件是
/etc/nginx/nginx.conf
-
爲了使 Nginx 配置更易於維護,建議爲每個域創建一個單獨的配置文件。
-
新的 Nginx 服務器塊文件必須
.conf
以/etc/nginx/conf.d
目錄結尾並存儲在目錄中。你可以根據需要擁有任意數量的服務器塊。 -
例如,如果你的域名是
rumenz.com
那麼你的配置文件應該命名爲
/etc/nginx/conf.d/rumenz.com.conf
-
Nginx 日誌文件(
access.log
和error.log
)位於該/var/log/nginx/
目錄中。建議有不同access
,並error
爲每個服務器模塊的日誌文件。 -
你可以將域文檔根目錄設置爲你想要的任何位置。webroot 最常見的位置包括:
/home/<user_name>/<site_name>
/var/www/<site_name>
/var/www/html/<site_name>
/opt/<site_name>
/usr/share/nginx/html
本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源:https://mp.weixin.qq.com/s/FD1wyiwHQvuarkhRvr2XCQ