輕量 nginx 網頁伺服器,在之前曾寫過一篇超簡易的 nginx 設置上手文
今天來加點料,加上可執行 PHP 的環境,並設置 Laravel 的專案環境
設置環境
- Windows 10
- nginx 1.12.0
- PHP 7.1.4-nts-x86/x64
準備檔案
- nginx
- Visual C++ 可轉散發套件
- PHP
至於 Visual C++ 可轉散發套件要選什麼版本,是依照 PHP 下載的版本來決定要用哪個。
如果像下圖 php-7.1.4-nts-Win32-VC14-x86 則需要裝 vc_redist_x86.exe (下載的是 Microsoft Visual C++ 2015 的 x86 版本)

安裝 nginx
下載 nginx 並解壓縮至任何資料夾 (e.g. C:\ngnix)
只要在命令提示字元開啟,並輸入 .\nginx 就完成 nginx 啟用,這個太簡單了厚?
好,先跳過,來設點複雜一點點的 PHP 設定
安裝 Visual C++ 2015 可轉散發套件 (x86 / x64)
直接執行下載下來的 vc_redist_x86.exe / vc_redist_x64.exe 一直執行下一步至安裝完成
安裝 PHP 7.1.4
下載下來的壓縮檔,將它解壓至目標目錄 (e.g. E:\_devlib\php)
複製 php.ini-development 另存成 php.ini (若要實際佈署用請複製 php.ini-production)
設置 php.ini
基本需要設定的部分,用編輯器的搜尋功能去找會比較快
extension_dir = "ext"
cgi.fix_pathinfo = 1
date.timezone = Asia/Taipei
要開啟的外掛就依需求修改,目前先依 Laravel 專案的需求開啟以下外掛
extension=php_curl.dll
extension=php_fileinfo.dll
extension=php_gd2.dll
extension=php_gettext.dll
extension=php_mbstring.dll
extension=php_openssl.dll
extension=php_pdo_mysql.dll
extension=php_pdo_sqlite.dll
設置 nginx.conf
再回來到 nginx 的設定部分,我們要來讓它可以執行 PHP
在 nginx/conf 的資料夾中,編輯 nginx.conf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#user nobody; | |
worker_processes 1; | |
events { | |
worker_connections 1024; | |
} | |
http { | |
include mime.types; | |
default_type application/octet-stream; | |
sendfile on; | |
keepalive_timeout 65; | |
server { | |
listen 80; | |
server_name localhost; | |
charset utf-8; | |
access_log logs/host.access.log main; | |
location / { | |
root html; | |
index index.html index.htm index.php; | |
} | |
#error_page 404 /404.html; | |
# redirect server error pages to the static page /50x.html | |
# | |
error_page 500 502 503 504 /50x.html; | |
location = /50x.html { | |
root html; | |
} | |
# proxy the PHP scripts to Apache listening on 127.0.0.1:80 | |
# | |
#location ~ \.php$ { | |
# proxy_pass http://127.0.0.1; | |
#} | |
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 | |
# | |
location ~ \.php$ { | |
root html; | |
fastcgi_pass 127.0.0.1:9000; | |
fastcgi_index index.php; | |
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; | |
include fastcgi_params; | |
} | |
# deny access to .htaccess files, if Apache's document root | |
# concurs with nginx's one | |
# | |
location ~ /\.ht { | |
deny all; | |
} | |
} | |
} |
其中在
location /
區塊的 index 部分加上 index.php,PHP 預設索引檔就設好了再來是
location ~ \.php$
區塊,原本 nginx 就很貼心的先幫我們預留,只需要做點修改在 fastcig_param 的部分改成下方的設定
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
設定快完成了,只差 PHP-CGI 怎麼執行呢?在命名提示字元輸入以下指令
E:\_devlib\php\php7.1.4\php-cgi -b 127.0.0.1:9000 -c E:\_devlib\php\php7.1.4\php.ini
127.0.0.1:9000 是對應我們剛剛在 nginx.conf 中 fastcgi_pass 的值,當然 port 也可以隨自己的喜好做設定
這時再啟用 nginx 即完成最基本 PHP Server 設定!!
如果 nginx 已經在執行中,可以輸入
nginx -s reload
來重啟設定 start-nginx.bat 及 stop-nginx.bat
nginx 跟 php-cgi 都要透過命名列來開啟,但視窗需要常駐不能關掉,那麼就把這些索碎的事自動化吧
有網友寫好的設定傳送門,這只是結合上述的命令綜合起來,就不多佔篇幅,繼續設定 Laravel 專案的環境
Laravel 的 URL rewrite 設定
重點只在於來自客戶端的 Request,而 Route URL 要怎麼解析就很重要啦
另外記得 root 是要指到 Laravel 專案的 public 目錄喔
location / {
root E:/projects/laravel-demo/public;
index index.php index.html;
try_files $uri $uri/ /index.php?$query_string;
}
完整的 Laravel nginx.conf 設定範例
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#user nobody; | |
worker_processes 1; | |
events { | |
worker_connections 1024; | |
} | |
http { | |
include mime.types; | |
default_type application/octet-stream; | |
sendfile on; | |
keepalive_timeout 65; | |
server { | |
listen 80; | |
server_name localhost; | |
charset utf-8; | |
# root 在這裡統一設定 | |
root E:/projects/laravel-demo/public; | |
location / { | |
index index.php index.html index.htm; | |
# For Laravel URL rewrite | |
try_files $uri $uri/ /index.php?$query_string; | |
} | |
location ~ \.php$ { | |
fastcgi_pass 127.0.0.1:9000; | |
fastcgi_index index.php; | |
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; | |
include fastcgi_params; | |
} | |
location ~ /\.ht { | |
deny all; | |
} | |
} | |
} |
重啟 nginx,輸入 http://localhost 看到 Laravel 專案了嗎?
