一、準備工作
在安裝 Nginx 前,確保你已滿足以下條件:
1、已安裝 Windows 操作系統。
2、已安裝 Visual C++ 運行庫,下載地址為https://support.microsoft.com/kb/2977003。
3、已下載 Nginx 的 Windows 版本,下載地址為http://nginx.org/en/download.html。
二、安裝 Nginx
1、解壓 Nginx 壓縮包並放置在合適的目錄下(建議放置在 C 盤根目錄下)。
2、按 Win+R 組合鍵,打開「運行」窗口,輸入命令「cmd」並回車打開命令行界面。
3、在命令行中切換到 Nginx 的解壓目錄,如「cd C:\nginx-1.19.10」。
C:\>cd nginx-1.19.10
4、啟動 Nginx,輸入命令「start nginx」並回車。
C:\nginx-1.19.10>start nginx
5、檢查 Nginx 是否啟動成功,輸入命令「tasklist /fi ‘imagename eq nginx.exe’」並回車,如果有 nginx.exe 的進程,則說明 Nginx 啟動成功。
C:\nginx-1.19.10>tasklist /fi 'imagename eq nginx.exe' Image Name PID Session Name Session# Mem Usage ========================= ======== ================ =========== ============ nginx.exe 12608 Services 0 10,816 K
6、在瀏覽器中輸入網址「http://localhost」,如果出現「Welcome to nginx!」字樣,則說明 Nginx 安裝成功。
三、配置 Nginx
1、在 Nginx 的安裝目錄下找到「conf」文件夾,並打開其中的「nginx.conf」文件。
C:\nginx-1.19.10\conf\nginx.conf
2、在「http」模塊下添加「server」塊,用於配置虛擬主機,如下所示:
http { ... server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } } }
3、重啟 Nginx,輸入命令「nginx -s reload」並回車。
C:\nginx-1.19.10>nginx -s reload
4、在瀏覽器中輸入網址「http://localhost」,如果出現「Welcome to nginx!」字樣,並且可以訪問「html」文件夾中的「index.html」文件,則說明 Nginx 配置成功。
四、常見問題解決
1、安裝 Visual C++ 運行庫時,提示「此包不適用於當前計算機的處理器類型」的解決方法:
(1)查看操作系統的位數(右鍵點擊「計算機」→「屬性」→查看「系統類型」)。
(2)根據操作系統的位數,下載對應的 Visual C++ 運行庫。
2、啟動 Nginx 時,提示「nginx: [emerg] bind() to 0.0.0.0:80 failed (10013: An attempt was made to access a socket in a way forbidden by its access permissions)」的解決方法:
(1)查看「conf」文件夾下的「nginx.conf」文件中是否有重複的「server」塊。
(2)檢查是否已經有其他程序佔用了 80 端口,如果有,可以修改「nginx.conf」文件中的監聽端口號。
3、瀏覽器無法訪問網頁的解決方法:
(1)檢查「nginx.conf」文件中配置的虛擬主機是否正確。
(2)檢查「html」文件夾中是否有可訪問的網頁文件。
原創文章,作者:NFTLZ,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/371500.html