Install nginx and PHP on Windows

1 minute read

This tutorial is based on Windows 8.1 + nginx 1.5.12 + PHP 5.5.11 but should work with other versions as well.

Setup

Some downloads will have a x86 and x64 version. Choose the same architecture as your Windows installation. You can check this by pressing Windows Key + Pause.

  1. Download nginx (nginx/Windows), PHP (Non Thread Safe) and Visual C++ Redistributable for Visual Studio 2012 (PHP needs this to run)
  2. Install Visual C++ Redistributable for Visual Studio 2012
  3. Extract nginx to a directory
  4. Create a directory called PHP inside nginx’s directory
  5. Extract PHP to the PHP directory you just created
  6. Open the file conf\nginx.conf and uncomment (remove the leading #) the following lines:
    #location ~ \.php$ {
    #    root html;
    #    fastcgi_pass 127.0.0.1:9000;
    #    fastcgi_index index.php;
    #    fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
    #    include fastcgi_params;
    #}
    
  7. Change /scripts$fastcgi_script_name to an absolute path, replacing the backslashes (\) by slashes (/):
    C:/Users/Louis/Desktop/nginx-1.5.12/html$fastcgi_script_name
    
  8. Open the PHP directory
  9. Rename php.ini-production or php.ini-development to php.ini (depending on your needs)
  10. Start PHP
    1. Hold shift, right-click on an empty spot in the PHP directory and choose Open command window here
    2. Type php-cgi -b 127.0.0.1:9000 and press enter
  11. Start nginx
    1. Hold shift, right-click on an empty spot in the nginx directory and choose Open command window here
    2. Type nginx and press enter

You’re done. You have set up and started your webserver with PHP.

Test

To test if everything went right, follow these simple instructions:

  1. Create a file called test.php in the html directory
  2. Edit the file and insert the following PHP code:
    <?php phpinfo(); >
    
  3. Browse to http://localhost/test.php

If you see a page displaying your PHP version and other information everything is working as it should. If you see an error you should check if you did everything right.

Install service

You’re now running nginx and PHP in a command window. You can also run it as a service (in the background). This way Windows will start nginx and PHP automatically at startup, make sure they restart when they crash and you can easily start, stop or restart them yourself.

To install normal programs as a service in Windows I recommend NSSM (the Non-Sucking Service Manager). It’s a very small program and very easy to use.

A tutorial about NSSM can be found here.

Updated:

Leave a comment