The Raspberry Pi as a Server

This page contains instructions for setting up your Raspberry Pi as a web server in the context of the CU CSFP RPi Project.

A playlist of video segments of this session is available on YouTube

Prerequisites

We will be starting with the the Raspbain OS configured as we left it at the end of the first session. If you are using your own RPi or SD Card, you may download a fresh version (sha1) of this configuration and flash it via the normal means.

Network Setup

Before we can use our RPi as a server, we need to give it a permanent location on our network. Normally, when you connected your Pi to a network, it uses the Dynamic Host Configuration Protocol (DHCP) to automatically obtain an IP address and related network information. This address may change every time we reboot the Pi or reconnect to the network. A server, however, must have a constant IP address so that we can always connect to it on the network.

Linux Network Configuration

This step is covered in the "Web Server - Part 2: Network Setup" video segment on YouTube.

Before we start reconfiguring the network, it's best to shut down our network connection. This will minimize the likelihood of conflicts between the new and old configuration. You can shutdown the network connection (assuming your are using a USB wifi adapter) by running the following command:


	  sudo ifdown wlan0
      

To setup a static IP, we'll use the traditional file-based Linux network configuration systems. On Debian-derived Linux distributions, the network settings are set via the '/etc/network/interfaces'. Open this file as root ('sudo') in a text editor of your choice (emacs, vim, etc) and make the following changes (we assume you are using the USB wifi adapter to connect to the network and that it is currently connected):


	  auto lo
	  iface lo inet loopback
	  
	  auto eth0
	  iface eth0 inet dhcp
	
	  auto wlan0
	  allow-hotplug wlan0	
	  iface wlan0 inet static
	      wpa-ssid YOUR_WLAN_SSID
              wpa-psk  YOUR_WLAN_PASSWORD
              address  YOUR_STATIC_IP
              netmask  YOUR_NETWORK_MASK
              gateway  YOUR_GATEWAY
              dns-nameservers 8.8.8.8 8.8.4.4

	  #iface wlan0 inet manual
	  #    wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
	  #iface default inet dhcp
      

You will also likely need to update your '/etc/resolv.conf' file if you wish to properly resolve DNS queries:


	  nameserver 8.8.8.8
	  nameserver 8.8.4.4
      

Finally, we can bring the network back up (if this command generates errors or does not work, confirm that the configuration is correct and/or try restarting your Pi):


	  sudo ifup wlan0
      

Network Testing

This step is covered in the "Web Server - Part 03: Network Testing" video segment on YouTube.

You may find the following commands helpful when confirming network functionality:

Apache HTTP Server

The Apache HTTP Server is one of the most popular HTTP servers on the Internet. You can use Apache to serve static web pages, dynamic web applications, files, and much more.

Install Apache

This step is covered in the "Web Server - Part 04: Installing Apache" video segment on YouTube.

We will start by installing Apache on our Raspberry Pi. Apache is included as a package in the standard Debian package repositories, so we can install it using apt-get. The following commands will install Apache on the Pi:


	sudo apt-get update
	sudo apt-get install apache2
      

Configure and Use Apache

This step is covered in the "Web Server - Part 05: Using Apache" video segment on YouTube.

Now that Apache is installed, we can configure and use it. The Apache configuration files are located in the "/etc/apache2/" directory on Debian-like Linux systems. By default, Apache server the files in the "/var/www/" directory on Debian-like Linux systems.