Call us Toll-Free:
1-800-218-1525
Live ChatEmail us

 Sponsors

How to install Nginx, PHP, PHP-FPM and MySQL under Windows with Cygwin

Michel Nadeau, 09-18-2008
Are you using Nginx, PHP, PHP-FPM and MySQL on your Linux servers? Are you totally in love with this setup? I do! But, as a Web developer, I also need a local Web server on my Windows machine for testing. So far, I've always been using the standard "WAMP" setup: Windows, Apache, MySQL, PHP. Why couldn't I run Nginx and PHP-FPM on my Windows machine as well? Today, I will teach you how to achieve it so you can really say goodbye to Apache!

This post covers the installation and configuration of the following products:

- MySQL Win32 (server and client)
- Cygwin
- Nginx under Cygwin
- MySQL (client only) under Cygwin
- PHP and PHP-FPM under Cygwin

IMPORTANT NOTES

- This post was tested on Windows XP Pro SP3. I have no idea if it will work with Windows Vista.

- This post assumes that you are logged on to your Windows machine as the "Administrator" user. Always replace "Administrator" by your actual Windows username.

- This post assumes that you install MySQL Win32 (server and client) in c:\mysql. You can install it where you want, simply replace c:\mysql by your actual installation path.

- This post assumes that you install Cygwin in c:\cygwin. You can install it where you want, simply replace c:\cygwin by your actual installation path.

- To extract ".tar.gz" files under Windows, you can use WinRAR.

- If you're going to install this setup, PLEASE read this post COMPLETELY before installing!



Table of contents

1 - Getting the needed files
2 - Installing MySQL Win32 (server and client)
3 - Installing Cygwin
4 - Installing Nginx under Cygwin
5 - Installing MySQL (client only) under Cygwin
6 - Installing PHP and PHP-FPM under Cygwin
7 - Creating Windows services
8 - Using your new installation
Conclusion
Resources
Annex 1 - Simple benchmark

1 - Getting the needed files

Download these files to your Windows machine:

http://www.cygwin.com/setup.exe
http://dev.mysql.com/get/Downloads/M.../mysql_mirror/
http://sysoev.ru/nginx/nginx-0.6.32.tar.gz
http://dev.mysql.com/get/Downloads/M.../mysql_mirror/
http://php-fpm.anight.org/downloads/...-0.5.9.diff.gz
http://us3.php.net/get/php-5.2.6.tar...om/this/mirror
http://www.sra.co.jp/people/m-kasahr...o-1.6.3.tar.gz

If the PHP-FPM link isn't working, go here:

http://php-fpm.anight.org/download.html

And download the latest version of PHP-FPM for PHP 5.2.6.

2 - Installing MySQL Win32 (server and client)

First of all, unpack mysql-noinstall-5.0.67-win32.zip in c:\ and rename the mysql-5.0.67-win32 folder to mysql.

Then, create the c:\my.cnf file with the following lines:

c:\my.cnf:
[mysqld]
basedir=c:/mysql
datadir=c:/mysql/data

Now, open a Command Prompt window (Start, Run, "cmd" - or Win+R, "cmd") and type:

REM " Change to the MySQL bin directory
cd c:\mysql\bin

REM " Install the MySQL service
mysqld-nt --install

REM " Start MySQL
net start mysql

MySQL is now running. It's a good idea to change the root password. In your Command Prompt Windows, type:

REM " Change the MySQL root password
mysqladmin -u root password pass

By default, the MySQL service will start with Windows. You can change this behavior in Control Panel, Administrative Tools, Services.

3 - Installing Cygwin

Run setup.exe and hit "Next" 6 times (you want "Install from Internet", choose the mirror of your choice). You will arrive at a huge list of packages to choose from. Tip: you can maximize that window.

To select a package, click in the "New" column (it cycles through Skip, Keep, Uninstall, etc.). When a version number is displayed, it will be installed.

Select these additional packages (dependencies for these packages will be automatically selected):

