Configuring nginx with PHP on windows

Nginx is a ridiculously fast web server. And it’s small too! The total download size is about1 MegaByte. Because of that, it’s also incredibly popular. It is in fact the second most popular web server right now.

Installing Nginx

Grab it here:

http://nginx.org/en/download.html

Unzip to a folder of your choice and run nginx.exe. If the window opens and appears to hang, it might already be working. Then try opening http://localhost/ in your favorite browser. If you see “Welcome to nginx!” everything is working fine. You can stop the server by pressing CTRL+C.

If however, the window opens and closes immediatly, there might be a configuration issue. Open up a command window (Start, run/search and enter “cmd”). Then try running nginx again. If you see this error:

nginx: [emerg] bind() to 0.0.0.0:80 failed (10013: An attempt was made to access a socket in a way forbidden by its access permissions)

Since the error mentioned permissions, I thought maybe I had to run as administrator, but that changed nothing. Apparently it has to do with port 80 being bound to a system service. If you’re running this as a production server, refer to this solution. If it’s a development server, just use another port. Here’s how you do that:

Open the file called “conf/nginx.conf” and edit the line that reads:
listen 80;
Change it to

listen 8080;

Installing PHP

Ok it’s installed but until you have a scripting engine to go with it, it’s not very useful. How about some good old fashioned PHP?

It’s easy to do, just download the zip package from here and extract to a location of your choice.

http://windows.php.net/download/

Then start the PHP listening service using this command:

php-cgi.exe -b 127.0.0.1:9000

Go back to your nginx configuration file and un-comment these lines by removing the # sign:
location ~ \.php$ {
root c:/www/root/;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

Also notice that I changed two lines! Make those two changes as well.

Now save a file called c:\www\info.php with this content (standard info page):

<?php phpinfo() ?>

Start nginx and bring up this address:

http://localhost:8080/info.php

You should see a page like this!

Troubleshooting

If something isn’t working, you might want to try connecting directly via telnet to PHP to see if it’s listening. If you’re using Windows 7, you’ll need to install Telnet before you can use it.

Also, if you need to restart nginx, you need to kill the process first. To stop PHP however, you can just do CTRL+C.

Posted in Programming

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>