Nginx + PHP + MySQL on Windows in 6 minutes

The last time I posted a tutorial on Nginx, there wasn’t a native port of the server available. Riez Opuz posted a link to his Xenstack project on that post that prompted me to write the rest of what I’ve been putting off. It’s a good way to tweak the stack to your own needs.

I tried to leave this as “in 5 minutes”, but then I remembered how long it would take to download MySQL… Even on broadband.

Kevin Worthington had very kindly provided a Cygwin build that ran on Windows, however Nginx now has a Windows build that we can use and this time, we can add MySQL to the list as well. To keep everything compatible, we’ll be using the 32 bit versions for all downloads.

Once you’ve also downloaded Nginx (0.8.53 at the time of this post), head on to the PHP libraries and remember to download the Windows Libraries only (5.3.3 as of today) and select the thread safe version. The first steps are the same with the exception of the download link to MySQL and we need the no-install download.

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 freakin’ huge download so feel free to delete mysql-test, Embedded, sql-bench and folders named debug once unzipped. If you want to minimize the folder even more, you can optionally delete any .pdb files. This would come in handy if you want to deploy the whole ensamble on a thumb drive or package it for a demo application and are really penny-pinching the available storage space.

Once the cleanup is complete, copy my-medium.ini in C:\nginx\mysql\ into my.ini. I think the medium configuration takes care of most uses and, for a moderately busy site, it fares pretty well.

Always try to copy exising 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.

#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 let’s try and run our 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

Onward to setting up PHP


111 thoughts on “Nginx + PHP + MySQL on Windows in 6 minutes

  1. Hello

    I am using this tutorial along with some info on nginx, but I wish to setup slightly different.
    Instead of having php inside c:/nginx/php….
    I want to have it like

    I assume I must alter only fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;,
    However nginx does not provide a manual on its variables which is very irritating.

    Do you know what I should put here?

    Thanks in advance.

    • Hi Jimbo,

      Nginx won’t actually look for the physical PHP installation path at all in this case so you can move to to c:/php without any problems. No need to change anything in nginx.conf at all so you can leave it as-is.

      Reason for this is because Nginx will direct all .php requests to localhost:9000 so it will only be interacting with the fastcgi server.

      Just remember to change the locations in the startup helper script and you’re good to go.

      • Hi Eksith thanks for the reply.

        When you say start up are you referring to PHPini = “c:\nginx\php\php.ini” inside the vb script?
        (changed to c:\php\php.ini)

        Or something else?

        Thanks in advance.

      • Yup. That should do it.

        You may need to adjust permissions if Windows complains (sometimes, files downloaded from the internet will trigger execution warnings), but I don’t think it will really be a problem.

        If something like that happens, just right-click on the php-cgi.exe > select properties > and click “Unblock” if that button is enabled.

  2. Thanks for sharing. I tried on my windows 7 (32 bit) and it works well.

    But when i tried to installed it on my Windows Server 2003, it shows this error
    “C:\nginx\php\php-cgi-exe is not a valid win32 application”

    My windows server is a 32 bit OS. Here is some information regarding my Windows Server:
    1. No IIS Installed.
    2. Windows Server Standard Edition SP 2.

    Can somebody help me to fix this.

  3. Or you could use WT-NMP – Windows Nginx MySql Php stack

    A portable, preconfigured, lightweight, fast and stable server stack for developing php mysql applications on windows, based on the excellent webserver Nginx. Just unzip and start developing!

  4. great article
    I was looking for it about a mount
    now all installation is complete
    but when I paste wordpress folder in c:\nginx\html and run in my browser : “localhost:8080/html/wordpress/wp-config.php
    can’t load it
    I browse localhost:8080 and nginx is load
    is the root folder html
    how can I run wordpress on nginx
    where to put wordpress folder and files
    how to run it
    what is the address to run it to install wordpress
    help me please
    thanks again
    regards :

    • You would need to put the WordPress files in /html and not /html/wordpress, but if the pages aren’t still loading, it sounds like a configuration problem to me. Normally, WordPress would need specific instructions in .htaccess for the Apache web server, but Nginx needs its own settings to be set very differently.

      This page might help with that It details a lot of what needs to be done to get wordpress to work with Nginx.

      Special care should be made of the “General WordPress rules” and the “URL Rewrites / Permalinks” sections as they detail how the pages are sent to PHP for processing.

  5. Pingback: Php | Pearltrees

  6. Pingback: Alternatif Apache: Nginx | Di sini Cisini, di situ Cisitu

  7. wemp server default path,and i need install some program via nginx so i need currect path and where i put my script in wemp server

  8. Very detailed. Thanks. But I’m wordering how to set rewrite rule like Apache .htaccess
    RewriteRule ^index\.php$ – [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]

    Do you know how to set it in Nginx? Thank you.

  9. My problem is that

    ‘ Navigate to MySQL and start “cmd /k CD C:\nginx\mysql\bin\ & mysqld”, 0

    does not start mysql.

    If i do it manually and I write “net start mysql” , then mysql does start.

    if I replace mysqld with net start mysql, like this >>

    ‘ Navigate to MySQL and start “cmd /k CD C:\nginx\mysql\bin\ & net start mysql”, 0

    It does not work.

    I don’t know VB, not one bit. Any ideas?

  10. Could not run command: mysqladmin -u root password newpassword
    Mysql error has display:
    C:\nginx\mysql\bin>mysqld -u root password ‘1234’
    2015-09-10 08:06:43 0 [Warning] TIMESTAMP with implicit DEFAULT value is depreca
    ted. Please use –explicit_defaults_for_timestamp server option (see documentati
    on for more details).
    2015-09-10 08:06:43 0 [Note] mysqld (mysqld 5.6.26) starting as process 6332 …

    Please help me to resolve it
    Thanks and Regards.

  11. How can I start mysql from cigwin. Please post the tutorial.And I am using Aptana studio. How do I configure them both so that I can run my create the templates on Aptana and run them using NMP

  12. Pingback: How to add MySQL to my nginx, which is running on windows 7? [closed] | Questions

  13. Pingback: Chanh Vuong » Blog Archive » Nginx with PHP and MySQL on Windows 7

  14. Pingback: Installing web server on windows 10 – Site Title

  15. Pingback: Proj web2py rasp | Pearltrees

  16. Pingback: Nginx + php + mysql on windows in 6 minutes _ this page intentionally left ugly install java ubuntu

  17. Pingback: Noted Links | Technical Knowledge Sharing

  18. This may very well not be a problem, but that safety net was nice to have.
    The Colorado Rockies Roster: Starting Pitchers – One Month From Spring Training – January 30, 2018West Ham United: Sakho Out, Crystal Palace In – January 29, 2018.
    However, nobody should be surprised if any of them are moved.

    About Latest Posts TRS StaffThe sports you love, the teams you follow, we’ve got it covered.
    Their head-to-head records speaks for itself with the visitors coming out on top just three times out of the last 40 and are without a win against France since 2013.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s