Raspberry Pi Lighttpd Web Server Tutorial

Setting Up

Acquire a Raspberry Pi, configure your SD card with Raspbian, and connect to the internet via ethernet.  Get yourself a case similar to the one shown or make one from lego or cardboard.

Once you’ve booted your Pi, it’s crucial to change the default password.  If you’re comfortable at the command line, ditch the default user Pi and the desktop interface.  This will help preserve the life of your SD card.  If you think the server will get much traffic, it’s worth taking the time to boot Raspbian from a USB instead of the SD card.

Obtaining A Static IP

Dynamic Host Configuration Protocol (DHCP) is used to distribute network configuration parameters (e.g., an IP address) in a dynamic way; that is, the parameters are changed every time the system boots.  DHCP is there to keep us from having to do all this configuration manually, but for our server, we want to take control of IP assignment.

Open the file interfaces in the network directory.

cd etc/network/
nano interfaces

You’ll need to change the line

iface eth0 inet dhcp

to

iface eth0 inet static

The change of ‘dhcp’ to ‘static’ means that we will manual assign a static (not-changing) IP address to our server, rather than DHCP automatically assigning a dynamic (changing) IP address.  Next, we need to do said manual assigning.  Add the lines below directly after the line you just changed.

address 192.168.10.191
netmask 255.255.255
gateway 192.168.10.1
broadcast 192.168.10.225
network 192.168.10.0

The address is the internal IP you want to give your Raspberry Pi Server.  This can be any IP number in the network range, but higher is better to avoid overlapping.  When you configure your router settings to accommodate your server, make sure to set the DHCP range to prevent overlaps.

To find the current IP of your Raspberry Pi along with others needed above, use the command:

ifconfig

Under eth0, the IP of your RPi will be next to inet addr, the broadcast value will be by Bcast, and the netmask value next to Mask — it will nearly always be 255.255.255.0.  Broadcast and network refer to the range of IPs provided by your router.   The gateway value is your router’s IP.  Technically, here you only need the address and netmask lines, but the other three worth including.

It is best now to restart your system and check that the IP you assigned is being used.  Use the command below to restart, and the command above to check the IP.

sudo reboot

Configuring your Router

This is a simple step: login to your router’s web accessable settings and forward all traffic on port 80 (web traffic) to your server’s IP.

Installing Lighttpd Server Software

Lighttpd is an open source project initially released in 2003 and regularly maintained (last stable release as of this post was in January of 2014).  Several well-known websites use Lighttpd, including YouTube and some Wikimedia servers.

Lighttpd has a small memory footprint and places a small load on the CPU.  Because of this, it is fairly ideal for the Raspberry Pi, particularly if you are not comfortable at the command line completely and are still using the desktop interface with the user Pi.  Support for FastCGI, SGI, and CGI is included.

Installation

Start by installing Lighttpd, using the command below.

sudo apt-get -y install lighttpd

After Lighttpd has been installed, it will start on the next system reboot.  Upon startup, if all goes well, you will see the following displayed.

[ ok ] Starting web server: lighttpd.

When you enter the IP address of your server into your browser’s URL bar, you should now see the Lighttpd placeholder page.  Next, we’ll replace this page with one of our own.

Changing the Default Page

Files displayed by the web sever are stored in the www directory, under the var directory.  Start by navigating into this directory.

cd /var/www/

In this directory you will see the file index.lighttpd.html — move, remove, or rename this file.  Because the Raspberry Pi only has two USB ports and mine are both occupied, I copied files to this directory from my primary computer via ssh.  To copy from another machine to the Raspberry Pi, use the following command (where user, ip, and path refer to the username, IP, and location of the file on the remote computer)

scp user@ip:path/to/file/index.html /var/www/

If you have a free USB port and want to copy the file from a connected USB storage device to the Raspberry Pi, figure out the path to the file on your USB device.  Your USB device should be located in the media directory under the name of your device.  To copy your file, use the line below.  

cp /media/USBname/path/index.html /var/www/

Confirm that your new index.html page is showing in place of the Lighttpd placeholder again by putting your server’s IP in your browsers URL bar.

DNS Configuration

You’ll need several DNS listings to get connect your domain to your server.

NS Listings: Your Nameserver listings will depend on how you have registered your domain and set up your DNS, but there will be two to four of them, and they will look somewhat like this: NS1.WORDPRESS.COM

Two A Listings: Fairly simple: one of your A Listings will be your Raspberry Pi server’s external IP, and the other your sever’s internal IP.

One TXT Listing: This TXT listing should be the server’s external IP and the port number the router is using for the server, separate with a colon.  It should look like ##.##.###.##:80 where the number signs are your external IP, and 80 is your port.

Once again, restart your server, wait for DNS to update, and check to make sure everything is working.  If it isn’t, dont be discouraged: this is complicated and difficult to accomplish at first.  Retrace your steps, check your configuration files and DNS listings.  And when it does at last work, be happy!

Conclusion

Lighttpd is only one of several choices for lightweight web server software.  Apache is another option, and I will post a Raspberry Pi Apache Web Server tutorial too.  Apache is a heftier, more configurable, and arguably more stable option.  It has been around longer than Lighttpd, and can do more than Lighttpd, such as the web accessable admin console.  The syntax of Apache configuration files is less readable than those of Lighttpd.

Other software to consider using for your small Raspberry Pi web server includes MySQL and a web application framework such as Django.  While you can do all of this installing and configuring manually at the command line as described in these tutorials, like most things with computers it is easier in the long run to write a script to do all of this for you.  Especially if you plan to install this software on multiple servers.  

DISCLAIMER: This tutorial is an explanation of the steps that have worked for me: they may work for you, they may need to be slightly altered.  The steps outlined in this tutorial can be undone by reinstalling the OS or returning to a previous backup, however it is important to know that using the sudo command or working as the root user has the potential to alter and break your operating system in unintended ways if done incorrectly.

If you try this tutorial, please let me know how it goes in the comments!  I would be interested in discussing alternative methods and any problems that arise, as well as any advice for future Raspberry Pi tutorials.

Advertisements

6 thoughts on “Raspberry Pi Lighttpd Web Server Tutorial

  1. I’m not sure why but this website is loading incredibly slow for
    me. Is anyone else having this problem or is it a problem on my end?
    I’ll check back later oon and see if thhe problem
    still exists.

    Like

    1. I don’t seem to be having a problem loading this page or other WordPress pages, so it sounds like it might be on your end. I hope you were able to resolve it!

      Like

Add a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s