Categories

Meta

A web server on a Raspberry Pi

Here I offer a small tutorial for those trying to use it as a simple web server.
This will assume you already built your raspberry pi box with Raspbian installed in the SD card.

I compiled a list of recipes from different websites and pages, in the hope that this will be useful to someone with a similar setup as mine (a macbook with OS X and a wifi router at home.)

You don’t need your pi to be connected to an external monitor and keyboard to make it work. If your pi came with the Raspbian OS then it’s prepared to be accessed through SSH out of the box.

Connect your pi to your router with an ethernet cable or set up your wifi connection (you need to enable wifi first on your pi.) Then you need to find out which internal IP was the raspberry assigned to by your router, and then ssh with user pi and password raspberry. To find the IP of your pi you can check on your router settings, the DHCP Client List. Your pi should be named raspberrypi or similarly.

In principle it’s not guaranteed that the IP will be the same every time the device connects, but in my experience with my router, the IP assigned to each device is always the same.

The first time you log in you will be greeted with a setup menu. I suggest you follow the instructions on this site to get it up and running. Most importantly, change the default password to log in!

Once you know your pi’s IP (in my case was 192.168.2.3, yours may differ) you can log in normally.

ssh pi@192.168.2.3
password: raspberry

Installing Apache 2

The following are steps to install an Apache server on your raspberry pi. Apache is a popular web server in the Linux side of computers. Another emerging contender on the open source side is Nginx (pronounced ‘Engine X’), but I have no experience with it. I should probably buy another pi and try this new server; after all, one of the advantages of the pi is the relatively inexpensive hardware!

Test Apache with a simple html page

You can manage several sites on your pi with Apache, all available sites are stored in files under /etc/apache2/sites-available, of those you can enable or disable the ones you like with the commands a2ensite and a2dissite (a2 is for apache2 and the rest is enable/disable site.) These commands basically create and delete symlinks into /etc/apache2/sites-enabled.

Go to /etc/apache2/sites-available. We’ll make a test page to confirm apache is running well. First we copy the default site provided by apache to have a template to work with.

sudo cp default test

Edit the test file replacing instances of “/var/www” to “/home/pi/www”
and create a simple welcome index.html in /home/pi/www

Below is an example of a simple html. If emacs is your preferred command line editor, you will have to install it now, because Raspbian doesn’t include it by default.

You should see the Welcome page you set up in /home/pi/www, when you visit the pi’s IP (type http://192.168.2.3 on your favorite web browser)

If you want to access your website from outside your internal network (given by your router) you need to direct the external HTTP requests on port 80 to your raspberry pi’s port 80.

If you’re connected through Wifi like I am, then you have a wireless router and you have to configure your router to do the redirection. Select incoming port 80 to map to your pi’s internal IP’s (192.168.2.3 in my case) port 80.

Once you do that, you can access your website through your IP (the one provided by your ISP) on the web browser. You may want to hire a Domain Name Service to assign your IP number to some memorable web address. Some websites offer this service for free, noip.com is one of them.