PHP FastCGI on Windows¶
Overview¶
NGINX interfaces with PHP via php-cgi.exe on Windows. While Windows is not the recommended platform for production NGINX deployments, this guide covers the setup for development purposes.
Prerequisites¶
- NGINX for Windows
- PHP for Windows
- Ensure
php-cgi.exeis in your PHP installation directory
Quick Start¶
Start PHP FastCGI¶
Create start-php-fcgi.bat:
@ECHO OFF
ECHO Starting PHP FastCGI...
set PATH=C:\PHP;%PATH%
C:\PHP\php-cgi.exe -b 127.0.0.1:9000
Running in Background
To run PHP-CGI hidden, use Windows Task Scheduler or a service wrapper like NSSM (Non-Sucking Service Manager).
NGINX Configuration¶
server {
listen 80;
server_name localhost;
root C:/www;
index index.php index.html;
location / {
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;
fastcgi_param HTTP_PROXY "";
include fastcgi_params;
}
}
Running as Windows Service¶
Using NSSM¶
- Download NSSM from nssm.cc
- Install PHP-CGI as a service:
nssm install php-cgi "C:\PHP\php-cgi.exe" -b 127.0.0.1:9000
nssm start php-cgi
- Install NGINX as a service:
nssm install nginx "C:\nginx\nginx.exe"
nssm set nginx AppDirectory "C:\nginx"
nssm start nginx
Using Task Scheduler¶
- Open Task Scheduler
- Create a task to run at startup under SYSTEM account
- Action: Start program
C:\PHP\php-cgi.exe - Arguments:
-b 127.0.0.1:9000
Troubleshooting¶
| Issue | Solution |
|---|---|
| 502 Bad Gateway | PHP-CGI not running or wrong port |
| "No input file specified" | Check SCRIPT_FILENAME path uses forward slashes |
| PHP crashes | Increase PHP_FCGI_MAX_REQUESTS environment variable |
Recommendation¶
Production Use
For production PHP workloads, Linux with PHP-FPM is strongly recommended over Windows. Consider using WSL2 for development if you need Windows.
See Also¶
- PHP-FPM Configuration (Linux)