There is a new version of this tutorial available for Ubuntu 16.04 (Xenial Xerus).

Using PHP5-FPM With Apache2 On Ubuntu 12.04 LTS

Version 1.0
Author: Falko Timme
Follow me on Twitter

This tutorial shows how you can install an Apache2 webserver on an Ubuntu 12.04 LTS server with PHP5 (through PHP-FPM) and MySQL support. PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation with some additional features useful for sites of any size, especially busier sites.

I do not issue any guarantee that this will work for you!


1 Preliminary Note

In this tutorial I use the hostname with the IP address These settings might differ for you, so you have to replace them where appropriate.

I'm running all the steps in this tutorial with root privileges, so make sure you're logged in as root:

sudo su


2 Installing MySQL 5

First we install MySQL 5 like this:

apt-get install mysql-server mysql-client

You will be asked to provide a password for the MySQL root user - this password is valid for the user [email protected] as well as [email protected], so we don't have to specify a MySQL root password manually later on:

New password for the MySQL "root" user: <-- yourrootsqlpassword
Repeat password for the MySQL "root" user: <-- yourrootsqlpassword


3 Installing Apache2

Apache2 is available as an Ubuntu package. We need the apache2-mpm-worker package which we can install it like this:

apt-get install apache2-mpm-worker

Now direct your browser to, and you should see the Apache2 placeholder page (It works!):

Apache's default document root is /var/www on Ubuntu, and the configuration file is /etc/apache2/apache2.conf. Additional configurations are stored in subdirectories of the /etc/apache2 directory such as /etc/apache2/mods-enabled (for Apache modules), /etc/apache2/sites-enabled (for virtual hosts), and /etc/apache2/conf.d.


4 Installing PHP5

We can make PHP5 work in Apache2 through PHP-FPM and Apache's mod_fastcgi module which we install as follows:

apt-get install libapache2-mod-fastcgi php5-fpm php5

PHP-FPM is a daemon process (with the init script /etc/init.d/php5-fpm) that runs a FastCGI server on port 9000.

Next enable the following Apache modules...

a2enmod actions fastcgi alias

... and restart Apache:

/etc/init.d/apache2 restart


5 Configuring Apache

To make Apache work with PHP-FPM, we need the following configuration:

        <IfModule mod_fastcgi.c>
                AddHandler php5-fcgi .php
                Action php5-fcgi /php5-fcgi
                Alias /php5-fcgi /usr/lib/cgi-bin/php5-fcgi
                FastCgiExternalServer /usr/lib/cgi-bin/php5-fcgi -host -pass-header Authorization

