How to install Apache2 + PHP-FPM on Raspberry Pi

We have prepared a simple tutorial on how to install the Apache web server with PHP-FPM in Raspbian. It takes a few minutes to prepare Raspberry Pi to launch your website.

1. Add a "non-free" component to /etc/apt/sources.list, if not already there and add deb-src source

root@raspberrypi:~# cat /etc/apt/sources.list
deb wheezy main contrib non-free rpi
root@raspberrypi:~# cat >> /etc/apt.sources.list
deb-src wheezy main contrib non-free rpi
root@raspberrypi:~# cat /etc/apt/sources.list
deb wheezy main contrib non-free rpi
deb-src wheezy main contrib non-free rpi

2. Update/upgrade and a good habit is to install the time synchronization and postfix

root@raspberrypi:~# apt-get update && apt-get -y upgrade
root@raspberrypi:~# apt-get install postfix ntp ntpdate

3. Install Apache, php-fpm and other packages according to your needs

root@raspberrypi:~# apt-get install apache2-mpm-worker php5-fpm php-apc

4. Install all dependencies for libapache-mod-fastcgi

root@raspberrypi:~# apt-get install debhelper dpatch libtool cdbs libapr1-dev apache2-threaded-dev

5. Install libapache-mod-fastcgi from source

root@raspberrypi:~# apt-get -b source libapache-mod-fastcgi
root@raspberrypi:~# dpkg -i libapache2-mod-fastcgi*.deb

6. Enable mods rewrite, actions etc. and restart apache

root@raspberrypi:~# a2enmod rewrite ssl actions include
root@raspberrypi:~# service apache2 restart


Do you need more power than RPi?

7. Install MySQL server and PHPMyAdmin if needed

root@raspberrypi:~# apt-get install mysql-client mysql-server php5-mysql phpmyadmin

8. Add group and user for your domain. First uid and gid is 10000 and home user (domain) directory is in /home/

root@raspberrypi:~# addgroup --gid 10000 group001
root@raspberrypi:~# adduser --home /home/ --shell /dev/null --uid 10000 --gid 10000 --disabled-password --disabled-login --gecos '' user001

9. Create structure logs and web directory

root@raspberrypi:~# mkdir /home/       # for Apache logs
root@raspberrypi:~# mkdir /home/  # for PHP logs
root@raspberrypi:~# mkdir /home/        # for your web page
root@raspberrypi:~# mkdir /home/        # for temp
root@raspberrypi:~# mkdir /home/   # for sessions
root@raspberrypi:~# chown -R user001:group001 /home/

10. Add php-fpm.conf for your and edit

root@raspberrypi:~# cp /etc/php5/fpm/pool.d/www.conf /etc/php5/fpm/pool.d/
root@raspberrypi:~# nano /etc/php5/fpm/pool.d/

11. Modify /etc/php5/fpm/pool.d/

; pool name ('www' here)
; Unix user/group of processes
user = user001
group = group001
; The address on which to accept FastCGI requests.
listen = /var/run/
; Set permissions for unix socket, if one is used. In Linux, read/write
listen.owner = user001 = group001
listen.mode = 0666
; Default Value: nothing is defined by default except the values in php.ini ...
; add php.ini admin values
php_admin_value[sendmail_path] = /usr/sbin/sendmail -t -i -f
php_flag[display_errors] = off
php_admin_value[error_log] = /home/
php_admin_flag[log_errors] = on
php_admin_value[upload_tmp_dir] = /home/
php_admin_value[session.save_path] = /home/
php_admin_value[open_basedir] = /home/
php_admin_value[mail.log] = /home/
php_admin_value[memory_limit] = 64M
php_admin_value[post_max_size] = 18M
php_admin_value[max_execution_time] = 60
php_admin_value[allow_url_fopen] = Off
php_admin_value[upload_max_filesize] = 18M
php_admin_value[date.timezone] = Europe/Prague

12. Create Apache virtual host

root@raspberrypi:~# nano /etc/apache2/sites-available/
# eliminate www
<VirtualHost *:80>
    Redirect 301 /
<VirtualHost *:80>
        DocumentRoot /home/
        <Directory /home/>
                Options -Indexes -FollowSymLinks -MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
        # eliminate log warning about ico
        Redirect 404 /favicon.ico
        <Location /favicon.ico>
                ErrorDocument 404 "No favicon"
        # *.php3-5 file extension (enable different extensions in /etc/php5/fpm/pool.d/ first)
        <FilesMatch ".+\.ph(p[345]?|t|tml)$">
                SetHandler application/x-httpd-php
        # FastCGI
        Action application/x-httpd-php /php5-fcgi
        Alias /php5-fcgi /usr/lib/cgi-bin/
        FastCgiExternalServer /usr/lib/cgi-bin/ -socket /var/run/ -pass-header Authorization
        # apache logs
        ErrorLog /home/
        LogLevel warn
        CustomLog /home/ combined

13. Create index.php file in your web home directory and insert phpinfo function into this file

root@raspberrypi:~# nano /home/
<?php phpinfo();?>

14. Create vhost symlink from sites-available to sites-enabled directory and restart apache and php5-fpm

root@raspberrypi:~# ln -s /etc/apache2/sites-available/ /etc/apache2/sites-enabled/
root@raspberrypi:~# service apache2 restart && service php5-fpm restart
[ ok ] Restarting web server: apache2 ... waiting .
[ ok ] Restarting PHP5 FastCGI Process Manager: php5-fpm.

15. Enjoy!

Raspberry Pi Raspbian Apache2 PHP5-FPM


Hello there! Do you know if they make any plugins to protect against hackers?

I'm kinda paranoid about losing everything I've worked
hard on. Any recommendations?