Chris Rieger | Raspberry Pi Web Hosting

Hosting this website on the Raspberry Pi

I've decided to move my website from the restrictive hosting company I was with and host it from my Raspberry Pi. It seems to be runny fairly snappy using PHP, Nginx and FastPHP. In fact it's showing 100% performance improvements over the previous hosting solution. I thought I'd share my configuration here on how to get the raspberry PI set up for hosting a simple php site such as this one. Firstly, ensure you have a fast SD card (class 10) in your Raspberry Pi. I had mistakenly initially used a class 2 card, which ran less than optimal. As I'm using a 512mb PI, I first did a firmware update to get access to the additional resources. I also configured the governor to overclock the pi to 900Mhz if needed.

#Update pi according to here, https://github.com/Hexxeh/rpi-update
sudo wget http://goo.gl/1BOfJ -O /usr/bin/rpi-update && sudo chmod +x /usr/bin/rpi-update
sudo rpi-update
sudo reboot

#[Optional] Enable root account in case you screw something up.
passwd root
(enter new password)

Whilst setting up the system at home, I used a wifi dongle and configured the PI to automatically connect to my network.

#Install wicd-curses for a nice interface to scan networks.
sudo apt-get update
sudo apt-get install wicd-curses
#Get a decent editor, <3 vim
sudo apt-get install vim
#Set wifi settings
sudo vim /etc/network/interfaces
		auto lo
		iface lo inet loopback

		# Temporarily disable ethernet
		#auto eth0
		#iface eth0 inet dhcp

		auto wlan0
		iface wlan0 inet dhcp
		wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

		iface default inet dhcp

#There are two accesspoints in our house. I've set up both below.
sudo vim /etc/wpa_supplicant/wpa_supplicant.conf
		ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
		update_config=1

		network={
			ssid="HomeWireless-N"
			proto=RSN
			key_mgmt=WPA-PSK
			pairwise=CCMP TKIP
			psk="YOURPASSWORD"
		}

		network={
			ssid="HomeWireless"
			proto=RSN
			key_mgmt=WPA-PSK
			pairwise=CCMP TKIP
			psk="YOURPASSWORD"
		}

Next, I set up FTP access to upload my files.

#Set up FTP
sudo apt-get install vsftpd

sudo nano /etc/vsftpd.conf
# Change the following lines:
anonymous_enable=NO
local_enable=YES
write_enable=YES
# Add to Bottom of File:
force_dot_files=YES

sudo service vsftpd restart
#Allows you to write to /var/www using ftp client.
sudo usermod -a -G www-data pi

Now time to set up Nginx. Nginx is not too different to Apache, but a lot more lightweight.

sudo apt-get install nginx
#Packages depend on the type of Website you've got.
sudo apt-get install php5 php5-fpm php5-cgi php5-cli php5-common
sudo useradd www-data
sudo groupadd www-data
sudo usermod -g www-data www-data
sudo mkdir /var/www
sudo chmod 775 /var/www -R 
sudo chown www-data:www-data /var/www 
sudo chown www-data:www-data /var/www/chrisrieger/

Make a directory for activating the website in Nginx.

sudo vim /etc/nginx/sites-available/chrisrieger
#Basic Config
server
{
    server_name chrisrieger.com;

    access_log /var/log/nginx/chrisrieger.access.log;
    error_log /var/log/nginx/chrisrieger.error.log;

    #Error Redirect
    error_page 404 /404.php;

    ### Default location
    root /var/www/chrisrieger;
    index index.php index.html index.htm;

    ### GZIP
    gzip on;
    gzip_disable "msie6";

    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_buffers 32 16k;
    gzip_http_version 1.0;
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml text/javascript application/javascript text/x-js;

    ### Static content passed through
    location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml)$ {
        expires 5d;
        access_log off;
    }

    #if the URL with .php tacked on is a valid PHP file, rewrite the URL to .php
    if (-f $document_root$uri.php) {
        rewrite ^(.*)$ /$uri.php;
    }

    # use fastcgi for all php files
    location ~ \.php
    {
        try_files $uri =404;
        include /etc/nginx/fastcgi_params;
        keepalive_timeout 0;
        fastcgi_param   SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_pass    127.0.0.1:9000;
    }

    # enforce NO www
    if ($host ~* ^www\.(.*))
    {
        set $host_without_www $1;
        rewrite ^/(.*)$ $scheme://$host_without_www/$1 permanent;
    }

    # deny access to apache .htaccess files
    location ~ /\.ht
    {
        deny all;
    }
}

Next, we symbolically link the file to enable the website.

sudo ln -s /etc/nginx/sites-available/chrisrieger /etc/nginx/sites-enabled/chrisrieger
sudo service nginx restart

Finally, In order for my homepage to display temperature data, I must allow access as follows:

sudo usermod -a -G video www-data

For the curious, I using the following php code to return the temperature data from the PI

shell_exec('/opt/vc/bin/vcgencmd measure_temp');

There we have it. I'm quite happy with my transition to hosting on the Raspberry Pi. It's quick, easy and all i need for my purposes.

© Copyright 2017 by Chris Rieger.
Raspberry Pi CPU: 1% , temp=44.9'C