Setup a Raspberry Pi as a Web and Database Server

Late last year (2012) I ordered and received a couple of Raspberry Pi (Model B with 256MB RAM) from Element14.  As part of that order, I also ordered preloaded 4GB SD cards with the linux distro already on them.  When I got them I was really excited to get started with them but for reasons beyond my control, I’ve been too busy to really play around with them much.

Lately however I’ve had more time to pursue some home automation, integration and generally geeky projects that I’ve been wanting to do for a while.  Most of the home automation work I’ve done so far is with Z-Wave based controllers and devices (mostly light switches, but also thermostats and door locks), and while doing research on compatibility and other such things, I saw several mentions of OpenSprinkler for tying sprinkler automation into their systems.  So I decided to check it out and ended up buying one of the OpenSprinkler Pi boards from Ray’s Hobby.

While I was waiting for my board to ship, I thought it might be a good idea to figure out what needed to be done to prep one of my Raspberry Pi boards to build the sprinkler controller.  I found a nice article on wikiHow which details the process of getting the Raspberry Pi updated and getting the system configured for SSH and as a web/database server.  There are several similar articles I found with steps to update the OS.  Borrowing from the wikiHow article, I ran through the following process and made notes along the way.

The Basics

Depending on how old the image on your SD card is, this process can take some time.  In my case, it ended up taking an hour or two and ended up downloading/updating around 300 different packages.  Use the following commands to configure the timezone, then update apt-get, then update all of the installed packages.

sudo dpkg-reconfigure tzdata
sudo apt-get update
sudo apt-get upgrade

With the basics completed, you’ll then want to get the rpi-update tool so you can update the firmware on your Pi. You can use the following commands to get the tool and run it. I noted during the earlier update/upgrade process that I already had the ca-certificates package, but you can safely run the command to install it again just in case.

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

A note on enabling SSH – the wikiHow article I read indicated I needed to manually configure the system to start the SSH daemon, however I didn’t see a boot.rc or boot_enable_ssh so that part of the wikiHow article doesn’t seem to apply. I assume, but did not verify, that ssh is operational on the updated version of the distro, however it could be because the preloaded SD card is simply running Debian 6 for Linux rather than the off-the-shelf Raspbian distro. I am not entirely sure, however I included the steps they listed below just in case. You can easily check to see if sshd is already running by executing ps -aux on the terminal and reviewing the output, looking for sshd in the list.

sudo mv /boot/boot_enable_ssh.rc /boot/boot.rc
sudo shutdown -r now
The Web Server

For a web server, you have several choices here, and the wikiHow article covers both Apache and Cherokee. The choice depends ultimately on what you plan to do with your web server, so I suggest a bit of research here. We can use PHP with Apache for sure, but is Apache too heavy for your needs? Looks like Cherokee can run PHP as CGI. At this point I thought I would need PHP with some backend for persisting configuration data, but I don’t really need to be worried about CGI/FastCGI/any other method for running PHP “applications” because my server will be internal (there are some security/performance considerations when running PHP via CGI).

Ultimately, I decided that I could optimize Apache well enough to run my basic web front end – and also decided that I don’t really need the extended functionality that PHP will offer because I decided against using MySQL for persistent storage and decided to use CouchDB instead (with MySQL the thought was that I’d need to connect using a backend/server-side language but since I am going with CouchDB, I don’t need to because CouchDB exposes an endpoint that client-side JavaScript can use REST and JSON to communicate with). See below for more commentary on database technologies.

After running the install for apache2, I verified the server was operational by hitting the IP address of my Raspberry Pi via Firefox. After the install, the Apache process consumed around 40MB of the available RAM on my Pi, which I am sure I can reduce after performing some optimization to the configuration. I’ve included the commands you would use to install Cherokee below as well, although I didn’t use or test these myself.

Apache:
sudo apt-get install apache2 php5 libapache2-mod-php5
sudo groupadd www-data
sudo usermod -g www-data www-data
sudo service apache2 restart
Cherokee:
sudo apt-get install cherokee
sudo addgroup www-data
sudo apt-get install php5-cgi
(Note: edit php.ini and remove comment for cgi.fix_pathinfo=1)
/etc/init.d/cherokee restart
The Database Server

For database technologies, the de facto standard on the Linux platform is MySQL, which includes libraries for PHP5. This is nice, and I’ve used the stack before to build dynamic web sites, however after some thought I’m almost entirely certain this is more than overkill for a little RPI installation where all I really need is to store some basic application information (things like valves/stations, days to water, length of time to water, etc). This led me to review several other database/persistent storage options including MongoDB, CouchDB, Raven, Redis, Couchbase and a handful of other NoSQL type options.

