Kirk Miller

So, it has come to this...

Raspberry Pi as a Web Server

Today I set out to accomplish two tasks 1) to cut down the size of the Raspbian install and 2) setup the LAMP (Linux Apache MySQL PHP) stack on it. After setting this up you will quickly see that using RPi to host a dynamic webpage on the internet probably won’t be your best option but there are still functional uses such as a small internal web server or maybe a web interface for your next hardware hacking project using the GPIO pins. Unfortunately in my testing Apache is just too slow on the RPi so we will be using Nginx and SQLite instead of Apache and MySQL. I also did some testing with lighttpd and, while I don’t have hard numbers to back me up, Nginx felt the fastest of all three web servers.

In my quest to put Raspbian on a diet I stumbled across this. It appears CNXSoft has already done what I set out to do so why try to reinvent the wheel. Go here and download the image and load it onto an SD card.

Now that we are booted into our new slim Raspbian image we are going to tweak the performance a bit. Here you can find a well written, easy to follow guide on how to squeeze more performance out of your system. From here on out I will be giving you the step-by-step that will result in you having a functional LAMP stack installed on your RPi.

Install raspi-config.

1
2
$ apt-get install raspi-config git-core wget ca-certificates binutils -y
$ raspi-config

Once installed and you have run raspi-config you will want to select expand_rootfs and after that completes reboot

Install rpi-update, required bins, run the update with a 240/16 memory split, and reboot

1
2
3
4
$ apt-get install git-core wget ca-certificates binutils -y
$ sudo wget http://goo.gl/1BOfJ -O /usr/bin/rpi-update && sudo chmod +x /usr/bin/rpi-update
$ rpi-update 240
$ shutdown -r now

Remove the extra tty’s

1
$ sed -i '/[2-6]:23:respawn:\/sbin\/getty 38400 tty[2-6]/s%^%#%g' /etc/inittab

Enable a 512MB swapfile

Change the size of the swapfile from 100MB to 512MB

1
$ echo "CONF_SWAPSIZE=512" > /etc/dphys-swapfile

Initialize the swapfile

1
$ dphys-swapfile setup

Enable the swapfile

1
$ dphys-swapfile swapon

Optionally you can disable IPv6 but for my deployment I left it enabled.

Install and start Nginx

1
2
$ apt-get install nginx
$ /etc/init.d/nginx start

Install PHP and SQLite, then modify config.

1
2
$ apt-get install php5-fpm php5-sqlite
$ nano /etc/nginx/sites-available/default

Uncomment line:

1
listen 80; ## listen for ipv4.

Add index.php to line with index, to look like:

1
index index.php index.html index.htm

Add or uncomment lines:

1
2
3
4
5
location ~ \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}

Make sure to uncomment the last } as I forgot the first few times. Now we should be able to reload Nginx and PHP should be live.

1
$ /etc/init.d/nginx reload

From here we can generate a PHPinfo page to verify and you should be all set!

1
$ echo "<?php phpinfo(); ?>" > /usr/share/nginx/www/phpinfo.php

Sources:
RPi Webserver
RPi Nginx Webserver
84 MB Minimial Raspbian ARMHF Image
Raspberry Pi Raspbian Tuning

Comments