NGINX on windows


Recently I tried to install NGINX web server in Windows environment. For the perosn’s who don’t know, NGINX is a web server like Apache, but it’s lightweight and fast. In the production servers, we mostly use NGINX web server sinc,e it has really good load balancing capabilities. To read more about NGINX visit official site http://nginx.org.

Get a comparison between Apache and NGINX refer the below article. https://www.digitalocean.com/community/tutorials/apache-vs-nginx-practical-considerations

So why should we install NGINX over Apache?

Well, there is no big difference if you are just focusing on what you do (work) and you are not into trying new things and keeping  up with new technology. As I experienced, It’s faster than Apache and it’s very light weight, less resource consuming tool. There are even pre bundled packages similar to WAMP/XAMMP for Windows/Linux.

https://bitnami.com/stack/nginx/installer
https://wtserver.wtriple.com/

NGINX for windows

Windows is not the most popular and the best OS to install NGINX, It is widely used in UNIX based servers mostly and only a few people are using in windows. So support and troubleshooting, you have to dig in google a bit. However, the official website provides a good documentation so you can manage most of the things from there.

When Install NGINX on windows, I recommend keeping the existing WAMP/XAMMP installation as it is so, if something goes wrong you will be able to run with Apache. So your work won’t be interrupted because of me. If you do this you should remember to stop Apache service before running NGINX.

What we need

To install NGINX on windows we need several components, some of them may be already there (PHP, MySQL) if you have an existing web server or you need to setup from start.

You may already have MySQL running if you have a WAMP installation. Let’s get started then.

Installing

Create a folder in a different drive other than your OS is installed.

Ex : D://server

NGINX

First Lets extract NGINX components it’s in a zip file. Extract the content to nginx folder.

So structure will be like

D://server/

-nginx/

–nginx.exe

In the conf folder, you have the nginx.conf which contains the configuration of nginx.

PHP

Extract the PHP version(s) to desired folders I’m using both PHP versions here.

D://server/

-php/

-php5/

MySQL

Use the installer and follow the instructions, if you don’t have MySQL installed already. Else if you are using shared DB’s you don’t need this. But, It’s good to have MySQL server running in local Developer machine.

Configuring

So here comes the tricky part. Please note that this is the method I used and there are many possibilities and ways to do one thing. So please experiment and share the knowledge.

According to the above installation configurations for nginx.conf

File Path: D://server/nginx/conf/nginx.conf

Change the Below Lines

