Installing Apache 2 with PHP FastCGI on Ubuntu 12.10

I've just got a new development computer, and one of the first things to do after installing a fresh Kubuntu 12.10 is setting up the LAMP stack. Rather than install Apache and PHP as a module I decided to run PHP via FastCGI. I don't know if there is any benefit to running PHP this way on a machine used exclusively for development, but since setting it up I have had no problems at all.

For this I am using Kubuntu 12.10, I am sure the same instructions will work for any Ubuntu mix and indeed for any Debian based distro. One thing to note is that the installation and configuration has changed slightly from early Ubuntu 12.04. Anyway on with the instructions.

All the commands need to be run by root.

Install Mysql

apt-get install mysql-server mysql-client

You will be asked to provide a root password for mysql, I left it blank after all this is a development setup, if installing for production choose a long complicated password.

Install Apache

apt-get install apache2-mpm-worker

In a web browser go to http://localhost if all goes well you should see the 'It works!' page from Apache

Install PHP

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

php-fpm is the fastcgi daemon, in previous Ubuntu releases it would run on port 9000, in Ubuntu 12.10 it is run as a unix socket.

Now we need to enable some apache modules.

a2enmod actions fastcgi alias

Configure Apache

To setup Apache to work with php-fpm we need to edit the configuration file for apache. Use your favourite text editor to edit the default site vhost configuration. /etc/apache2/sites-available/default and place the following 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 -socket /var/run/php5-fpm.sock -pass-header Authorization
</IfModule>

Restart Apache

services apache2 restart

Test it out

Create a the file info.php in /var/www and save the following:

<?php phpinfo();

Visit http://localhost/info.php in your browser. If all goes well you should see the purple php info page, check the Server API line, if it says FPM/FastCGI then all is good.

on 12th November 2012

1 Comment


STD
Oct 22, 2013
For me was necessary to create manually php5-fcgi in /usr/lib/cgi-bin/.

Thanks for article.

Add new comment