Admin/cygrunsrv
Devel/autoconf
Devel/automake
Devel/bison
Devel/curl-devel
Devel/flex
Devel/gcc
Devel/libiconv
Devel/libmcrypt-devel
Devel/libtool
Devel/libxml2
Devel/libxml2-devel
Devel/make
Devel/patchutils
Devel/pcre
Devel/pcre-devel
Editors/vim
Libs/jpeg
Libs/libmcrypt

Then hit "Next" to install. Tip: you can run setup.exe anytime to add or remove packages.

NOTE: these packages are those I needed for my own Nginx, MySQL and PHP ./configure's options. If your ./configure's options differ from mine, you may need to select additional packages or your ./configure's will fail for missing dependencies.


--

Now that Cygwin is installed, run c:\cygwin\Cygwin.bat to get a shell Window. Your home directory (c:\cygwin\home\Administrator) will be created and prepared.

Copy the following files in c:\cygwin\home\Administrator:

nginx-0.6.32.tar.gz
mysql-5.0.67.tar.gz
php-5.2.6.tar.gz
php-5.2.6-fpm-0.5.8.diff.gz
getaddrinfo-1.6.3.tar.gz

4 - Installing Nginx under Cygwin

Open Cygwin and extract nginx-0.6.32.tar.gz:

cd
tar xvfz nginx-0.6.32.tar.gz
cd nginx-0.6.32

Now, configure Nginx:

./configure --with-http_ssl_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module

And finally, compile and install Nginx:

make
make install

--

Now we will prepare our "www" directory:

mkdir /www

As well as our Nginx configuration:

/usr/local/nginx/conf/nginx.conf:
worker_processes  5;

events {
worker_connections 64; # VERY important. Do not leave 1024 there.
}

