Using PHP5-FPM With Apache2 On Ubuntu 11.10

Want to support HowtoForge? Become a subscriber!
 
Submitted by falko (Contact Author) (Forums) on Thu, 2012-02-09 19:32. :: Ubuntu | Web Server | Apache | PHP

Using PHP5-FPM With Apache2 On Ubuntu 11.10

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

This tutorial shows how you can install an Apache2 webserver on an Ubuntu 11.10 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 we fix the PHP-FPM init script /etc/init.d/php5-fpm which sends a wrong signal to the daemon process when you try to reload it (see http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=645934).

vi /etc/init.d/php5-fpm

Go to the do_reload() function and replace --signal 1 with --signal 12 in the start-stop-daemon line:

[...]
#
# Function that sends a SIGHUP to the daemon/service
#
do_reload() {
        #
        # If the daemon can reload its configuration without
        # restarting (for example, when it is sent a SIGHUP),
        # then implement that here.
        #
        #start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --name $NAME
        start-stop-daemon --stop --signal 12 --quiet --pidfile $PIDFILE --name $NAME
        return 0
}
[...]

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.


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.