http {
 include mime.types;
 default_type application/octet-stream;
 server_names_hash_bucket_size 64;
 server_names_hash_max_size 512;
 include D:/server/nginx/sites-available/*.conf;
 client_max_body_size 1000M;

Why I used them.

server_names_hash_bucket_size  64;

server_names_hash_max_size 512;

The URL requests were exhausted some times because of the length of URL and requests coming are too much with Magento installations, so I had to increase above values.

include    D:/server/nginx/sites-available/*.conf;

This includes our virtual host configurations, so it’s easy to manage rather than managing in a single file.

client_max_body_size 1000M;

Magento needs a lot of resources in installation it fails some times if the body size is small.

server {
 listen 80;
 server_name localhost 192.168.10.128;

....

server_name  localhost 192.168.10.128;

This is the default server configuration I used the IP because you can access it from local network It’s not necessary.

location / {
 root D:/server/www;
 index index.php index.html index.htm;
 }
location ~ \.php$ {
root D:/server/www;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_read_timeout 60s;
}

....

root   D:/server/www;

This is the default web root folder.

index  index.php index.html index.htm;

This is the default files which NGINX will run when we access the location.

  1. fastcgi_pass   127.0.0.1:9000;

  2. fastcgi_index  index.php;

  3. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

  4. include        fastcgi_params;

  5. fastcgi_read_timeout 60s;

1: This is where we configure PHP with NGINX we use fast-cgi to pass requests to PHP from NGINX. So we bind the local host port 9000 to pass our requests.

2: The index file which is initially loaded from a location.

3: Fast CGI script location

4: Fast CGI parameter includes.

5: Timeout for Fast CGI Requests, this needs to be modified since some times Magento requests take time.

These are the basic changes you need to do to run PHP with NGINX.

You can run Many PHP versions easily

Yes, you don’t have to change php version and restart the server with NGINX because it uses Fast CGI  to pass the requests. (You need to start the Fast CGI for your php versions with the port I will discuss it with starting nginx server.) You need to configure your host with the php version you use. For example

NGINX server block is like

server {
location / {
// root parameters
}
location ~ \.php$ {
// php parameters
fastcgi_pass 127.0.0.1:9000; // Change the port here for the desired php version
}
}

Starting the Server

Since we downloaded the NGINX server in a zip file we need to manually start it. Don’t worry it’s not that hard. Since Fast CGI listens to requests from command line interface when we run Fast CGI it has to be remaining open so because of that we use a hidden command line tool.

http://redmine.lighttpd.net/attachments/660/RunHiddenConsole.zip

It was recommended in the official documentation so No need to worry about Virus 

https://www.nginx.com/resources/wiki/start/topics/examples/phpfastcgionwindows/

We need to create 2 .bat files one to start and one to stop the server.

Start-nginx.bat

@ECHO OFF
start D:\server\nginx\nginx.exe
ECHO Starting PHP FastCGI...
D:\server\nginx\bin\RunHiddenConsole.exe D:\server\php\php-cgi.exe -b 127.0.0.1:9000 -c D:\server\php\php.ini
D:\server\nginx\bin\RunHiddenConsole.exe D:\server\php5\php-cgi.exe -b 127.0.0.1:9005 -c D:\server\php5\php.ini
ping 127.0.0.1 -n 1>NUL
echo Starting nginx
echo .
echo ..
echo ...
ping 127.0.0.1 >NUL
EXIT

In the above Code I have started nginx first and then I have started PHP 7 fast cig in port 9000 which is n php folder, and php 5.6 in port 9005 in php5 folder. So both will be listening to requests from NGINX.

When you run this .bat file command line window will open and then it will be closed. You can check if it’s running from Windows task manager process or just typing localhost in the browser. 

Stop-nginx.bat

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

This will simply kill the processes and stop php-cgi and nginx.

Configuring NGINX Server Blocks

As I mentioned earlier We are using individual files to include the server configuration to our virtual hosts, which we included in nginx default configuration file in this line

include    D:/server/nginx/sites-available/*.conf;

This will include all .conf files in sites-available folder. So when we are creating a local website we should create a separate file for each one. In Magento 2 they have already given a sample configuration file (nginx.conf.sample), we can use that with our settings.

In the server block, we are using sample code like below

server {
 listen 80;
 server_name dev.blackwoods.com.au;

 location / {
 root D:/magento/blackwoods/codepool;
 index index.php index.html index.htm;
 try_files $uri $uri/ /index.php?$args;
 }

 location ~ \.php$ {
 root D:/magento/blackwoods/codepool;
 fastcgi_pass 127.0.0.1:9005;
 fastcgi_index index.php;
 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
 include fastcgi_params;
 fastcgi_read_timeout 60s;
 }
}

This will work for any type of virtual host in case it needs special requirements. I tested with WordPress, Magento 1, for Magento 2 use the default configuration file in installation root with your modifications.

In the Bolded line, you should include the port of PHP version you need. As for this example, I have used 9005 since this site is a Magento 1 site and I need to run it in PHP 5.6.

These are the very basic steps to configure NGINX in Windows environment and it’ very easy. There are many online guides and tutorials for configuring NGINX and feel free to contact me if you get into trouble and share your experience.

S.S.Niranga

Senior Tech Lead at Netstarter PTY ltd. Magento Certified Front End Developer, Certified ScrumMaster, Microsoft Certified Professional, Member of British Computer Society

Leave a Reply

  • (will not be published)