http {
include mime.types;
default_type application/octet-stream;

sendfile on;

keepalive_timeout 65;

server {
listen 80;
server_name localhost;

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

location ~ \.php$ {
root /www;

fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
}

You may adjust this configuration file to suit your needs.

5 - Installing MySQL (client only) under Cygwin

We install the MySQL client only to be able to compile PHP with MySQL support.

Open Cygwin and extract mysql-5.0.67.tar.gz:

cd
tar xvfz mysql-5.0.67.tar.gz
cd mysql-5.0.67

Now, configure MySQL:

./configure --without-server

And finally, compile and install MySQL:

make
make install

6 - Installing PHP and PHP-FPM under Cygwin

Open Cygwin and extract php-5.2.6.tar.gz:

cd
tar xvfz php-5.2.6.tar.gz

Then, patch PHP with PHP-FPM:

gzip -cd php-5.2.6-fpm-0.5.7.diff.gz | patch -d php-5.2.6 -p1

Under Linux, you would be ready to configure, make and install PHP, but under Cygwin, there's a little fix to apply...

Applying the getaddrinfo fix

When I first tried to compile PHP with PHP-FPM under Cygwin, I ran into troubles with getaddrinfo. I tried to fix it myself but I only found 50% of the solution. So I contacted Andrei Nigmatulin, the creator of PHP-FPM, and he helped me with this problem. Here's what you need to do:

cd
tar xvfz getaddrinfo-1.6.3.tar.gz
cp getaddrinfo/getaddrinfo.c getaddrinfo/getaddrinfo.h php-5.2.6/sapi/cgi/fpm

Then, edit php-5.2.6/sapi/cgi/fpm/fpm_sockets.c and add the following line after the other includes:

php-5.2.6/sapi/cgi/fpm/fpm_sockets.c:
#include "getaddrinfo.h"

Then, you need to add "getaddrinfo.c" to the FPM_SOURCES variable in php-5.2.6/sapi/cgi/fpm/config.m4:

php-5.2.6/sapi/cgi/fpm/config.m4:
FPM_SOURCES="fpm.c \
fpm_conf.c \
fpm_signals.c \
fpm_children.c \
fpm_worker_pool.c \
fpm_unix.c \
fpm_cleanup.c \
fpm_sockets.c \
fpm_stdio.c \
fpm_env.c \
fpm_events.c \
fpm_php.c \
fpm_process_ctl.c \
fpm_shm.c \
getaddrinfo.c \
xml_config.c \
zlog.c"

Then, you have to rebuild the PHP Makefile:

cd php-5.2.6
./buildconf --force

--

We are now ready to configure PHP:

./configure --enable-fastcgi --enable-fpm --with-mcrypt --with-zlib --enable-mbstring --with-openssl --with-mysql --with-gd --with-jpeg-dir --enable-gd-native-ttf --without-sqlite --disable-pdo --disable-reflection --with-curl --with-iconv

Then, make and install PHP:

make all install

And finally, install the default php.ini file:

cp php.ini-recommended /usr/local/lib/php.ini

You can edit /usr/local/lib/php.ini to suit your needs.

You can also edit /usr/local/etc/php-fpm.conf if you need.

7 - Creating Windows services

The Cygwin application "cygrunsrv" allows you to create Windows services. We definitely want to do this for Nginx and PHP.

First of all, in Cygwin, let's create the services scripts:

/home/Administrator/php.sh:
#!/bin/sh

function handleQuit
{
echo "STOPPING"
su Administrator -c "/usr/local/sbin/php-fpm stop"
exit
}

echo "STARTING"
su Administrator -c "/usr/local/sbin/php-fpm start"

trap "handleQuit" SIGQUIT

echo "WAITING"
while true
do
sleep 1
done

/home/Administrator/nginx.sh:
#!/bin/sh

function handleQuit
{
echo "STOPPING"
su Administrator -c "kill -QUIT $(cat /usr/local/nginx/logs/nginx.pid)"
exit
}

echo "STARTING"
su Administrator -c "/usr/local/nginx/sbin/nginx"

trap "handleQuit" SIGQUIT

echo "WAITING"
while true
do
sleep 1
done

Then, under Windows, use the following batch file to create the services:

cywgin_install_services.bat:
@echo off

echo.
cd c:\cygwin\bin

echo Stopping services...
net stop php
net stop nginx

echo Removing services...
cygrunsrv -R php
cygrunsrv -R nginx

echo Installing services...

cygrunsrv --install php --path /home/Administrator/php.sh --desc "PHP-FPM" -t auto --termsig QUIT --shutdown
cygrunsrv --install nginx --path /home/Administrator/nginx.sh --desc "Nginx" -t auto --termsig QUIT --shutdown

echo These services are now installed:
echo.
cygrunsrv -L
echo.

pause

Voila! Our Windows services are created! But... how does it work?

Let's examine the creation of a service, with some comments...

cygrunsrv

# Name of the service
--install php

# Path of the file to execute
--path /home/Administrator/php.sh

# Description of the service
--desc "PHP-FPM"

# Startup type (auto or manual)
-t auto

# Signal to send to the service when stopping it
--termsig QUIT

# Stop the service when shutdowning Windows
--shutdown

When /home/Administrator/php.sh is invoked by Windows to start the service, we start PHP-FPM as Administrator (because we don't want to run as SYSTEM). Then, we order the script to trap QUIT and to execute the handleQuit() function when it gets one. Then, we wait for something to happen with an infinite while/sleep loop. When a QUIT signal is caught (Windows wants to stop the service), the handleQuit() function is executed: PHP-FPM is stopped, and the script exits.

8 - Using your new installation

To start/stop MySQL on your Windows machine, use the following commands:

REM " Start MySQL
net start mysql

REM " Stop MySQL
net stop mysql

To start/stop PHP on your Windows machine, use the following commands:

REM " Start PHP
net start php

REM " Stop PHP
net stop php

To start/stop Nginx on your Windows machine, use the following commands:

REM " Start Nginx
net start nginx

REM " Stop Nginx
net stop nginx

So if you start MySQL, Nginx and PHP, and that you create this file...

/www/index.php:
print "Hello world!";

...then you should be able to point http://locahost with your Web brower and see "Hello world!".

--

VERY important note about PHP and mysql_connect

Don't do this:

mysql_connect("localhost","root","pass");

It won't work because then PHP wants to use the mysql.sock file (/tmp/mysql.sock or /var/run/mysql.sock) and this file doesn't work under Cygwin.

So instead, do this:

mysql_connect("127.0.0.1","root","pass");

By using the IP address, PHP will connect to MySQL using TCP networking. So far, it's the only limitation I've found with this setup.

Conclusion

That's it! You did it! You've just replaced your old WAMP setup by a faster Nginx+PHP+PHP-FPM (WNMP?) setup! I ran a pretty simple benchmark (see Annex 1 below) with both setups and, on my machine, my new WNMP setup is around 33% faster than my old WAMP setup!

Thanks for reading... all comments are welcome!

Resources

Nginx Web site
http://nginx.net/

Nginx English Wiki
http://wiki.codemongers.com/Main
http://wiki.codemongers.com/NginxCommandLine

And thanks to Andrei Nigmatulin, the creator of PHP-FPM, for his help.

Annex 1 - Simple benchmark

I ran a very simple benchmark on my 2 setups:

- connect to MySQL
- flush test table
- start timer
- insert 10,000 records in test table
- select the 10,000 records from test table
- display each record
- close connection to MySQL
- display timer

The average time for Nginx was 2 seconds while Apache had an average of 3.1 seconds.

If you use this script, don't forget to adjust the root password for mysql_connect! And don't run the test on both setups at the same time because they share the same test table.

SQL:
CREATE DATABASE test;

USE test;

CREATE TABLE test (
pk bigint auto_increment,
a varchar(50),
b varchar(50),
c varchar(50),
d varchar(50),
primary key(pk)
);

index.php:
// Debug function
function debug($d)
{

  print
"<pre>".print_r($d,true)."</pre>";
}

// Include the timer class file
require_once("timer.php");

// Connect to the database
mysql_connect("127.0.0.1","root","pass");

// Use the test database
mysql_select_db("test");

// Remove everything from the test table
mysql_query("delete from test;");

// OK we're ready to begin the test, start the timer
$timer = new Timer();

// Insert 10,000 records in the test table
for ( $i = 1; $i <= 10000; $i++ )
{
 
mysql_query("insert into test (a,b,c,d) values ('a','b','c','d');");
}

// Fetch these 10,000 records
$res = mysql_query("select * from test;");

// Display these 10,000 records
while ( $row = mysql_fetch_object($res) )
{
 
debug($row);
}

// We're done, close the MySQL connection
mysql_close();

// Display the time
print "<hr>";
print
$timer->fetch();

timer.php:
  class Timer {
  var
$s;

  function
Timer() {
   
$this->s = $this->getmicrotime();
  }

  function
fetch($decimalPlaces = 3) {
    return
round(($this->getmicrotime() - $this->s), $decimalPlaces);
  }

  function
getmicrotime() {
    list(
$usec, $sec) = explode(" ", microtime());
    return ((float)
$usec + (float)$sec);
  }
}

Mike Peters, 09-19-2008
Great article. Very detailed!

I'm going to follow these steps and see how quickly I can get it all running on my local machine

Thanks for the tip about localhost => 127.0.0.1, I'm sure that would have cost me a while chasing down

Mike Peters, 10-14-2008
Note - If you want to allow users using subdomains, very important to have this line at the top of your nginx.conf:


    server_name_in_redirect off
;

sikoshi, 10-20-2008
fpm's link is dead

wtf, 10-21-2008
could not start nginx, wtf ?

Filip, 10-25-2008
Done as described but I get following message:

/home/Administrator/php-5.2.6/ext/posix/posix.c:471: error: structure has no member named 'domainname'
make: *** [ext/posix/posix.lo] Error 1

millisami, 10-27-2008
I went through each lines of this post and I was on the right direction until Step 6.
Then at Step 7, comes the windows part and I'm stuck here.

I created both php.sh and nginx.sh files ,with their above respective codes, and created the cygwin_install_services.bat and put the above code. Then I opened the win cmd prompt and execute the .bat file and here is the error:
-------

d:\cygwin\home\millisami>cygwin_install_services.b at

Stopping services...
System error 1060 has occurred.

The specified service does not exist as an installed service.

System error 1060 has occurred.

The specified service does not exist as an installed service.

Removing services...
cygrunsrv: Error removing a service: OpenService: Win32 error 1060:
The specified service does not exist as an installed service.

cygrunsrv: Error removing a service: OpenService: Win32 error 1060:
The specified service does not exist as an installed service.

Installing services...
cygrunsrv: Given path doesn't point to a valid executable
Try `cygrunsrv --help' for more information.
cygrunsrv: Given path doesn't point to a valid executable
Try `cygrunsrv --help' for more information.
These services are now installed:


Press any key to continue . . .
----------------------------------------------
Where lies the problem??
Oh, and I'm on Windows Vista Home edition in my laptop.

Michel Nadeau, 10-31-2008
sikoshi:

You can find PHP-FPM here:

http://php-fpm.anight.org/download.html

http://php-fpm.anight.org/downloads/...-0.5.9.diff.gz

--

wtf and Flip: are you running XP or Vista?

- Mike

Michel Nadeau, 10-31-2008
Hi!

For those that have problems starting services, did you change "Administrator" by YOUR Windows username?

Please re-read the "Important notes" section at the top of the article and adjust the scripts to suit your configuration.

Thanks!

- Mike

Filip, 11-14-2008
When I manualy start nginx and php-fpm from cygwin, all work best!
Thanks for the tutorial!

Kevin Worthington, 01-22-2009
If you do not want to build Nginx for Windows from scratch using Cygwin, I have precompiled setup packages here: http://www.kevinworthington.com/nginx-for-windows/

I built it using Cygwin on Vista, but you do not need Cygwin installed. Thanks.

Mike Peters, 02-25-2009
Important:

Always use:

worker_processes 1

Unless you have more than 1 CPU available to NGinx. Even then, we found NGinx buggy under heavy load where worker_processes is set to anything above 1.

Adam Medeiros, 03-18-2009
An issue I found with the configure parameters for php above is with the --disable-reflection flag. If you use Zend Framework at all, you will need to not pass in --disable-reflection. Zend Framework relies on it. I am curious why it would be left out? Has it been found to be a bad thing? Just curious. Thank you for posting this install guide. Installed great.

Adam Medeiros

JB, 03-26-2009
Great post - however, I am having some problems getting my Wordpress blog to display properly on other machines when accessing my local web server/PC. The Wordpress loads but all the images are missing from the blog and I see a lot of 502 errors in my nginx access.log file.

When I access my blog from my web server e.g. http://localhost/wordpress, everything loads without issue. When I try to access it from http:///wordpress, I see the following in my browser's URL - http://localhost/wordpress/wp-login.php?redirect_to=http%3A%2F%2F192.168.1.4%2Fw ordpress%2Fwp-admin%2F

It seems with this setup of running in a Cygwin environment, everything is getting redirected to localhost which only seems to work properly if I am accessing PHP content from the local server. All regular .htm content with referenced img tags load properly when accessing from the localhost as well as from other PCs on the LAN.

I was able to get phpmyadmin working using the suggestion above to change $cfg['Servers'][$i]['host'] =

to $cfg['Servers'][$i]['host'] = 127.0.0.1.

Here is my nginx.conf:
*************************************************
#server_name_in_redirect off;
worker_processes 1;




events {

worker_connections 64; # VERY important. Do not leave 1024 there.

}



http {

include mime.types;

default_type application/octet-stream;



sendfile on;



keepalive_timeout 65;



server {

listen 80;

server_name localhost;



location / {

root /www;

index index.php index.html;

}



location ~ \.php$ {

root /www;



fastcgi_pass localhost:10000;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME /www$fastcgi_script_name;

include fastcgi_params;

}

}

}
**********************************************

You will see I changed the port of my FAST-CGI listener to 10000 from the default 9000. Other than that, I believe my nginx.conf is pretty much the default. My wordpress php directory is located under /www as well.

Any suggestions?

Chrisco, 04-07-2009
millisami, I hit your same problem, until I remembered (and I suggest it be added here) that the .sh files need to be made executable.

So, in cygwin, where you created php.sh etc., do:
chmod +x *.sh

Chrisco, 04-17-2009
I found that Devel/libjpeg was also needed.
Also, I needed ttydefaults.h. I just copied mine from my ubuntu installation, to c:\cygin\usu\include\sys\ttydefaults.h

Chrisco, 04-17-2009
So far, I cannot get crypt() to work.
I get CRYPT_SALT_LENGTH = 2 (should be 12), and CRYPT_MD5 = 0 (should be 1).

MechanisM, 04-18-2009
My config was:
Windows Vista Ultimate SP1(x86)
nginx-0.7.51.tar.gz
mysql-noinstall-5.1.33-win32.zip (server)
mysql-5.1.33.tar.gz (client)
php-5.3.0RC1.tar.gz
php-5.3.0-cvs20081213-fpm-0.5.10.diff.gz
getaddrinfo-1.6.3.tar.gz

to Cygwin I added some additional libraries dont remember which ones..
But everything installed, and working just fine!!
Thanxxx!!!

Jordan, 05-30-2009
Can this setup be used in a production environment or is it only for a development environment?

My server is windows and i do not want to use Apache or ISS. I'm debating between Nginx and LighTPD, but going more for Nginx.

My website might be a heavy load website with lots of users (might have to eventually to a multiple server setup).

YK, 09-02-2009
Hi,
Has anyone tried to follow these steps recently ? I have tried to install php on Cygwin on a windows 2003 server.

I'm the same versions php, php-fpm and gettaddrinfo as in the tutorial. The only difference is Cygwin.

I get stuck at step 6 because after I use buildconf, I get a lot of 'bad reference' warnings when i try to configure php. Then it freezes.

Does anyone know why I get all these errors and how to solve this ?

JB, 02-22-2010
Hello,
It has been sometime since I last visited this site. I have recently been trying to upgrade my current PHP installation from 5.2.9 to 5.3.0 under Cygwin. My Cygwin version was also updated to 1.7.1. I am getting a lot of make errors when I try to compile php 5.3.0 such as the following:

/php-5.3.0/main/streams/streams.c:183: warning: visibility attribute not supported in this configuration; ignored
/php-5.3.0/main/streams/streams.c: In function `zm_deactivate_streams':

and at the end of the build/make process I get these errors:
/main/streams/cast.c:39: error: conflicting types for `cookie_io_functions_t'
/php-5.3.0/main/streams/cast.c:41: error: conflicting types for `fopencookie'

Anybody seen these errors before or tried to upgrade their php to 5.3.0 or higher with success?

Thanks.

J

Sergey, 07-26-2010
There are a 3 new addon for nginx/Windows.

1. Packet for install nginx as service.
2. Utility for convertation from Apache virtual host to nginx server.
3. Program for manage and control nginx service. Automatic restart in case of freezing service. Stop, start, view status in tray.

NG_SRVINST http://ng_srvinst.att.kiev.ua/
NG_SITE http://ng_site.att.kiev.ua/
NG_TRAY http://ng_tray.att.kiev.ua/

Pheng Vang, 07-27-2010
@YK, you need to tell buildconf which version of autoconf you're using. Try

PHP_AUTOCONF=autoconf-2.13 ./builconf --force

and I was able to use this guide to build php-fpm on my windows 7. Thx

kevin.flexdeveloper@gmail.com, 08-25-2010
Detailed Article Thanks For the Share

Riez, 10-08-2010
this is very great article, detail and easy understood.. thank you

tvs001, 04-19-2011
If you work in win 7, vista:
Edit hosts file in windows\system32\drivers\etc
Add follow line, you can connect with localhost in config (error by windows)
localhost 127.0.0.1

Ps. Sorry aboout my english, hope you understand.

Paul M, 02-23-2012
Thanks for this article.

I really needed pcntl_fork for php under windows. WAMP and XAMPP aren't an option so this had to work.

I got both PHP 5.3.10 and MySQL 5.1.33 working under Cygwin 1.7.1 with a VM under XP. I am not too bothered about nginx as I need this for CLI only.

Cheers for the article! :)
Enjoyed this post?

Subscribe Now to receive new posts via Email as soon as they come out.

 Comments
Post your comments












Note: No link spamming! If your message contains link/s, it will NOT be published on the site before manually approved by one of our moderators.



About Us  |  Contact us  |  Privacy Policy  |  Terms & Conditions