As I mentioned earlier, I opted to use CouchDB instead of MySQL. My primary reason for going this route was based on the fact that the persistent storage requirements for the solution are lightweight, and while somewhat relational, the relationships themselves can be enforced in code with little overhead. This also removes any requirement (perceived or otherwise) for PHP server side code, as I can interact with CouchDB using a RESTful JSON implementation, allowing me to leverage more client side code (my goal is/was to use HTML/CSS/JavaScript for the front end code, and JSON is, well, native to JavaScript).

If you want to use MySQL, you would install using the follow apt-get command:
sudo apt-get install mysql-server mysql-client php5-mysql
For CouchDB, you use this apt-get command:
sudo apt-get install couchdb

The base CouchDB implementation, when started after installation uses about 40MB of the available RAM on my Raspberry Pi. I do not have readily accessible statistics for MySQL memory usage out of the box but I would assume it is similar, if not more.

Final Prep

The last piece I wanted to get setup on the Raspberry Pi was to enable a wireless connection. The main consideration and reason for doing so is that, despite some work I’ve put into wiring my home (check out my photo diary of that project here) I didn’t wire up the garage, and have little interest in making a special run out there because I don’t spend a whole lot of time there. I figured wireless would be more than good enough, since the communication would be solely for configuration purposes with maybe a little bit of status checking/updates to my phone or other computers.

I ordered a $10 USB wireless adapter from NewEgg – the Edimax EW-7811Un Wireless Nano Adapter which supports 802.11b/g/n up to 150Mbps. I’m impressed with it’s overall size and functionality for the price (make sure if you choose another variety that it isn’t too large to fit in the OpenSprinkler Pi enclosure, if that’s what you’re ultimately going to use it for).

After plugging the adapter into my Pi’s USB port, the Pi restarted itself and automatically configured the device. Which is nice, because you would otherwise have to determine which wireless chipset the adapter was built with, and then find the appropriate package using apt-get to download and configure the device. I did however have to locate and download support for WPA/WPA2 (which is known as ‘wpasupplicant’).

sudo apt-get install wpasupplicant

There are a few changes that need to be made to the configuration after downloading and installing wpasupplicant. I followed the directions listed on a PDF I found online and which I have also pasted here with a link back to the original article. It’s a straight forward process and it was relatively easy to follow. I opted against the steps that indicate you should download the .ko module and replace with what was already on the system, and everything still worked out fine. It may go without saying but case is important in these configuration files and commands. I messed up initially when I capitalized a parameter in the .conf file for wpa_supplication and that caused problems bringing the network up. As a final note, most of these commands will require root access, so you may need to prefix them with ‘su’ or ‘sudo’, and I reference vi in the details here but you can use nano or something else to edit the files as you see fit.

sudo vi /etc/udev/rules.d/70-persistent-net.rules
delete the line referencing wlan0 if it exists
sudo vi /etc/network/interfaces
update the file, adding a section for wlan0, so it looks something like this:
# Used by ifup(8) and ifdown(8). See the interfaces(5) manpage or 
# /usr/share/doc/ifupdown/examples for more information. 
auto lo 
iface lo inet loopback 
iface eth0 inet dhcp 
auto wlan0 
iface wlan0 inet dhcp 
pre-up wpa_supplicant -Dwext -i wlan0 -c /etc/wpa_supplicant.conf -B
sudo vi /etc/wpa_supplicant.conf
add the following to the file, replacing ssid and psk with your settings
ctrl_interface=/var/run/wpa_supplicant 
ctrl_interface_group=0 
ap_scan=2 
network={ 
 ssid="myssid" 
 scan_ssid=1 
 proto=RSN 
 key_mgmt=WPA-PSK 
 pairwise=CCMP 
 group=CCMP 
# choose one of the following 
 psk=”this is my secret phrase” 
 psk=58cfe1c8a36ce8a1f3cd938e281ca1222707df58e27df4745148bfebca7c4197 
}

This file is critical and you should note the following points:

  1. There must be quotes around the SSID.
  2. These parameters relate to WPA2-PSK, even though it says WPA-PSK.
  3. The key (psk) can be specified in quotes or as a hexadecimal string obtained from running wpa_passphrase. If you are concerned about security, you may prefer this but it’s not necessary.
  4. You can make the file unreadable by ordinary users by running chmod 600 wpa_supplicant.conf.

With all of that out of the way, simply reboot your Pi and it should all just work. You should see the blue light on the wireless adapter light up, it will acquire an IP and you can now connect via SSH over wireless and/or hit the web site running on it as well. You can verify the IP address by running ifconfig -a on the Pi. Be sure everything is working as it should before disconnecting your monitor and keyboard from the device if you’ve been accessing it that way.

That’s it! Your Raspberry Pi is now up to date and ready for some serious development work. As time permits I will be adding more info on the software components I use to talk GPIO on the Raspberry Pi, as well as the front-end technologies I plan on using to put the UI together. I will eventually get this project and code posted to GitHub, and may even reach out to Ray’s Hobby about the project. Let us hope I can continue to squeeze in time to keep this project going!

Leave a Comment

You must be logged in to post a comment.