Published on Oct 1, 2011

Setup PHP-FPM with APC on Ubuntu 10.04 for Faster Performance

PHP-FPM with APC

PHP-FPM with APC

PHP has been one of the most successful server side dynamic scripting language of current world. Many successful web applications and web sites including facebook use this language along with MySQL and Memcached for datastore tasks. On the other hand Fast CGI is an optimized version of CGI protocol for interacting with the web-server and PHP language interpreter. You’ll need to install PHP-FPM for enabling the PHP interpreter to talk to the web server program you’ve installed earlier. PHP-FPM (PHP FastCGI Process Manager) is an alternative PHP FastCGI implementation with some additional features useful for sites of any size, specially busier sites.

These features include:

  • Adaptive process spawning
  • Basic statistics (ala Apache’s mod_status)
  • Advanced process management with graceful stop/start
  • Ability to start workers with different uid/gid/chroot/environment and different php.ini (replaces safe_mode)
  • Stdout & stderr logging
  • Emergency restart in case of accidental opcode cache destruction
  • Accelerated upload support
  • Support for a “slowlog”
  • Enhancements to FastCGI, such as fastcgi_finish_request() – a special function to finish request & flush all data while continuing to do something time-consuming (video converting, stats processing, etc.)

… and much more.

Now let me introduce you with the Alternative PHP Cache (APC) which is a free and open opcode cache for PHP. Its used for caching and optimizing PHP intermediate code which eventually will boost performance of your website. So we will be installing and configuring them in our server and our server will be capable of serving dynamic PHP applications, will be performance optimized and much faster than before. So lets install and configure PHP-FPM and APC in our server:

Deploy PHP with FastCGI Process Manager (FPM)

In order to deploy PHP applications, you will need to implement the following step by step instruction to allow your web server to properly handle and serve pages that contain PHP code.

  1. Use the following commands to add and update the necessary repositories:
    sudo -s
    add-apt-repository ppa:nginx/php5
    apt-get update
  2. Now install PHP necessary packages with the following commands:
    sudo apt-get install php5 php5-dev php5-suhosin
    sudo apt-get install php-pear php5-cgi php5-cli php5-curl
    sudo apt-get install php5-gd php5-imagick php5-mcrypt
    sudo apt-get install php5-fpm php-apc php5-memcache php5-mysql
    The suhosin package will fix several security issues with your php installation. The curl extension is needed if you need to query other web services or API’s using PHP. The gd and imagick libraries are needed for different image content processing in PHP and is required by many PHP frameworks and CMS. The mcrypt library supports a wide variety of block algorithms such as DES, TripleDES, Blowfish (default), 3-WAY, SAFER-SK64, SAFER-SK128, TWOFISH, TEA, RC2 and GOST in CBC, OFB, CFB and ECB cipher modes and is used by many frameworks for encryption of important data and for authentication systems. The mysql php libraries are necessary to connect with MySQL server from PHP and work with database queries. The memcache library is needed for PHP to talk to memcache servers which is used mainly to optimize performance and reduce database load in highly busy traffic sites.
  3. Now Let’s Configure the FastCGI Behaviour by editing the file /etc/php5/fpm/pool.d/www.conf with vi using the following command:
    sudo vi /etc/php5/fpm/pool.d/www.conf
    The file has comments that explains the function of each configuration setting, you just need to un-comment the specific configuration by removing the semicolon “;” at the beginning of the line containing that setting. But if you are somewhat lost, then here is a sample configuration profile optimized for a server with Quad Core Processor and 512MB RAM:
    [www]
    listen = 127.0.0.1:6000
    listen.allowed_clients = 127.0.0.1user = www-data
    group = www-datapm = static
    pm.max_children = 10
    pm.max_requests = 100request_terminate_timeout = 30
    You can copy this configuration, remove everything in the file by pressing “ESC” and then typing :1,$d and paste the copied content from here into vi by right clicking on top of vi editor, then save and exit vi (press “ESC” then type :x and then hit enter). There are many other parameters available, but I preferred them to stay in the default setting, so I just didn’t mention them in the configuration profile. With this configuration, php-fpm will listen to localhost port 6000 whereby your server program nginx is already configured to forward .php file requests to port 6000 to let php-fpm handle the .php files.
  4. Now let’s configure the PHP settings by editing the file /etc/php5/fpm/php.ini issuing the following command:
    sudo vi /etc/php5/fpm/php.ini
    This file also has comments explaining each configuration setting, if you are not sure then keep the default settings. Just put the memory_limit value to a higher value specially if you want to use php applications like wordpress or similar applications that requires a lot of memory.
  5. APC will be enabled by default if you’ve installed it from repository as described here. So you don’t have to enable it from the php.ini. But if you’ve installed php-apc by compiling from source then you might have to un-comment the line extension=apc.so in the php.ini file to enable apc.
  6. Now let’s check if php is working properly by creating a phpinfo.php file in your “adminpanel” directory, because it’s not a good practice to let others know your php configurations. So let’s create the file using vi:
    sudo vi /var/www/example.com/adminpanel/phpinfo.php
    Inside the file, type or copy paste the following simple php code:
    echo phpinfo();
    Now save the file by pressing “ESC” then typing :x and then hitting Enter. Now let’s check if it’s working or not. Type http://your_ip_address/adminpanel/phpinfo.php in your browser address bar and hit enter to see the output. You should see a page describing your php settings and available php extensions.
  7. Now it would be great if you could see how your apc cache is working. Download this file, extract and upload the apc.php file to your server’s “adminpanel” folder using an sftp client like FileZilla. Then type the url in your address bar http://your_ip_address/adminpanel/apc.php. What you’ll see is a graphical output of the APC Cache Statistics and some settings info.

