How to Install Nginx, PHP, and MySQL on Windows 10

Installing a web server using a distribution package like XAMPP, and WinNMP are probably the easiest solution to make your localhost server works. But what if we wanted to make it from scratch? and avoid duplication whenever we install a composer, laravel and other development kit. Also, you’re free to upgrade them whenever a new version comes out.

Table of Contents

  1. Nginx
  2. PHP
  3. MySQL
    1. Using Installer
    2. Using Zipped
  4. Add them to PATH environment
  5. Wrap Up

To get started, you need to install the Microsoft Visual C++ Redistributable for Visual Studio 2019 you can get it from https://visualstudio.microsoft.com/downloads/ at the bottom at Other Tools and Frameworks. If you don’t have that, you’ll get this error message when using the PHP.

The code execution cannot proceed because VCRUNTIME140.dll was not found. Reinstalling the program may fix this problem.

or if you have a lower version of VC_Redist installed, you’ll get this error.

'vcruntime140.dll' 14.0 is not compatible with this PHP build linked with 14.16 in Unknown on line 0 - Google Search

That’s for installing the prerequisite. Let’s now install the latest version of NGINX 1.17.8 + PHP 7.4+ MySQL 8.0.19.

Nginx

To install nginx, you need to download it from http://nginx.org/en/download.html. Select the Mainline version zip files. Currently, it’s nginx/Windows-1.17.8.

Once downloaded, create a folder in your computer. You may follow our naming convention or create your own. In our case we create a folder at C:\WebServer.

After created a folder, create again a folder for our Nginx. It should be C:\WebServer\nginx.

Unzip the downloaded nginx-1.17.8.zip at C:\WebServer\nginx folder.

It’s all good for now, we can discuss later how we activate it.

PHP

To get binaries installation for PHP on Windows, you can grab it at https://windows.php.net/download/.

Then download the VC15 x64 Non Thread Safe zip file, so we can use it alongside with our Nginx and MySQL.

Once downloaded, create a folder C:\WebServer\php and extract the file at C:\WebServer\php

MySQL

There are two ways to install MySQL by its installer or via zip. Both has pros and cons. Installing using its installer is easier and will not allow you to set things. Advantage of using the zipped version is its portability, you can easily transfer everything in a new computer.

Using Installer

To install MySql, you can downloaded the installer version at https://dev.mysql.com/downloads/installer/. Pick the mysql-installer-web-community-8.0.19.0.msi.

Run the installation, and when you are asked to what to install. Just install MySQL Server, other module is not needed.

You’ll then asked to set your MySQL password. Just fill up the form and when finish, the MySQL will run automatically.

We also suggest to start MySQL when windows start.

Using Zipped

You can download the zipped version at https://dev.mysql.com/downloads/mysql/.

Once done, create a folder and unzipped the downloaded file at C:/WebServer/mysql.

Launch a command prompt as a Administrator and we can now initialize the MySQL setup.

cd C:/WebServer/mysql/bin
mysqld --initialize-insecure

You can then run the mysql using this command.

cd C:/WebServer/mysql/bin
mysqld --console

Then login to mysql.

mysql -u root

In MySQL 8.0 and above, the default authentication plugin is caching_sha2_password which not allow you to connect to the mysql using the default native login in php. Most users will get this error message saying “Connection failed: The server requested authentication method unknown to the client”. To enable it back to native, we have to set your root password with mysql_native_password.

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '';
FLUSH PRIVILEGES;

Add them to PATH environment

In order for us to call nginx, php and MySQL straight from the command line or Windows Powershell which is needed for other module that you’d like to use such as Laravel and Composer. We need to add it in the PATH directory.

In Search bar in your Windows taskbar, just search Edit the system environment variables.

At the bottom, Click the Environment Variables.

Then, you can either add both on User Variables for ??? or System variables path.

Look for Path then click Edit.

Now, we have to add these directories. If you use the installer, no need for the C:\Program Files\MySQL\MySQL Server 8.0\bin and if you use zipped, no need for the C:\WebServer\mysql\bin.

C:\WebServer\nginx
C:\WebServer\php
C:\Program Files\MySQL\MySQL Server 8.0\bin
C:\WebServer\mysql\bin

Once done, click OK.

To try it, run Windows Powershell or Command Prompt. Then run these command.

php -v

You should get these output.

PHP 7.4.3 (cli) (built: Feb 18 2020 17:29:57) ( NTS Visual C++ 2017 x64 )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies

