The single board computer, Rasberry Pi can be quickly set up in such a way that makes it possible to es­sen­tially run the server yourself. Even if its per­form­ance is a bit more modest than that of other com­pet­it­ors, a private Raspberry Pi web server can prove useful in many respects: testing and hosting websites in a hosting en­vir­on­ment or in­di­vidu­ally managing files and making them available through the cloud are just a few ap­plic­a­tions that this option offers. Our tutorial will show you how to set up your own web server on a small PC by using this free-of-charge software packet.

What are Raspberry Pi web servers capable of?

For those using Raspberry Pi web servers, it’s important to be aware of the fact that their hardware is limited. The per­form­ance of a computer is more modest than that of a con­ven­tion­al web server that can be rented via a standard internet provider. Raspberry Pi isn’t suitable for hosting complex internet presences (like online stores or websites with lots of dynamic content), and also should be avoided when dealing with sites that receive large amounts of visitors. Many domestic internet con­nec­tions feature reduced band­widths to prevent Raspberry Pi from becoming full-fledged servers. Despite these points, there are numerous ap­plic­a­tion areas for web servers that are operated using Raspberry Pi: it can be used as a home server with internet con­nec­tion or also used for a company network. What’s more, internet presences can also be tested while online and small websites ex­per­i­en­cing modest amounts of traffic can also be hosted. Ad­di­tion­ally, you can also operate your own cloud with this tech­no­logy or set up programs designed for house auto­ma­tion (con­trolling lighting, heating, etc.). A further advantage comes in the form of the low costs as­so­ci­ated with running a Raspberry Pi web server: it only requires an internet con­nec­tion and power. The latter component doesn’t normally go above the 5W mark, making constant operation hardly anything to lose sleep over. Ad­di­tion­ally, the one-time in­stall­ment fee as well as the other required com­pon­ents also con­sti­tute fairly neg­li­gible costs.

Re­quire­ments for a Raspberry Pi web server

The following web server presents one of many different ways of setting up Raspberry Pi as a web server. Set-up requires the following in addition to a small computer:

  • SD card on which the operating system Raspbian is set up
  • An internet con­nec­tion— through a network cable (re­com­men­ded) or WiFi
  • Power supply through a micro USB cable

Given that web servers are headless (i.e. they operate without screens or input devices), which in turn makes them energy efficient, it’s best to also set up a remote access option via SSH. With the help of a SSH client (such as PuTTY, WinSCP) for Windows or OpenSSH for Unix operating systems, users are able to easily set up and make changes to web servers from a computer, a smart­phone, or other devices. For those who have decided to ad­min­is­ter their servers via SSH, enter your Raspberry Pi’s IPv4 address into your SSH client in order to connect the two with one another. In case you don’t know your Raspberry Pi’s address, you can call it up in the terminal with the command hostname –l (or al­tern­at­ively with ifconfiq). For those using a headless Rasp­berrry Pi set-up, you can find out its address through your router by calling up your web browser (generally under "http://192.168.0.1") Those looking to use the webs server for a private cloud or another ap­plic­a­tion, which also need more storage space, should expand their storage capacity. With Raspberry Pi, this can be done quite easily via a USB storage stick or an external hard drive. 

Making a server per­man­ently available under the same IP address

Servers need to be both available online and under the same IP address so that users are able to con­stantly access them. Generally, a home server’s internet con­nec­tion only has one dynamic IP address. This changes regularly (most of the time after 24 hours) and prevents the server from being available under the same IP address. A static IP address would normally be of use here, but it’s not offered by every internet provider.

There are, however, other pos­sib­il­it­ies for hosting a server under the same address; for example, this can be done with a dynamic DNS (DDNS). For this option, there are both free-of-charge as well as fee-based pos­sib­il­it­ies available. Here, domain names are re­gistered and then connected with a computer or router. Via software, the domain name is always auto­mat­ic­ally allocated the current IP address of your internet access, which allows the server to remain con­tinu­ously available under the same name.

Setting up Raspberry Pi web servers with LAMP

The following tutorial shows you how to set up Raspberry Pi on your web server with a so-called LAMP stack. LAMP is the acronym for a programme package comprised of the following com­pon­ents: a Linux operating system (in this tutorial: Raspbian, which comes with the device), an Apache web server, a MySQL database, and the script language PHP. In addition to the LAMP packet, the phpMy­Ad­min is also installed. First register in the terminal on your Raspberry Pi. Before you can begin setting up the LAMP software packet, update all of the already-installed packets with the following two commands:

sudo apt-get update
sudo apt-get upgrade

Now you can start setting up the in­di­vidu­al LAMP com­pon­ents. This tutorial takes you through each process step by step. Here, some con­nec­tions are explained as well as test processes that allow you inspect the func­tion­al­ity of each in­stall­a­tion.

Setting up Apache

