Install NginX and PHP 5.3.3 with PHP-FPM, MySQL and APC
As of July 22nd 2010 PHP-FPM is a part of PHP 5.3.3 core's trunk. FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation with some useful additional features. In this tutorial I will compile NginX 0.8.47 and PHP 5.3.3 with FPM, MySQL, Suhosin, APC and Imagick on Ubuntu 10.04.
I will not explain how to configure NginX here. You can find a lot of resources about that on the nginx wiki or my previus post Install NginX with PHP-FPM in Ubuntu 10.04.
In order to compile these programs you will need the following dependencies (most of them should already be installed):
apt-get install htop binutils cpp flex gcc libarchive-zip-perl libc6-dev libcompress-zlib-perl m4 libpcre3 libpcre3-dev libssl-dev libpopt-dev lynx make perl perl-modules openssl unzip zip autoconf2.13 gnu-standards automake libtool bison build-essential zlib1g-dev ntp ntpdate autotools-dev g++ bc subversion psmisc
apt-get install libmysqlclient-dev libcurl4-openssl-dev libjpeg62-dev libpng3-dev libxpm-dev libfreetype6-dev libt1-dev libmcrypt-dev libxslt1-dev libbz2-dev libxml2-dev libevent-dev libltdl-dev libmagickwand-dev imagemagick
Now let's create a folder in which we can play:
mkdir ~/lemp cd ~/lemp
and start downloading the sources:
wget http://nginx.org/download/nginx-0.8.47.tar.gz wget http://us2.php.net/get/php-5.3.3.tar.gz/from/this/mirror wget http://download.suhosin.org/suhosin-patch-5.3.3-0.9.10.patch.gz wget http://download.suhosin.org/suhosin-0.9.32.1.tar.gz wget http://pecl.php.net/get/APC-3.1.3p1.tgz wget http://pecl.php.net/get/imagick-3.0.0.tgz
Decompress them:
tar zxvf nginx-0.8.47.tar.gz tar xzvf php-5.3.3.tar.gz tar xzvf APC-3.1.3p1.tgz tar xzvf imagick-3.0.0.tgz tar xzvf suhosin-0.9.32.1.tar.gz gunzip suhosin-patch-5.3.3-0.9.10.patch.gz
And now let's install them one by one. I like to put everything I compile by myself in one place, the /opt directory. By doing this I know where to find things like configuration files and libraries.
cd nginx-0.8.47/ ./configure \ --prefix=/opt/nginx \ --conf-path=/opt/nginx/conf/nginx.conf \ --http-log-path=/var/log/nginx/access.log \ --error-log-path=/var/log/nginx/error.log \ --pid-path=/var/run/nginx.pid \ --lock-path=/var/lock/nginx.lock \ --user=www-data \ --group=www-data make make install
cd .. wget http://nginx-init-ubuntu.googlecode.com/files/nginx-init-ubuntu_v2.0.0-RC2.tar.bz2 tar --use-compress-program bzip2 -xvf nginx-init-ubuntu_v2.0.0-RC2.tar.bz2 mv nginx /etc/init.d chmod +x /etc/init.d/nginx update-rc.d -f nginx defaults
Do not forget to edit the files and change the path of the executable and configuration path.
You sould also create the file /etc/logrotate.d/nginx in order to compress the logs.
vi /etc/logrotate.d/nginx
And add the following lines:
/var/log/nginx/*.log {
weekly
missingok
rotate 52
compress
delaycompress
notifempty
create 640 root adm
sharedscripts
postrotate
[ ! -f /var/run/nginx.pid ] || kill -USR1 `cat /var/run/nginx.pid`
endscript
}Now let's apply the latest Suhosin patch and compile PHP:
cd php-5.3.3 patch -p 1 -i ../suhosin-patch-5.3.3-0.9.10.patch ./buildconf --force ./configure \ --prefix=/opt/php5 \ --with-config-file-path=/opt/php5/etc \ --with-curl \ --with-pear \ --with-gd \ --with-jpeg-dir \ --with-png-dir \ --with-zlib \ --with-xpm-dir \ --with-freetype-dir \ --with-t1lib \ --with-mcrypt \ --with-mhash \ --with-mysql \ --with-mysqli \ --with-pdo-mysql \ --with-openssl \ --with-xmlrpc \ --with-xsl \ --with-bz2 \ --with-gettext \ --with-fpm-user=www-data \ --with-fpm-group=www-data \ --enable-fpm \ --enable-exif \ --enable-wddx \ --enable-zip \ --enable-bcmath \ --enable-calendar \ --enable-ftp \ --enable-mbstring \ --enable-soap \ --enable-sockets \ --enable-sqlite-utf8 \ --enable-shmop \ --enable-dba \ --enable-sysvmsg \ --enable-sysvsem \ --enable-sysvshm make make install
Because the executables are not in a standard location I added the following lines to /etc/bash.bashr so that the new path is always loaded:
echo 'if [ -d "/opt/php5/bin" ] && [ -d "/opt/php5/sbin" ]; then PATH="$PATH:/opt/php5/bin:/opt/php5/sbin" fi' >> /etc/bash.bashrc
And also apply the path to the current session:
export PATH="$PATH:/opt/php5/bin:/opt/php5/sbin"
The next step is to configure PHP-FPM:
mkdir /var/log/php-fpm chown -R www-data:www-data /var/log/php-fpm cp -f php.ini-production /opt/php5/etc/php.ini chmod 644 /opt/php5/etc/php.ini cp /opt/php5/etc/php-fpm.conf.default /opt/php5/etc/php-fpm.conf cp -f sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm chmod 755 /etc/init.d/php-fpm update-rc.d -f php-fpm defaults
If you installed these somewhere custom like me you should also modify the init file for php-fpm (/etc/init.d/php-fpm:.
prefix=/opt/php5 ##(in this case only, maybe you have it somewhere else) exec_prefix=${prefix} php_fpm_BIN=${exec_prefix}/sbin/php-fpm php_fpm_CONF=${prefix}/etc/php-fpm.conf php_fpm_PID=/var/run/php-fpm.pid
Create /etc/logrotate.d/php-fpm with the following:
/var/log/php-fpm/*.log {
weekly
missingok
rotate 52
compress
delaycompress
notifempty
create 640 www-data www-data
sharedscripts
postrotate
[ ! -f /var/run/php-fpm.pid ] || kill -USR1 `cat /var/run/php-fpm.pid`
endscript
}Now let's install the latest APC:
cd ../APC-3.1.3p1 /opt/php5/bin/phpize ./configure --enable-apc --enable-mmap make make test make install
You will have to add the extension in php.ini. We will do that after we install everything.
Also install the Suhosin extension:
cd ../suhosin-0.9.32.1 /opt/php5/bin/phpize ./configure --enable-suhosin make make test make install
And the imagick extension:
cd ../imagick-3.0.0 /opt/php5/bin/phpize ./configure --with-imagick make make test make install
and now you can add all of them to php.ini
vi +920 /opt/php5/etc/php.ini
extension = suhosin.so extension = imagick.so extension = apc.so apc.enabled=1 apc.shm_size=64 apc.rfc1867 = on
This is it! You can now start the servers:
/etc/init.d/php-fpm start /etc/init.d/nginx start
Attached you will find an example of php-fpm configuration.
| Attachment | Size |
|---|---|
| php-fpm.conf | 11.13 KB |




Vlad, thanks for writing this
Vlad, thanks for writing this tutorial!
I am having an issue getting PHP-FPM to start. When I run the 'sudo /etc/init.d/php-fpm start' command it stalls and then fails. It says...
Starting pfp-fpm ................................ failed.
You have to either set the
You have to either set the right permissions for your .pid file and/or .sock, or to adjust the init file (/etc/init.d/php-fpm) to include the corect paths to the fpm files:
You also must make sure the
You also must make sure the pid file exists. I had to touch the file to make it work:
touch /var/run/php-fpm.pid
Thanks!
I wanted to say thank you for writing this up. It really helped me to get nginx running with php-fpm which I was having trouble doing after reading about 20 other write-ups as well as spending hours of wasted time trying to figure it out on my own, which just did not work.
Thanks again!
I tried them all, and after
I tried them all, and after failing so many times, I compiled everything by myself.
Problem parsing php
I finally installed everything, started every process but there's no way to parse php i guess.
in my nginx.conf
location ~ .php$ { root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; #also tried with /html$fastcgi_script_name; include /opt/nginx/conf/fastcgi_params; }on the browser i see only The page you are looking for is temporarily unavailable.
Please try again later., wich is the 50x.html
First of all check the
First of all check the php-fpm.conf file to see if it is listening on a socket or tcp:
If you used my example attached to this article it'a a socket so your it should be:
fastcgi_pass unix:/var/run/php-fpm.sockOr whatever your sock file is...
If not you can check the location regexp, and change it into location ~ \.php$ (Maybe the dot needs to be escaped).
Then you can try using absolute paths, so instead of just html, something like /opt/nginx/html.
Starting php-fpm
when i try to start php fpm i get:
[ALERT] [pool www] pm.min_spare_servers(0) must be a positive value
Aug 20 16:46:39.110439 [ERROR] failed to post process the configuration
I have the following working
I have the following working configuration on a 512MB RAM machine:
So just do as the error said: increase the number of min_spare_servers. Also I think the number of start servers needs to be equal or greater than max_spare_servers.