Articles in this Step by Step VPS Setup Guide

Getting Started with VPS – The Beginners Tutorial
Introductory concepts on web servers and virtual private servers. Introducing you to the world wide web from a technical point of view.

Shared Hosting, VPS, Dedicated and Cloud Servers
Comparing the popular server hosting solutions. Basic understanding of shared, dedicated, vps and cloud servers.

Deploy a Linux VPS Server using Linode
Tutorial on deploying your linux distribution in your VPS server and setting up hostname and initial configurations

Setup NginX Web Server (Not Apache!) on Ubuntu 10.04
Analyzing why NginX web server is better for you instead of the mostly used Apache. Demonstration on how to setup and configure NginX on Ubuntu server.

Enable HTTPS (HTTP+SSL) in NginX Web Server
Tutorial on enabling secure http connections for NginX web server. Demonstrating how to self sign your SSL certificate for the VPS server.

NginX Password Protect Web Directory
Helping you to protect specific web directories with passwords for restricting access to administration section or important private files that need to be accessed through the web.

Setup PHP-FPM with APC on Ubuntu 10.04 for Faster Performance
Installing and Configuring PHP5 with Fast CGI Process Manager along with the excellent op-code cache solution APC for faster and optimized PHP backend.

Setup MySQL with PHPMyAdmin on Ubuntu 10.04
Setting up MySQL database server and configuring for better performance in low memory environment. Also setup a web based database management front-end named PHPMyAdmin.

Configure Domains and Subdomains in your VPS Running NginX
Tutorial on DNS setup for adding additional domains and subdomains. Also configuring NginX for handling each domains/subdomains.

Domain Emails for Free Using Google Apps
Setup domain specific email addresses without installing any email server in your VPS. Google Apps seems to be a better solution in this regard.

Author: Masum
Tags: , , , ,

1 Comment + Add Comment

  • Marcus

    June 16, 2013 at 10:49 am

    I had some problems with the FastCGI PM (FPM). Now, after uninstalling it with Debian 6.0 the pages runs very fine.

Leave a comment

Sponsored