In­stalling an Apache HTTP server can be carried out quite quickly. Just one command needs to be entered — the same applies for the other three LAMP com­pon­ents of the Raspberry Pi web server. For Apache 2 just enter the following command in the terminal:

sudo apt-get install apache2

In order to ensure that the in­stall­a­tion was a success, call up Raspberry Pi’s IP address in your browser’s URL bar. Once Apache 2 has been correctly set up, a default page appears in the browser that reads ‘it works!’. Under Raspbian Jessie, this HTML page is found in the directory /var/www/html/index.html. On the older Wheezy version of Raspbian, this is stored under /var/www/index.html. In the directory, users are able to edit the site and create any amount of ad­di­tion­al web pages. 

sudo nano /var/www/index.html

or

sudo nano /var/www/html/index.html

If you make any changes, then it’s important to make sure that you’re able to see these once you reload the page in the browser. 

Setting up PHP

In order for web servers to be able to process HTML, CSS, JavaS­cript as well as PHP files, install PHP5:

sudo apt-get install php5 libapache2-mod-php5

Now you can generate a PHP file under /var/www. For test purposes, create the file phpinfo.php:

sudo nano phpinfo.php

In the editor, make the following changes:

<?php
phpinfo();
?>

When you call up your Raspberry Pi IP address followed by /infophp.php (http://192.168.X.X/phpinfo.php), the cor­res­pond­ing page should then appear. Once this happens, the PHP on your Raspberry Pi is then fully set up.

Setting up MySQL

phpMy­Ad­min can be used to ad­min­is­ter MySQL. With the help of this free-of-charge software, users are able to easily manage databases via a graphical user interface in the browser. The following command is used for in­stall­a­tion:

sudo apt-get install phpmyadmin

In the screen that appears following in­stall­a­tion, select Apache 2 as the web server. Next, you’ll be asked if the phpMy­Ad­min man­age­ment database should be installed; answer this question with a ‘Yes.’ After this, decide on a password for phpMy­Ad­min (it can be the same one as the MySQL password) and the in­stall­a­tion is now complete.

Finally, you need to connect the newly installed phpMy­Ad­min with the Apache web server. This is done with the following command above which you can edit the con­fig­ur­a­tion file, apache2.conf, with the Nano editor.

sudo nano /etc/apache2/apache2.conf

Now, using your cursor, go to the end of the con­fig­ur­a­tion file (you can also con­tinu­ously press the key com­bin­a­tion ‘Ctrl’ + ‘V’) and write new lines into the file:

Include /etc/phpmyadmin/apache.conf

Now save the changes via the shortcut ‘Ctrl’ + ‘O’ and close the con­fig­ur­a­tion file with ‘Ctrl’ + ‘X’. After that, restart the apache web server via a further command in the console:

/etc/init.d/apache2 restart

Now both your web server is set up, and your data base should be ad­min­is­trable in the web browser via phpMy­Ad­min.  To this end, just enter the Raspberry Pi IP address and then the /phpmy­ad­min ("http://192.168.X.X/phpmy­ad­min") into the URL bar and register on the phpMy­Ad­min page with the user name ‘root’. As soon as you’ve logged in, you can list data sets, create or delete tables and databases, or use your own ad­di­tion­al functions, all within phpMy­Ad­min. 

Raspberry Pi web servers in ap­plic­a­tion

Your web server has now been fun­da­ment­ally con­figured and is ready to use. You’re now able to create and host a website by equipping your web server with HTML and PHP pages. The in­di­vidu­al web pages can either be created directly onto Raspberry Pi or under the directory /var/www (Raspbian Wheezy). Al­tern­at­ively, you can also create the pages on an external computer with a web editor of your choice and transfer these to the web server via a SFTP client software. This step is carried out with pro­grammes like FileZilla, WinSCP, PuTTy, or OpenSSH. Settings for the Apache web server can be adjusted with the con­fig­ur­a­tion file, .htaccess. Via this file, users are able to create in­di­vidu­al 404 error pages for their web sites or redirects to another domain. Using Raspberry Pi enables users to easily create websites and learn how to ad­min­is­ter web servers in a few easy steps. In addition to web hosting, the web server can also be used for many other needs. For example, using Raspberry Pi’s web server as a private file and media server. On this, you can also operate and sent up a private cloud with the freely available software, ownCloud. The file hosting programme allow users to store files in the cloud as well as call up and syn­chron­ise these as well. The list of the projects that can be completed with a Raspberry Pi web server is long.

Web server security

As with any IT component, security is paramount when dealing with web server security. The chances of an attack es­pe­cially rise when the server is con­stantly connected to the internet. For this reason, it’s important that you always ensure that your web server is equipped with the newest updates. Sensitive data should only be hosted via web servers when you’re sure you know what to look out for when doing this.

Go to Main Menu