Raspberry Pi web server with Nginx, SQLite and PHP

Today our dear postman delivered the Raspberry Pi Model B, that I was expecting for days. Its basically a tiny computer with a 700Mhz ARM CPU and 256MB RAM. Not much, but it’s enough to run a simple web, if you have minimal load on it, because the hardware is not powerful enough to serve anything public. After several tries with the more conventional LAMP stack I can tell that running Apache and MySQL is too much for the RPi, so instead we will run Nginx as the web server, and SQLite for serving the database for our dynamic content running on PHP5.

Because it’s not running a standard x86 or amd64 CPU, first of all we need an operating system that supports our chip. Fortunately the ARM port of Debian called Raspbian Wheezy is available from the Raspberry Pi Foundation website. But mind you this distro has destop packages installed so the total footprint of it is around 2GB which is quite a waste for our tiny server. You can load this image to an SD card and free up storage by removing the xserver packages by running the following command after you boot up your pi:

sudo apt-get remove purge xserver-xorg

You can do all these steps via SSH (default username:pi, password:raspberry), as the image enables it on boot, so as long as you know the device’s IP address every step here can be done remotely.

Install raspi-config, for easy RPi configuration.

sudo apt-get install raspi-config
sudo raspi-config

After you’ve installed and run raspi-config you will want to select expand_rootfs if you haven’t expanded the partition on your SD before. Restart after saving changes.

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

sudo 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
sudo rpi-update 240
sudo reboot

Remove the extra tty’s

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

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

sudo nano /etc/dphys-swapfile

Change “CONF_SWAPSIZE=100″ to “CONF_SWAPSIZE=512″ in the file, save and quit nano.

Initialize the swapfile

sudo dphys-swapfile setup

Enable the swapfile

sudo dphys-swapfile swapon

Install and start Nginx

sudo apt-get install nginx
sudo /etc/init.d/nginx start

Install PHP and SQLite, then editthe nginx config file

sudo apt-get install php5-fpm php-gd php5-sqlite
sudo nano /etc/nginx/sites-available/default

Uncomment the following line:

listen 80; ## listen for ipv4.

Add index.php to the enabled index files, so the line looks like this:
index index.php index.html index.htm

Add the following lines:

location ~ \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;

Now Nginx and PHP should be all set up and live, reload Nginx config

sudo /etc/init.d/nginx reload

Now create a PHPinfo file so we can test if PHP and Nginx are set up correctly

sudo echo "" > /usr/share/nginx/www/phpinfo.php

Now try to install Drupal 8 on it! :)


  1. Pingback: Installing Ruby on Rails to the Raspberry Pi | Monoport

  2. Rodrigo

    Hi man, grate tutorial, thank you so much for that. But I’m not sure of certains points, for example I don’t understand what is the “dphys-swapfile” for.
    (I’m runing an wheezy raspbian distro in my pi whit the defaults options).

    I’m actually working in a new project witch I’ll glade to talk you about. But I’m using mysql apache2 and maybe is not the best solution for making a web server because it seems the raspy just can’t with them (that’s normal ).

    I know very well Drupal (drupal 7) and I know he likes to use a LOT of ram and querys, but I’m surprised to see how well it’s works here.

    Thank you again.

  3. Ashley

    Great tutorial. I installed this on my android box (mx2) – I skipped the pi bits. Everything seemed to work except it was a bit sluggish. I proceded to install php-apc. After enabling and reloading nginx/php5-fpm I noticed an amazing improvement.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>