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 phone or even laptop, 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 tested on Raspbian Wheezy operating system, but it should work with any other version of Raspbian as well.
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 sourca packages repository:
deb-src http://mirrordirector.raspbian.org/raspbian/ wheezy main contrib non-free rpiThen 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
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 </Files> </Directory> </IfModule>
Installing APC
Your server will work even without this module, but usage of APC 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 (Alternative PHP Cache) 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:
service apache2 restart && service php5-fpm restart
Add your website source code to /var/www