PHP環境安裝配置:
從官網下載PHP壓縮包后,解壓到指定目錄,在PHP文件目錄下將php.ini-development改名為php.ini
然后修改php.ini中的配置如下:
指定擴展路徑,根據自己實際目錄配置
extension_dir = "C:\Program Files\PHP\php\php-7.3.10-Win32-VC15-x64\ext"
配合nginx相關的CGI配置
enable_dl = On cgi.force_redirect = 0 cgi.fix_pathinfo=1 fastcgi.impersonate = 1 cgi.rfc2616_headers = 1
其他相關擴展,根據自己需求開啟
extension=bz2 extension=curl extension=fileinfo extension=gd2 extension=gettext extension=mbstring extension=exif extension=mysqli extension=odbc extension=openssl extension=pdo_mysql extension=pdo_odbc extension=pdo_pgsql extension=pdo_sqlite extension=pgsql extension=soap extension=sockets extension=sqlite3 extension=tidy extension=xmlrpc extension=xsl
設置時區
date.timezone = Asia/Shanghai
注意:需要用到的配置,如果前面有分號(;)一定要去掉。
Nginx相關配置:
從官網下載Nginx壓縮包后,解壓到指定目錄,在Nginx文件目錄下找到conf文件,然后里面有個nginx.conf
然后修改nginx.conf中的配置如下:
#端口號,沒有被占用的端口號即可
listen 8080;
server_name localhost; location / {
#root是你項目的入口路徑,如果不是用框架的話直接PHP項目目錄即可 root C:/Users/Dolts/phpProjects/mall/public; index index.html index.htm index.php;
#我是ThinkPHP框架,為了在地址欄省去index.php而設置的,你一開始可以不用配置
#配置該項后,http://localhost:8080/index.php?=index/index/hello可以訪問
#也可以直接用http://localhost:8080/index/index/hello訪問 if (!-e $request_filename) { rewrite ^/index.php(.*)$ /index.php?s=$1 last; rewrite ^/(.*)$ /index.php?s=$1 last; } }
location ~ \.php$ {
root C:/Users/Dolts/phpProjects/mall/public;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
這里的“$document_root”就是指前面“root”所指的站點路徑
日志相關配置(根據自己需求配置,不需要可以不管)
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
access_log logs/host.access.log main;
在開啟Nginx服務之前,必須要開啟php-cgi
端口與nginx.conf配置下的fastcgi_pass保持一致
php-cgi -b 127.0.0.1:9000 不要關掉該窗口
然后開啟另一個cmd窗口
start nginx (然后任務管理器中,詳細信息下就會出現php-cgi.exe、nginx.exe)
或者nginx -s reload
常見錯誤:
1.nginx -s reopen/nginx -s reload后 nginx: [error] invalid PID number "" in "/run/nginx.pid"
原因:nginx.pid文件內沒有指定PID(Windows系統每一個進程都有一個PID)
解決方案:start nginx 該命令會自動給出PID
2.頁面請求PHP文件后 An error occurred,HTML文件正常
解決方案:查看php.ini中相關cgi的配置是否正確配置了,是否開啟php-cgi服務 php-cgi -b 127.0.0.1:9000
3.頁面請求PHP文件后 No input file specified,HTML文件正常
解決方案:修改nginx.conf配置
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; 修改如下
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
4.執行開啟nginx服務后 nginx: [error] CreateFile()
原因:它無法創建相關文件,windows家庭版有很多目錄需要管理員身份權限才能進行操作
解決方案:在指定目錄下,自己手動創建相關文件。(如果該目錄無法創建文件,可在桌面創建后copy過去)
擴展:
如果你有多個項目,nginx.conf可以進行拆分
在nginx.conf中加入include config_web/*.conf; (include 文件名稱/*.conf;)
然后在nginx.conf目錄下創建config_web文件夾,在該文件下可以創建多個conf文件
在新建的conf文件中,只需要copy nginx.conf文件中的server部分,根據自己的需求進行相關配置,我的配置如下
server { listen 8081; server_name localhost; #charset koi8-r;
access_log logs/mall.access.log main;
error_log logs/mall.error.log;
location / { root C:/Users/Dolts/phpProjects/mall/public; index index.html index.htm index.php; if (!-e $request_filename) { rewrite ^/index.php(.*)$ /index.php?s=$1 last; rewrite ^/(.*)$ /index.php?s=$1 last; } } #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 C:/Users/Dolts/phpProjects/mall/public; 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; #} }
nginx.conf文件可以換成初始狀態,如果要用到日志的話,日志相關配置要開啟,日志文件無法自動創建的話,手動創建
參考鏈接:https://blog.csdn.net/hl648777580/article/details/79565494