Installing LAMP with FastCGI, PHP-FPM and APC

Source: Link (Author: Jára Vaněk)

If you are looking for a way how to utilize your Rasberry PI as a cheap web server, this article is written just for you. Although Raspberry’s computation power isn’t as good as your laptop or even your phone, it is still enough to host a small website. To be more precise, small website with a low traffic.

In this blog post I would like to show you step by step tutorial how to set up web server with PHP and optionally MySQL on your Raspberry to gain the most possible performance.

This guide has been developed using 2013-02-09-wheezy-raspbian operating system.
Note: The guide is successfully used with Raspbian netinst, February 2015.

Installing Apache with PHP

First you need to switch yourself to root:

sudo su

Install packages for Apache and PHP:

apt-get install apache2-mpm-worker php5-fpm

Installing FastCGI

Then you need to install FastCGI module for Apache, but because this one is not available as binary (.deb) package in Rasbian repository, it is needed to be compiled from the source.

Install all dependencies first:

apt-get install debhelper cdbs apache2-threaded-dev dpatch libtool libapr1-dev pkg-config

Add line to /etc/apt/sources.list to enable sources packages repository:

nano /etc/apt/sources.list
deb-src wheezy main contrib non-free rpi

Then download libapache-mod-fastcgi source package, compile and then install from binary package:

apt-get update
apt-get -b source libapache-mod-fastcgi
dpkg -i libapache2-mod-fastcgi*.deb

Configuring Apache and PHP

Enable mod_actions:

a2enmod actions

Create directory /var/www/fastcgi and make sure it’s accessible for user www-data

mkdir /var/www/fastcgi
chown www-data:www-data /var/www/fastcgi

Edit file /etc/apache2/mods-available/fastcgi.conf

nano /etc/apache2/mods-available/fastcgi.conf

to look following way:

<IfModule mod_fastcgi.c>
 AddHandler fastcgi-script .fcgi
 #FastCgiWrapper /usr/lib/apache2/suexec
 FastCgiIpcDir /var/lib/apache2/fastcgi

Alias /php5.fastcgi /var/www/fastcgi/php5.fastcgi
 AddHandler php-script .php
 FastCGIExternalServer /var/www/fastcgi/php5.fastcgi -socket /var/run/php5-fpm.sock
 Action php-script /php5.fastcgi virtual

# This part is not necessary to get it to work, but it stops anything else from being
 # accessed from it by mistake or maliciously.
 <Directory "/var/www/fastcgi">
 Order allow,deny
 <Files "php5.fastcgi">
 Order deny,allow

Installing APC

Your server will work even without this module, but usage of APC (Alternative PHP Cache) can significantly speed up your PHP applications. I personally tried with default WordPress installation and with APC the response time was about 200 % faster. The APC is a free and open opcode cache for PHP. Its goal is to provide a free, open, and robust framework for caching and optimizing PHP intermediate code. It just mean that the PHP code isn’t parsed everytime is needed again and again, but instead it’s saved in cache and parsed only first time. Which is of course huge performance improvement.

The only drawback is that APC is not very well compatible with common php-cgi setup, because the cache is not shared among all spawned PHP processes. That’s reason I used mod_fastcgi and PHP-FPM instead.

Let’s install APC:

apt-get install php-apc

Installing MySQL

When needed you can optionally install MySQL server. Installation is straightforward:

apt-get install mysql-server mysql-client php5-mysql

Finishing up

Restart Apache and PHP-FPM to reload new configuration:

/etc/init.d/apache2 restart
/etc/init.d/php5-fpm restart

Add your website source code to /var/www


One thought on “Installing LAMP with FastCGI, PHP-FPM and APC”

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s