Setup Nginx and PHP on Windows

Serving from home has never been easier

0 Comments

If you are a windows user and looking to host your own website then Nginx is a great light-weight server application you can use. When you pair this with PHP and MySQL you can have a great and productive site. Today I will be walking you through setting up nginx and php as services.

Note

If your reading this article and are trying to setup a webserver for your company then please remember, production webservers require maintenance and monitoring. In most cases you are better off working with a web-host, did I mention we offer managed web-hosting, as they will take care of that work for you.

Nginx

Nginx Server Folder As of this writing, the latest version of Nginx is 1.9.2 and can be downloaded from nginx.org. The link you will want to click is labeled nginx/Windows-1.9.2. Once you have downloaded the zip file that contains the applicaiton it is best to extract it to the root of your OS drive, normally c:\nginx-1.9.2.

Now we will create a junction to the c:\nginx-1.9.2 folder and name it nginx, this will make upgrading easier.

mklink /J c:\nginx c:\nginx-1.9.2

When you go to upgrade your version of nginx, unzip it next to the previous installation and then delete the previous junction and create a new one. This stops you from having to change the service or settings files that refer to the c:\nginx location.

rmdir c:\nginx

Edit the config files

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include             mime.types;
    default_type        text/html;
    sendfile            on;
    keepalive_timeout   65;

    server {

        #Uncomment and edit the line below if you want to use a custom domainname
        #server_name    your.domain.com;

        listen          80;
        root            c:/nginx/html

        location ~ \.php$ {
            fastcgi_pass     127.0.0.1:9000;
            fastcgi_index    index.php;
            fastcgi_param    SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include          fastcgi_params;
            access_log       logs/php.access.log;
            error_log        logs/php.error.log;
        }
    }
}

Firewall Rules

We now need to add an exception to the Windows firewall to have the service able to communicate, to do so we need to open a command prompt as administrator—right click the command prompt application and choose “run as administrator”—and run:

netsh advfirewall firewall add rule name="Nginx Server" dir=in action=allow protocol=TCP localport=443,80 profile=private

Test the server

Welcome to Nginx Now that Nginx is setup we can start up the service and see if it responds correctly. To do so navigate to the nginx folder and double click the nginx.exe file. Now you should be able to load the browser of your choice to goto http://127.0.0.1 and view your own webserver! The image to the left is what you will see if it is up and running.

PHP

PHP InstallationPHP is a powerful interpreted language that allows your pages to respond to user queries with data or customize pages based on user input. If you plan on accepting data from users or creating custom pages based on user input then you will want this. Just as with Nginx you will want to download the latest version from the PHP site, unless you have a specific reason not to, and then unzip it into you nginx folder. Be aware that there are normally four different releases on the download page and you should grab the Non Thread Safe Zip version that fits your computer/server, be it x86 or x64.

Firewall Rules

We now need to add an exception to the Windows firewall to have the service able to communicate, to do so we need to open a command prompt as administrator—right click the command prompt application and choose “run as administrator”—and run:

netsh advfirewall firewall add rule name="PHP Server" dir=in action=allow protocol=TCP localport=9000 profile=private
netsh advfirewall firewall add rule name="PHP Server" dir=in action=allow protocol=UDP localport=9000 profile=private

Test the PHP Interpreter

info.php Location To test if PHP is working correctly, you should create a new page in the html folder of your nginx installation, name it info.php and put the following text in that file.

<?php phpinfo(); ?>

PHP Info Next we will start up the php interpreter via command-line with the following command and then navigate to the info.php page we made to see if the php interpreter is working. If all is setup correctly then you will see a page like the one shown on the left.

c:\nginx\php\php-cgi.exe -b 127.0.0.1:9000 -c "c:\nginx\php\php.ini-production"

Creating the Services

NSSM Application If you plan to keep the web-server up all the time or just want it to run at boot even if no-one logs in then you will want to setup Ngix to run as a service. The best way to do this is with the help of NSSM - the Non-Sucking Service Manager and download the zip package. Once you have the zip file extract the appropriate nssm.exe file, again win32 or win64 will depend on your computer, and then place the nssm.exe file in the c:\windows\system32 folder as seen on the left.

Nginx Service

NSSM Nginx Service To setup the nginx service we need to open a command prompt and then issue the following command and fill out the application and details tabs as shown on the left.

nssm install nginx

PHP Service

NSSM PHP Service To setup the php service we need to open a command prompt and then issue the following command and fill out the application and details tabs as shown on the left.

nssm install php

Wrap-up

Now you should have a fully working webserver that is php capable. Stay tuned for the next post in this series that will show you how to setup MySQL and integrate it into this setup. A set that is great for sites like Wordpress blogs! Remember, if you enjoy the posts feel free to buy me a pint!

- By: Last Updated:

Comments

Small ad here
Select a size at which to preview the size