Install Nginx, PHP 7.2, MariaDB on Windows

There is an abundance of tutorials in case you want to install nginx, PHP and MariaDB on Linux but when it comes to installing these tools on Windows, tutorials are either outdated or scattered. Anyway, let’s see how we can setup Nginx, PHP and MariaDB on Windows.

Download Nginx

This is fairly easy. Just head to Nginx’s Download page and download the zip file which says nginx/Windows-1.15.2 which is the current version at the time of writing this tutorial. Once you have downloaded it, extract the zip file to C:\nginx folder or anywhere you would like. But keep it in the root directory for simplicity sake.

Download PHP

Head to PHP’s download site for Windows and download the NTS version of PHP 7.2.9 (the latest version available at the time of this tutorial). Non Thread safe version of PHP is usually recommended if you are going to use with Nginx.

Extract all the files to C:\nginx\php directory.

Download MariaDB

Visit the MariaDB download page and get the zip file for the latest MariaDB version. And yes, I know there is an installer but we are going to stick to the zip file for this one.

Setup Nginx

The configuration file for Nginx can be found in C:\nginx\conf directory named as nginx.conf. There is only one executable file namely nginx.exe in the main directory. Unlike Apache, Nginx doesn’t install itself as a Windows Service. Bummer, right?

But fret not. We can convert Nginx into a Windows Service. But before we do that, let’s take a quick look at the command line shortcuts for Nginx’s executable file.

nginx -s stop           fast shutdown
nginx -s quit           graceful shutdown
nginx -s reload         config change, graceful shutdown of old process and start new process with changed config
nginx -s reopen         re-opening log files

Out of these, we would be using nginx -s stop and nginx -s reload a couple of times in this tutorial.

Download WinSW

WinSW is a Windows Service wrapper which allows us to use any executable as a Windows Service. Grab the binary file from the Releases page. There are two binary files for .NET 2+ and .NET 4+ versions. Assuming that you are using Windows 8 and above, grab the .NET4 executable.

Copy the executable into the C:\nginx directory and rename it as nginxservice.exe.

Create a file named nginxservice.xml and paste the following code in it.

Now launch Powershell or Command centre in Administrator mode and run the following command

nginxservice.exe install

PS: When running from Powershell, if you want to run an executable which is in the current directory, use ./ in front of the file name or it won’t run. This is not required if you are running an executable from the current directory in Command Center.

To start the service run the command: net start nginx and to stop the service use net stop nginx. You can also see Nginx listed as a service if you launch the Services app from Start Menu>>Search>>Type Services.

Just run http://localhost in your browser to see if Nginx is running successfully. You should get the following page which tells us that the installation was successful.

Your HTML and other files will go by default in C:/nginx/html directory. Time to setup PHP now.

Setup PHP

PHP under Nginx runs as a CGI application. Like Nginx, PHP also doesn’t run as a Windows service. Therefore, we follow similar steps as we did for Nginx. Copy the WinSW executable to C:\nginx\php directory and rename it as phpservice.exe.

First, rename php.ini-production to php.ini. Also create an empty directory php under C:/nginx/logs. Then create a file named php-stop.cmd in php directory and add the following line to it
taskkill /f /IM php-cgi.exe
Now create an empty file named phpservice.xml and copy the following lines to it

I have used port number 9999 above. You can use any port number you like to start PHP under. Now you can start PHP service with the command net start php.

Now that we have setup PHP, let’s do a bit of a configuration so that PHP and Nginx can work together.

First make a backup of C:\nginx\nginx.conf file and then open it and uncomment the following lines. Also change the fastcgi_param as indicated below.
location ~ .php$ {
root html;
fastcgi_pass 127.0.0.1:9999;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

Also, change the following line

location / {
root html;
index.html index.htm;
}

to

location / {
root html
index.php index.html index.htm;
}

If you are using .htaccess files, then uncomment the following lines as well.

location ~ /.ht {
deny all;
}

This will ensure that PHP code is loaded properly inside Nginx. root html means that you need to keep your content in C:\nginx\html directory. You can change it accordingly. If you want to use a directory that’s outside the Nginx folder then you need to specify the full pathname.

Now we also need to make some adjustments to php.ini file that we had saved earlier. Open C:\nginx\php\php.ini and scroll down to the Dynamic Extensions section and uncomment the following lines

extension=bz2
extension=curl
extension=gd2
extension=gettext
extension=imap
extension=mbstring
extension=exif
extension=mysqli
extension=openssl
extension=sqlite3

You can uncomment more extensions depending upon your needs. One last thing is to increase the memory limits. You can do so by adjusting the following variables

post_max_size = 8M
upload_max_filesize = 2M
memory_limit = 128M

Adjust the values accordingly according to the system/server you are using and what works for you.

Now that both Nginx and PHP are setup, run the following commands on Powershell/Commandline

net restart nginx
net restart php

Setup MariaDB

This is the final phase of our tutorial. Run the following command on Command Prompt/Powershell after entering the directory C:\nginx\mariadb\bin

mysql_install_db.exe --datadir=C:\nginx\mariadb\data --service=MariaDB --password=secret

This command sets the data directory for databases as well as sets up the MariaDB service and the root password. And that’s it. MariaDB is working now.

And that’s all folks. Now you have a Nginx/PHP/MariaDB based server setup on your Localhost.

To start or stop any service use the commands

net start/stop nginx
net start/stop mariadb
net start/stop php

You can also do that by launching Services app by searching for it in the Start menu.

And that is the end of the tutorial. If you have any questions, shoot.

Source: Stackexchange and Nginx forum for pointers on setting up Nginx and PHP as windows services.

Leave a Reply

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