(To learn more about the FastCgiExternalServer directive, take a look at

You can put it in the global Apache configuration (so it's enabled for all vhosts), for example in /etc/apache2/conf.d/php5-fpm.conf (this file does not exist, so you must create it), or you can place it in each vhost that should use PHP-FPM. I want to use PHP-FPM with the default vhost so I open its vhost configuration file /etc/apache2/sites-available/default...

vi /etc/apache2/sites-available/default

... and put the following section somewhere between <VirtualHost></VirtualHost>:

        <IfModule mod_fastcgi.c>
                AddHandler php5-fcgi .php
                Action php5-fcgi /php5-fcgi
                Alias /php5-fcgi /usr/lib/cgi-bin/php5-fcgi
                FastCgiExternalServer /usr/lib/cgi-bin/php5-fcgi -host -pass-header Authorization

Restart Apache afterwards:

/etc/init.d/apache2 restart

Now create the following PHP file in the document root /var/www:

vi /var/www/info.php

Now we call that file in a browser (e.g.

As you see, PHP5 is working, and it's working through FPM/FastCGI, as shown in the Server API line. If you scroll further down, you will see all modules that are already enabled in PHP5. MySQL is not listed there which means we don't have MySQL support in PHP5 yet.


6 Getting MySQL Support In PHP5

To get MySQL support in PHP, we can install the php5-mysql package. It's a good idea to install some other PHP5 modules as well as you might need them for your applications. You can search for available PHP5 modules like this:

apt-cache search php5

Pick the ones you need and install them like this:

apt-get install php5-mysql php5-curl php5-gd php5-intl php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-ming php5-ps php5-pspell php5-recode php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl

Now reload PHP-FPM:

/etc/init.d/php5-fpm reload

Now reload in your browser and scroll down to the modules section again. You should now find lots of new modules there, including the MySQL module:


7 phpMyAdmin

phpMyAdmin is a web interface through which you can manage your MySQL databases. It's a good idea to install it:

apt-get install phpmyadmin

You will see the following questions:

Web server to reconfigure automatically: <-- apache2
Configure database for phpmyadmin with dbconfig-common? <-- No

Afterwards, you can access phpMyAdmin under


8 Making PHP-FPM Use A Unix Socket

By default PHP-FPM is listening on port 9000 on It is also possible to make PHP-FPM use a Unix socket which avoids the TCP overhead. To do this, open /etc/php5/fpm/pool.d/www.conf...

vi /etc/php5/fpm/pool.d/www.conf

... and make the listen line look as follows:

;listen =
listen = /tmp/php5-fpm.sock

Then reload PHP-FPM:

/etc/init.d/php5-fpm reload

Next go through your Apache configuration and all your vhosts and change the lineFastCgiExternalServer /usr/lib/cgi-bin/php5-fcgi -host -pass-header Authorization to FastCgiExternalServer /usr/lib/cgi-bin/php5-fcgi -socket /tmp/php5-fpm.sock -pass-header Authorization, e.g. like this:

vi /etc/apache2/sites-available/default
        <IfModule mod_fastcgi.c>
                AddHandler php5-fcgi .php
                Action php5-fcgi /php5-fcgi
                Alias /php5-fcgi /usr/lib/cgi-bin/php5-fcgi
                FastCgiExternalServer /usr/lib/cgi-bin/php5-fcgi -socket /tmp/php5-fpm.sock -pass-header Authorization

Finally reload Apache:

/etc/init.d/apache2 reload



About The Author

Falko Timme is the owner of nginx WebhostingTimme Hosting (ultra-fast nginx web hosting). He is the lead maintainer of HowtoForge (since 2005) and one of the core developers of ISPConfig (since 2000). He has also contributed to the O'Reilly book "Linux System Administration".

Share this page:

Suggested articles

12 Comment(s)

Add comment


By: Omar

Is it mandatory to complete step no. 8 or that's just an option to reduce tcp overhead?

By: admin

That step is totally optional. It is just an alternative to using TCP.

By: Ralph

Syntax error
FastCgiExternalServer: redefinition of previously defined class "/usr/lib/cgi-bin/php5-fcgi"
Obviously some information in the guide is misleading since you cannot redefine FastCgiExternalServer on each virtual host. This can only be defined once.

By: Donovan

Just a note that 12.04 requires you to use multiverse instead of universe.

By: Charlie

It would be very helpful if you could comment on step 8 and the TCP overhead. Are there benefits to either approach?

By: Scott Nightingale

Thank you.  Excellent tutorial.  I setup php-fcgi the hard way back in 10.04.  Following this was a breath of fresh air.  Went very smoothly, thanks for taking the time to document this.

 Cheers - Scott

By: subhojit777

Thanks for sharing. I followed every steps in this article. I am a Drupal Developer. I can access my sites from localhost.

I created a virtual host from this link

So everything under <IfModule mod_fastcgi.c>
 inside default sites is also copied to the virtual host settings. I was getting an error that you cannot declare FastCgiExternalServer twice. Therefore I created a blank php5-fcgi inside the docroot of my site like this:

FastCgiExternalServer /var/www/mysite/docroot/.cgi-bin/php5-fcgi -socket /tmp/php5-fpm.sock -pass-header Authorization -idle-timeout 3600

But it is still not working and I am getting a 403 access forbidden error.

By: Dayson

To get PhpMyAdmin working with Apache, you also need to do include the following line in /etc/apache2/apache2.conf. Include /etc/phpmyadmin/apache.conf

By: hdixon

Nice how-to. Worked perfect. Thanks much!

By: Infoget

If appears this error:

FastCGI: failed to connect to server "/usr/lib/cgi-bin/php5-fcgi": connect() failed

then check php5-fpm configuration (etc/php5/fpm/pool.d/www.conf) and comment this line:

listen = /var/php5-fpm.sock

and add/uncomment line: 

listen =

 and restart:

 sudo service php5-fpm restart && sudo service apache2 restart

By: Ivan

Don't forget to add multiverse to your package sources. There is no libapache2-mod-fastcgi in standard packages in 12.04

By: Dan Jallits

The recommended setup is to build Apache with the default MPM-Prefork.

We do not recommend using a threaded MPM in production with Apache 2. Use the prefork MPM, which is the default MPM with Apache 2.0 and 2.2.

PHP is glue. It is the glue used to build cool web applications by sticking dozens of 3rd-party libraries together and making it all appear as one coherent entity through an intuitive and easy to learn language interface. The flexibility and power of PHP relies on the stability and robustness of the underlying platform. It needs a working OS, a working web server and working 3rd-party libraries to glue together. When any of these stop working PHP needs ways to identify the problems and fix them quickly. When you make the underlying framework more complex by not having completely separate execution threads, completely separate memory segments and a strong sandbox for each request to play in, further weaknesses are introduced into PHP's system.

If you want to use a threaded MPM, look at a FastCGI configuration where PHP is running in its own memory space.

PHP recommends using FastCGI which is what we are doing by implementing PHP-FPM.