一、安裝 Nginx
1、在 CentOS 下使用 YUM 進行安裝:
yum -y install nginx
2、安裝完成後,可以使用 systemctl 來控制 Nginx 的啟動、停止和重啟。
systemctl start nginx #啟動 Nginx systemctl stop nginx #停止 Nginx systemctl restart nginx #重啟 Nginx
二、配置 Nginx
1、Nginx 的配置文件位於 /etc/nginx/nginx.conf,可使用 vim 等編輯器進行修改。
vim /etc/nginx/nginx.conf
2、修改配置文件需要注意以下幾點:
(1)server_name 修改為服務器地址或域名。
(2)root 修改為對應的網站目錄路徑。
(3)location 配置 URL 路徑。
3、配置 Nginx 後,使用以下命令測試配置是否正確。
nginx -t
三、啟動 Nginx
1、在 CentOS 下啟動 Nginx,使用以下命令:
systemctl start nginx
2、啟動 Nginx 後,可以使用以下命令檢查 Nginx 運行狀態:
systemctl status nginx
若顯示為 green,則表示 Nginx 運行正常。
四、常見錯誤處理
1、若啟動 nginx 出現錯誤信息:Job for nginx.service failed because the control process exited with error code. See “systemctl status nginx.service” and “journalctl -xe” for details.:
(1)查看 Nginx 錯誤信息:
systemctl status nginx.service journalctl -xe
(2)若 nginx.conf 文件中有未定義的變量,則在 /etc/nginx/nginx.conf 文件中添加以下代碼:
include /etc/nginx/conf.d/*.conf;
然後,在 /etc/nginx/conf.d/ 目錄下新建 *.conf 文件,對變量進行定義。
2、若啟動 nginx 後,通過瀏覽器訪問出現 403 錯誤,則需要在nginx.conf 文件中進行如下配置:
location / { root /usr/share/nginx/html; index index.html index.htm; try_files $uri $uri/ /index.html; }
五、結語
配置 Nginx 並啟動是很重要的一步,相信通過本文講解,大家可以更好地理解 CentOS Nginx 的啟動方法,希望本文的代碼示例對大家有所幫助。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/196762.html