Main Tutorials

Nginx + PHP on Windows

This article shows you how to install and integrate Nginx and PHP on Windows.

Tested

  1. Nginx 1.12.1
  2. PHP 7.1.10
  3. Windows 10

1. Install Nginx + PHP

Basically, just download zip file and extracts it, no installation.

To install Nginx

  1. Visit http://nginx.org/en/download.html
  2. Download nginx/Windows-1.12.2
  3. Extract to C:\nginx-1.12.1

To Install PHP

  1. Visit http://windows.php.net/download/
  2. Download PHP7.1 download VC14 x64 Non Thread Safe
  3. Extract to C:\php7.1.10
Note
This PHP VC14 builds require to have the Visual C++ Redistributable for Visual Studio 2015 installed

2. Integrate Nginx + PHP

Nginx communicates with PHP via php-cgi.exe

2.1 Start PHP at 127.0.0.1:9999

Terminal

c:\php7.1.10>php-cgi.exe -b 127.0.0.1:9999

2.2 Edit Nginx conf file.

C:\nginx-1.12.1\conf\nginx.conf

http {
    include       mime.types;
    default_type  application/octet-stream;

    server {
        listen       80;
        server_name  localhost;

		# Declares here, so that $document_root is able to find php files
		root www;
		
        location / {
            index  index.html index.htm;
        }

		# For PHP files, pass to 127.0.0.1:9999
		location ~ \.php$ {
			fastcgi_pass   127.0.0.1:9999;
			fastcgi_index  index.php;
			fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
			include        fastcgi_params;
		}

    }

}

2.3 Create a C:\nginx-1.12.1\www folder.

2.4 Create a simple PHP file and put it into the www folder.

C:\nginx-1.12.1\www\print.php

<?php
	phpinfo();
?>

2.5 Start Nginx

Terminal

C:\nginx-1.12.1> start nginx

3. Demo

http://localhost/print.php

Done.

References

  1. Nginx official websites
  2. PHP For Windows
  3. PHP-FastCGI on Windows
  4. php-cgi.exe – The application was unable to start correctly
  5. Nginx + PHP – No input file specified

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
18 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
siddharth
5 years ago

not working, php page is not loading

Sabbir Ahmed
5 years ago

nginx is running, but not serving. why???

Graham
1 year ago

Thanks this worked well for me

Henk
2 years ago

Great solution.
Working. So much faster then Apache o. Windows.

Nakorn
3 years ago

thx sir

miki
3 years ago

not working do not try it.

devansh vinayak
4 years ago

It did well !!

But my little Q is how to stop php service???

Harpagophytum
4 years ago

Merci pour votre aide.

Gregory L Bown
4 years ago

This will serve the print.php but not index.php for an application.
To serve your app the first location block needs to be
location / {
try_files $uri $uri/ /index.php;
}

Michael Cooper
5 years ago

In step 2.2 what does this mean? “fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;” is the first SCRIPT_FILENAME a variable?

Thanks,

Roberto
5 years ago

Thanks, its works very well!

TechnOv
5 years ago

how can i install nginx with php-fpm please?

Lin
5 years ago

Thanks a lot! It really helped after a couple of hours of vain search.

Dhimas Kirana
6 years ago

nice tutorial.. thank you..

Derek
6 years ago

does CURL work for you?

Ghigo
6 years ago

This is nice and all, but php-cgi dies after some requests. Have you ever seen this issue?

sucre
5 years ago
Reply to  Ghigo

xxfpm helps on this

Michael Cooper
5 years ago
Reply to  sucre

Do you have the steps to install this on Windows with PHP?