Using PHP5-FPM With Apache2 On Ubuntu 12.04 LTS

Want to support HowtoForge? Become a subscriber!
 
Submitted by falko (Contact Author) (Forums) on Fri, 2012-06-08 15:59. :: Ubuntu | Web Server | Apache | PHP

Using PHP5-FPM With Apache2 On Ubuntu 12.04 LTS

Version 1.0
Author: Falko Timme <ft [at] falkotimme [dot] com>
Follow me on Twitter
Last edited 05/02/2012

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 server1.example.com with the IP address 192.168.0.100. 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 root@localhost as well as root@server1.example.com, 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 http://192.168.0.100, 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 127.0.0.1:9000 -pass-header Authorization
        </IfModule>

(To learn more about the FastCgiExternalServer directive, take a look at http://www.fastcgi.com/mod_fastcgi/docs/mod_fastcgi.html#FastCgiExternalServer.)

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 127.0.0.1:9000 -pass-header Authorization
        </IfModule>
[...]

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

<?php
phpinfo();
?>

Now we call that file in a browser (e.g. http://192.168.0.100/info.php):

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 http://192.168.0.100/info.php 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 http://192.168.0.100/phpmyadmin/:

 

8 Making PHP-FPM Use A Unix Socket

By default PHP-FPM is listening on port 9000 on 127.0.0.1. 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 = 127.0.0.1:9000
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 127.0.0.1:9000 -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
        </IfModule>
[...]

Finally reload Apache:

/etc/init.d/apache2 reload

 

9 Links

 

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".


Please do not use the comment function to ask for help! If you need help, please use our forum.
Comments will be published after administrator approval.
Submitted by Dan Jallits (not registered) on Sat, 2014-03-01 17:24.

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.

http://www.php.net/manual/en/install.unix.apache2.php

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.  

http://www.php.net/manual/en/faq.installation.php#faq.installation.apache2

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

 

Submitted by Ivan (not registered) on Sat, 2014-02-01 13:25.

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

Submitted by Infoget (not registered) on Thu, 2014-01-09 15:17.
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 = 127.0.0.1:9000

 and restart:

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

Submitted by hdixon (not registered) on Wed, 2013-09-25 23:25.
Nice how-to. Worked perfect. Thanks much!
Submitted by Dayson (not registered) on Sun, 2013-09-08 10:51.

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

Submitted by subhojit777 (not registered) on Fri, 2013-07-19 05:37.

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 http://blog.code4hire.com/2011/03/setting-up-virtual-hosts-for-apache-on-ubuntu-for-local-development/.

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.

Submitted by Scott Nightingale (not registered) on Mon, 2013-05-20 11:07.

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

Submitted by Charlie (not registered) on Thu, 2012-10-18 17:09.
It would be very helpful if you could comment on step 8 and the TCP overhead. Are there benefits to either approach?
Submitted by Donovan (not registered) on Thu, 2012-10-11 06:52.
Just a note that 12.04 requires you to use multiverse instead of universe.
Submitted by Omar (not registered) on Mon, 2012-06-25 14:45.
Is it mandatory to complete step no. 8 or that's just an option to reduce tcp overhead?
Submitted by admin (registered user) on Tue, 2012-06-26 10:33.
That step is totally optional. It is just an alternative to using TCP.
Submitted by Ralph (not registered) on Fri, 2012-12-21 11:59.
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.