You cannot call nginx -t at the command prompt, because it will throw an error that are looking for files in its relative directory. We can discuss this later on how to get rid of this using a batch command file.

nginx: [alert] could not open error log file: CreateFile() "logs/error.log" failed (3: The system cannot find the path specified)
2020/02/27 11:43:49 [emerg] 400#7148: CreateFile() "C:\WebServer/conf/nginx.conf" failed (3: The system cannot find the path specified)
nginx: configuration file C:\WebServer/conf/nginx.conf test failed

Wrap up

Now that we already added them to path. Let’s do some finishing touch.

To make PHP communicate with our MySQL. At C:\WebServer\php, you need to rename the php.ini-production to php.ini file

After renaming it, open the php.ini file, search and uncomment the following by removing the semi-colon “;” to enable the extension. You can also enable other extension that are needed for your program.

extension=mysqli

Running nginx and php-cgi in the batch command line will not allow you to execute the next line in your code, in order for it to run and hide the command prompt windows, you have to download the RunHiddenConsole from lightpd.net.

At C:\WebServer\nginx, unzip the RunHiddenConsole.zip.

Then still C:\WebServer\nginx, we need to create a batch file called C:\WebServer\nginx\myserver.bat. This is where will start and stop our nginx and php-cgi by using a single command line. Add the following code.

@echo off
cd C:\WebServer\nginx


IF "%1" == "stop" (
	GOTO STOPSERVER
)else IF "%1" == "start" (
	GOTO STARTSERVER
)else (
	echo Use these commands:
	echo.
	echo myserver start
	echo myserver stop
)
GOTO END

:STARTSERVER
QPROCESS * | find /I /N "mysqld.exe">NUL
IF "%ERRORLEVEL%"=="0" (
	echo MYSQLD is already running.
)else (
	RunHiddenConsole.exe mysqld --console
	echo MYSQLD is now running.
)

QPROCESS * | find /I /N "nginx.exe">NUL
IF "%ERRORLEVEL%"=="0" (
	echo NGINX is already running.
)else (
	RunHiddenConsole.exe nginx
	echo NGINX is now running.
)

QPROCESS * | find /I /N "php-cgi.exe">NUL
IF "%ERRORLEVEL%"=="0" (
	echo PHP-CGI is already running.
)else (
	RunHiddenConsole.exe php-cgi -b 127.0.0.1:9000
	echo PHP-CGI is now running.
)

echo.
echo To stop, type "myserver stop"

GOTO END

:STOPSERVER

QPROCESS * | find /I /N "mysqld.exe">NUL
IF "%ERRORLEVEL%"=="0" (
	taskkill /F /IM mysqld.exe>NUL
	echo MYSQLD ended successfully.
)else (
	echo MYSQLD is not running
)

QPROCESS * | find /I /N "nginx.exe">NUL
IF "%ERRORLEVEL%"=="0" (
	::nginx -s stop
	taskkill /F /IM nginx.exe>NUL
	echo NGINX ended successfully.
)else (
	echo NGINX is not running
)

QPROCESS * | find /I /N "php-cgi.exe">NUL
IF "%ERRORLEVEL%"=="0" (
	taskkill /F /IM php-cgi.exe>NUL
	echo PHP-CGI ended successfully.
)else (
	echo PHP-CGI is not running
)

:END

Here are the command line that you can use:

  • myserver start – It will start Nginx, and PHP
  • myserver stop – It will stop all instances of Nginx and PHP

But before running the commandline above, we have to edit first the C:/WebServer/nginx/conf/nginx.conf. Inside the location / {} block. Add the following.

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;
}

Once everything is good. Create a file in C:\WebServer\nginx\html\testing.php. Then create a sample PHP codes with MySQL connection checker (Make sure to fill up your MySQL login, the default username is root).

<?php
	echo "PHP works!<br/><br/>";
	
	$servername = "localhost";
	$username = "root";
	$password = "";
	
	// Create connection
	$conn = mysqli_connect($servername, $username, $password);
	
	// Check connection
	if (!$conn) {
	    die("Connection failed: " . mysqli_connect_error());
	}
	echo "Connected successfully";


?> 

Then at the command prompt. Let’s start our server.

myserver start

You should get example output below.

MYSQLD is now running.
NGINX is now running.
PHP-CGI is now running.

To stop, type "myserver stop"

Now to really check its indeed running. Check at your browser and access https://localhost/testing.php.

Leave a Comment