Raspberry Pi – Creating a Web Server

Overview

This is the list of things I do to create a Raspberry Pi Web Server running the following software:

I usually start with my own headless OS image, but you can also use the standard Raspbian “wheezy” OS. Regardless, the first step in creating a web server is to get a basic Raspberry Pi (RPi) system up and running. Then get yourself connected to it with a command line tool (terminal, SSH, etc.).

On the RPi, we’re going to:

  • install and configure the web software
  • change the host name
  • change the important passwords
  • prepare the system for taking a snapshot

Once that’s done we’ll take a snapshot (disk image) of the resulting system for use as the starting point in creating other web servers. That disk image and the scripts used to produce it are available on the downloads page.

Install the Web Software

There are three parts to installing the web software, all of which can be performed by running the installation script with the sudo ./webserver.sh command.

The first piece of software to install is the web server. We’re going to use lighttpd instead of the more standard Apache because it consumes less resources. Once it’s installed we’ll update some permissions and add the pi user to the group (www-data) that can update the web files. After installing lightpd, you should be able to go to the RPi’s address (ex. 192.168.0.101) in a web browser and see the default web page.

sudo apt-get --yes install lighttpd
sudo chown -R www-data:www-data /var/www
sudo chmod 775 /var/www
sudo usermod -a -G www-data pi

Next we’ll install and configure PHP and MySQL. Use the standard password (raspberry) when prompted by MySQL and accept the defaults, except for changing the password, when configuring MySQL’s security.

sudo apt-get --yes install mysql-server php5-cgi php5-mysql
sudo /usr/bin/mysql_secure_installation
sudo sed -i 's/^;cgi.fix_pathinfo=/cgi.fix_pathinfo=/' /etc/php5/cgi/php.ini
sudo lighty-enable-mod fastcgi-php
sudo service lighttpd force-reload

Now it’s time to install the tool that connects them all together, phpMyAdmin. Do NOT let the installer configure a web server to automaticaly run phpMyAdmin, but do allow it to configure the database. And as before, use the standard password (raspberry) whenever prompted. After installing phpMyAdmin, you should be able to go to 192.168.0.101/phpmyadmin in a web browser and see phpMyAdmin’s main page.

sudo apt-get --yes install phpmyadmin
sudo ln -s /usr/share/phpmyadmin/ /var/www/phpmyadmin

Change the Host Name

In order to change the host name you must first run the hostname command and pass it the new host name. Second, you have to modify the /etc/hostname file and replace the name that’s already there with the new name. And third, you have to modify the /etc/hosts file and replace the name on the line containing 127.0.1.1 with the new name. For example, to change the host name to webpi, you would do:

sudo hostname webpi
sudo echo webpi > /etc/hostname
sudo sed -i "s/^127\.0\.1\.1.*$/127\.0\.1\.1\twebpi/" /etc/hosts

One of the scripts for this article is (host.sh), which will perform these steps for you. It is passed a single argument, the new host name, so you would do:

sudo ./host.sh webpi

Change the Passwords

There are two important passwords you should change in order to keep your server secure, and usually for a headless web server I like to keep them in sync. The first is the password for the “pi” user, which can be changed with the passwd command. The second is the password for the MySQL “root” user, which can be changed by modifying the database with the mysql command. For example, to change the password form raspberry to spiderberry you would do:

passwd
mysql -u root -p"raspberry" -D mysql -e "update user set Password=PASSWORD('spiderberry') where User='root';flush privileges;"

One of the scripts for this article is password.sh, which will perform these steps for you. It is passed two arguments, the old and new passwords, so you would do:

sudo ./password.sh raspberry spiderberry

Prepare the System for Taking a Snapshot

Now that we have a clean, up to date web server, there a few things we can do to prepare the system for taking a snapshot. Specifically, we can install the sfill utility, remove the apt-get cache, clear (fill with 0’s) the empty file system space, clear the swap file, delete the log files, and shut down the device. Here are those commands (sudo ./clean.sh as a script):

sudo apt-get --yes install secure-delete
sudo apt-get --purge autoremove
sudo apt-get clean
sudo sfill -f -ll -z /
sudo swapoff -a
sudo dd if=/dev/zero of=/var/swap bs=1M count=100
sudo swapon -a
sudo rm `find /var/log -type f`
sudo shutdown -h now

Create Disk Image (Mac)

You should now have an SD card containing a RPi web server, so it’s time to make a disk image from it. The first step is to copy the contents of the SD card into a file on your computer, pretty much the exact opposite of burning the original OS onto the SD card:

  • insert the SD card containing the web server
  • use diskutil list to get the SD card’s device (usually /dev/disk1)
  • copy the contents of the SD card into a file
     
    diskutil unmountDisk /dev/disk1
    dd if=/dev/disk1 of=rpi-web-server-os.img bs=1m
    diskutil eject /dev/disk1
  • remove the SD card, we’re done with it

That gives you a disk image the same size as the SD card, probably 2GB. Fortunately, most of that 2GB is empty file system space that we filled with zeroes (cleared) in the preparation step above, so the file is ripe for compression. Running 7ZIP with maximum compression will produce a compressed disk image that’s only about 110MB in size.

7za a -t7z -mx9 rpi-web-server-os.7z rpi-web-server-os.img

One thought on “Raspberry Pi – Creating a Web Server

  1. Pingback: 手機殼

Leave a Reply