How to install Nginx PHP and MySQL on Windows

If you are a web developer, you might want to test your PHP application on your own server before you go live. You will need a running web server to do that. If you want to set up a server on your windows pc, this post is a tutorial to install and set up Nginx PHP and MySql on your Windows PC.

How to install Nginx PHP and MySQL on Windows

How to install Nginx PHP and MySQL on Windows 3

Make sure to follow this directory structure!

Extract the Nginx files to C:\nginx
Extract PHP to C:\nginx\php
Extract MySQL to C:\nginx\mysql

First, let’s configure MySQL

MySQL no-install is a quite large download so feel free to delete mysql-test, Embedded, sql-bench, and folders named debug once unzipped. If you want to minimize the size of the folder, you can optionally delete any .pdb files. This would come in handy if you want to deploy the whole ensemble on a pen drive or flash drive.

Once the cleanup is complete, copy my-medium.ini in C:\nginx\mysql\ into my.ini. Always try to copy existing files before making changes instead of outright renaming them. This way, if something goes wrong with the new configuration, we still have the original handy to start over.

Open up the newly copied my.ini file and change the [client] block to match the following.

[client] #password = your_password 
port = 3306 
socket = c:/nginx/mysql/tmp/mysql.sock

Note the Unix style forward-slashes.

Now in the [mysqld] block in the same file, change to match the following :

port		= 3306
socket		= c:/nginx/mysql/tmp/mysql.sock
basedir		= c:/nginx/mysql
datadir		= c:/nginx/mysql/data
bind-address	= localhost
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M

Now run your MySQL server

Start a new command line window…
Note: If you’re running Windows Vista or above with UAC enabled, you need to right click on the command line link and select “Run as administrator”.

If you get a message saying “Install/Remove of the Service Denied!” when trying to start MySQL later on, then you probably have UAC running, so this step is very important.

Navigate to C:\nginx\mysql\bin\ and run :

mysqld --install-manual

There should be a slight delay followed by a “Service successfully installed”. We then must run :

net start mysql

…And if there are no errors noted, then Congratulations!

Before we proceed, we need to run some housekeeping operations. In the same command line window, run :

mysqladmin -u root password newpassword

Where newpassword is your new MySQL root password. This is an important step toward securing your installation.

Now that we’ve changed our root password enter the following :

mysql -u root -p

Which will give you a password prompt. Enter your newpassword created before. Once you’re logged in, you’re at the MySQL console.

If you need to change your root password at a future date, run MySQL as above type the following :

update mysql.user set password=PASSWORD('new-newpassword') where user='root';

Note that passwords are encoded before storage in the database, so we need to run the PASSWORD function on our new-newpassword. Once that’s done, be sure to run :

flush privileges;

Now we need to remove all the junk that came with the server.

Delete the test databases and anonymous users (Always remember the semicolon at the end!) :

delete from mysql.user where user='root' and host!='localhost';
drop database test;
delete from mysql.db where db='test' or db='test\_%';

And finally flush privileges and quit :

flush privileges; quit;

Now if we need to, we can stop MySQL by running the following (in C:\nginx\mysql\bin\ as an Administrator of course):

net stop mysql

And if we need to remove it from our services entirely, run the following :

mysqld --remove

Setup up PHP with Nginx

Navigate to C:\nginx\php, copy and rename php.ini-production to php.ini.

In this php.ini file, scroll down to (or hit Ctrl + F to find in Notepad) extension_dir = “ext”. Remove the semicolon in front uncomment this line.

Now scroll down to the “Windows Extensions” section and uncomment the following lines :


Also change the include path location :

include_path = "c:\php\PEAR"

This is in case you want to install any PEAR modules (which can be frankly annoying at times), you just need to download, extract and copy to the PEAR folder.

If you have an upload directory on your server, it’s best to avoid any PHP executions on any uploaded files.
example :

location /uploads {
	if $request_uri ~ \.php$ {return 403;}

And finally, uncomment the following line :

log_errors = On

After these changes are done, we can finally get into the Nginx configuration…

Setup Nginx

In C:\nginx\conf\ copy nginx.conf as a backup and edit the original. Find the following lines, uncomment them and change the fastcgi_param :

location ~ \.php$ {
	root		html;
	fastcgi_index	index.php;
	fastcgi_param	SCRIPT_FILENAME	$document_root$fastcgi_script_name;
	include		fastcgi_params;

Create a start-server.vbs file and enter the following :

Dim sh, fcgiPort, PHPini

fcgiPort = ""
PHPini = "c:\nginx\php\php.ini"

Set sh = WScript.CreateObject("WScript.Shell")

' Navigate to the nginx folder and run the server "cmd /K CD C:\nginx\ & start nginx", 0

' Navigate to the PHP folder and start the FastCGI executable "cmd /K CD C:\nginx\php\ & php-cgi.exe -b "& fcgiPort &" -c "& PHPini, 0

' Navigate to MySQL and start "cmd /k CD C:\nginx\mysql\bin\ & mysqld", 0
Set sh = Nothing

Now that we can start our services, we also need a way to stop them. Create a stop-server.vbs and enter the following :

Dim sh, fcgiPort, PHPini

Set sh = WScript.CreateObject("WScript.Shell")

' Navigate to the the nginx folder and shut it down "cmd /K CD C:\nginx\ & nginx -s quit", 0

' Shutdown PHP FastCGI "cmd taskkill /f /IM php-cgi.exe", 0

' Navigate to MySQL and shutdown the server "cmd /k CD C:\nginx\mysql\bin\ & mysqladmin -u root shutdown", 0
Set sh = Nothing

Congratulations, you have successfully installed Nginx PHP, and MySQL on your windows pc.