Install, Configure & Optimise PHP5 with PHP5-FPM

PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation with some additional features useful for sites of any size, especially busier sites.
Source : PHP-FPM Website

PHP-FPM is far better than the standard mod_php implementation of PHP and also easier to implement that spawn-fcgi. PHP-FPM works like an application that loads and kills PHP instances as needed. Of all the benefits that it offers, reduced memory usage is the most attractive one.

Step 1 : Add the Dotdeb repository

You can skip this step if you have already added the Dotdeb repository while installing NGINX on your Debian Server. The version of PHP and PHP-FPM in the Dotdeb repository is up-to-date and stable. We need to grab the latest version of PHP to benefit from all new updates and bug fixes.

Step 2 : Installing PHP and PHP-FPM

The following command will do it all for you. I have included some popular PHP modules. You can choose and install the modules that you require.

1
sudo apt-get install php5-cgi php5-cli php5-common php5-curl php5-dev php5-fpm php5-gd php5-idn php5-imagick php5-imap php5-json php5-mcrypt php5-memcache php5-mhash php5-ming php5-mysql php5-ps php5-pspell php5-recode php5-snmp php5-sqlite php5-suhosin php5-tidy php5-xcache php5-xmlrpc php5-xsl php-pear php-soap

This command will install PHP-FPM and auto start the service. PHP-FPM will get added to the startup.

Step 3 : Configuring and Optimising PHP

As you will be using PHP-FPM to process PHP scripts, edit the php.ini file that can be found at /etc/php5/fpm/php.ini. There are a lot of PHP settings that you can configure to optimise PHP for performance, speed and security. The following three are a good starting point.

1
2
3
memory_limit = 64M
expose_php = Off
display_errors = Off

Step 4 : Configuring and Optimising PHP-FPM

Remember, we are setting up a low-end (256MB) web server on Shine Servers. We need to conserve as much memory as possible. Edit the file /etc/php5/fpm/pool.d/www.conf

1
2
3
4
5
6
pm = dynamic
pm.max_children = 10
pm.start_servers = 1
pm.min_spare_servers = 1
pm.max_spare_servers = 4
pm.max_requests = 500

This will bring down the number of children processes and free up some RAM.

Step 5 : Restart PHP-FPM

1
service php5-fpm restart

This command will restart PHP-FPM.