nginx + PHP on Windows in 5 minutes

Update November 7, 2010

There’s now an updated version of this tutorial which also covers incorporating MySQL.


If you’ve ever needed a very fast, stable, no frills, web server to serve up some pages on a home system, then look no further than nginx. The server is rock solid and gets the job done. And the setup and configuration is unmatched in simplicity for other servers of similar capability.

Nginx is native to the UNIX platform, so you’ll need to get a precompiled version or install Cygwin. I opted for the former because there’s already a package available by Kevin Worthington that works very nicely.

Download the stable package and install it. Because of the Cygwin configuration, it will install to c:\nginx.

Then download the latest PHP Windows binaries (not the installer) and extract all files to c:\nginx\php. We will be using php-cgi.exe because of the nginx fast-cgi capability. Make sure the path is c:\nginx\php\php-cgi.exe during the installation.

Almost there…

Go into c:\nginx\conf and uncomment or modify the following lines in nginx.conf.

location ~ .php$ {
  root           html;
  fastcgi_pass   127.0.0.1:9000;
  fastcgi_index  index.php;
  fastcgi_param  SCRIPT_FILENAME c:/nginx/html/$fastcgi_script_name;
  include        fastcgi_params;
}

Then, in the same folder, edit start-nginx.bat to include the following line :

@ECHO OFF
c:\nginxnginx.exe
c:\nginx\php\php-cgi.exe -b 127.0.0.1:9000 -c c:\nginx\php\php.ini
ping 127.0.0.1 -n 1>NUL
echo Starting nginx
echo .
echo .
echo .
ping 127.0.0.1 >NUL
EXIT

Now edit stop-nginx.bat and add the following lines :


@ECHO OFF
taskkill /f /IM nginx.exe
taskkill /f /IM php-cgi.exe
EXIT

It’s not a perfect solution, but works for non-production applications.

That should be it!

If you need to hide that ugly command prompt during startup, just create two files in conf (alongside start-nginx.bat) and enter the following code :

In launch.js :


var objShell = WScript.CreateObject("WScript.Shell");
var result = objShell.Run("cmd.exe /c start-nginx.bat", 0);

// Give some startup time
WScript.Sleep(3000);

// Navigate to homepage
objShell.Run("http://localhost");

In shutdown.js :


var objShell = WScript.CreateObject("WScript.Shell")
var result = objShell.Run("cmd.exe /c stop-nginx.bat", 0)

Now to startup nginx with fast-cgi PHP, just double-click launch.js. To stop, double-click shutdown.js.

You can make yourself a HTML Application to run these JavaScripts and build a basic control panel at a future date.

Update 12/08

Changed the php.ini file location to an absolute path.

Changed the stop-nginx.bat commands to taskkill instead of multiple process -k lines (you can never tell how many instances there may be of php-cgi.exe, so it’s impractical to do it the old way).

Note: Copying entire blocks is recommended as parts of the code is hidden by my display theme. However all the text is there. Hightlighting the whole thing will ensure that no parts are left behind.

Advertisements

97 thoughts on “nginx + PHP on Windows in 5 minutes

  1. Pingback: nginxでwordpressのサイトを構築する - Hello, idea

  2. It’s bad practice to use:

    fastcgi_param SCRIPT_FILENAME c:/nginx/html/$fastcgi_script_name;

    Try this one:

    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

  3. Hi, im using Windows and i have a CakePHP project runing with apache, but i want to change it to nginx. I cant understand how to configurate nginx ’cause everyone use it on ubuntu :(… Can you help me or guide me? (PS: sorry if my english its too bad xD)

  4. There is a notable limitation in this recipe. The php-cgi, automatically, exited after some time. It will be better to show us how to keep php-cgi works.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s