记录如何在Windows环境下配置Nginx和PHP。
官网https://windows.php.net/下载 本地解压后,复制一份php.ini-development改成文件名为php.ini

打开php.ini后就是一顿改,把下面这个配置前面的注释给去咯 【释放注释,这里是php的CGI的设置(第771行)(这一步很有必要,这样才能让nginx的调用php);】
找到 ;cgi.fix_pathinfo=1 释放注释
CMD来到D:\php-8.2.24-Win32-vs16-x64目录就是一顿ctrl+c/ctrl+v输出
开启php的cgi
php-cgi.exe -b 127.0.0.1:9000 -c php.ini
或者:php-cgi.exe -b 127.0.0.1:9000 -c D:\install\php\php-7.4\php.ini
也可以将以下存为批处理start-php-fpm.bat ,直接运行。
@echo off
echo Starting PHP-FPM...
REM 杀死现有的php-cgi进程
taskkill /f /im php-cgi.exe 2>nul
REM 启动PHP-FPM (假设PHP安装在D:\install\php\php-7.4)
cd /d "D:\install\php\php-7.4"
start /b php-cgi.exe -b 127.0.0.1:9000 -c "D:\install\php\php-7.4\php.ini"
echo PHP-FPM started on 127.0.0.1:9000
pause
注意:运行后不要关闭,正常启动后可以在任务管理器的详细信息中看到php-cgi.exe 的进程,这样就可以正确解析你的web主目录里的php文件了。
二:配置nginx
Windows 环境:
server {
listen 8555;
server_name localhost;
root "D:\000\新建文件夹\master\public";
index index.php index.html;
client_max_body_size 200m;
location ~ .*\.(js|woff|ttf|css|font|gif|txt|jpg|jpeg|png|bmp|swf|xls|webp|mp4|asf|asx|m4v|3gp|mpg|wmv)?$ {
expires 12h;
}
location / {
index index.htm index.html index.php;
if (!-e $request_filename) {
rewrite ^/(.*)$ /index.php/$1 last;
break;
}
}
location ~ \.php/?.*$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
set $fastcgi_script_name2 $fastcgi_script_name;
if ($fastcgi_script_name ~ "^(.+\.php)(/.+)$") {
set $fastcgi_script_name2 $1;
set $path_info $2;
}
fastcgi_param PATH_INFO $path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name2;
fastcgi_param SCRIPT_NAME $fastcgi_script_name2;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
Linux 环境:
server {
listen 80;
server_name localhost;
#location / {
# root /var/www/html/public;
# index index.html index.htm index.php;
#}
client_max_body_size 200m;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/html/public;
}
location ~ .*\.(js|woff|ttf|css|font|gif|txt|jpg|jpeg|png|bmp|swf|xls|webp|mp4|asf|asx|m4v|3gp|mpg|wmv)?$ {
root /var/www/html/public;
expires 12h;
}
location ~ .*DGl_KJpW4SDXVjoC6E0YHBvkNbnbNgIjHU_RiuhEPOs?$ {
root /var/www/html/public;
}
location / {
index index.htm index.html index.php;
if (!-e $request_filename) {
rewrite ^/(.*)$ /index.php/$1 last;
break;
}
}
location ~ \.php/?.*$ {
root /var/www/html/public/;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
client_body_timeout 600s;
proxy_connect_timeout 600s;
proxy_send_timeout 600s;
proxy_read_timeout 600s;
send_timeout 600s;
keepalive_timeout 600s;
fastcgi_connect_timeout 75; #链接超时时间
fastcgi_read_timeout 600; #读取超时时间
fastcgi_send_timeout 600; #发送请求超时时间
#设置PATH_INFO SCRIPT_FILENAME,SCRIPT_NAME
set $fastcgi_script_name2 $fastcgi_script_name;
if ($fastcgi_script_name ~ "^(.+\.php)(/.+)$") {
set $fastcgi_script_name2 $1;
set $path_info $2;
}
fastcgi_param PATH_INFO $path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name2;
fastcgi_param SCRIPT_NAME $fastcgi_script_name2;
}
}
浙公网安备 33010602011771号