
![]() |
How to install Nginx, PHP, PHP-FPM and MySQL under Windows with CygwinMichel Nadeau, 09-18-2008 |
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:
basedir=c:/mysql
datadir=c:/mysql/data
Now, open a Command Prompt window (Start, Run, "cmd" - or Win+R, "cmd") and type:
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:
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):
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:
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:
tar xvfz nginx-0.6.32.tar.gz
cd nginx-0.6.32
Now, configure Nginx:
And finally, compile and install Nginx:
make install
--
Now we will prepare our "www" directory:
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:
tar xvfz mysql-5.0.67.tar.gz
cd mysql-5.0.67
Now, configure MySQL:
And finally, compile and install MySQL:
make install
6 - Installing PHP and PHP-FPM under Cygwin
Open Cygwin and extract php-5.2.6.tar.gz:
tar xvfz php-5.2.6.tar.gz
Then, patch PHP with PHP-FPM:
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:
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:
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:
./buildconf --force
--
We are now ready to configure PHP:
Then, make and install PHP:
And finally, install the default php.ini file:
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:
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:
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.
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...
# 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:
net start mysql
REM " Stop MySQL
net stop mysql
To start/stop PHP on your Windows machine, use the following commands:
net start php
REM " Stop PHP
net stop php
To start/stop Nginx on your Windows machine, use the following commands:
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:
...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:
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:
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:
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:
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:
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 |
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 |
server_name_in_redirect off;
![]() |
sikoshi, 10-20-2008 |
![]() |
wtf, 10-21-2008 |
![]() |
Filip, 10-25-2008 |
/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 |
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 |
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 |
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 |
Thanks for the tutorial!
![]() |
Kevin Worthington, 01-22-2009 |
I built it using Cygwin on Vista, but you do not need Cygwin installed. Thanks.
![]() |
Mike Peters, 02-25-2009 |
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 |
Adam Medeiros
![]() |
JB, 03-26-2009 |
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://
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 |
So, in cygwin, where you created php.sh etc., do:
chmod +x *.sh
![]() |
Chrisco, 04-17-2009 |
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 |
I get CRYPT_SALT_LENGTH = 2 (should be 12), and CRYPT_MD5 = 0 (should be 1).
![]() |
MechanisM, 04-18-2009 |
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 |
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 |
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 |
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 |
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 |
PHP_AUTOCONF=autoconf-2.13 ./builconf --force
and I was able to use this guide to build php-fpm on my windows 7. Thx
![]() |
Riez, 10-08-2010 |
![]() |
tvs001, 04-19-2011 |
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 |
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! :)
|

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