Using Raspberry Pi as a web server
The single board computer, Rasberry Pi can be quickly set up in such a way that makes it possible to essentially run the server yourself. Even if its performance is a bit more modest than that of other competitors, a private Raspberry Pi web server can prove useful in many respects: testing and hosting websites in a hosting environment or individually managing files and making them available through the cloud are just a few applications 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 performance of a computer is more modest than that of a conventional 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 connections feature reduced bandwidths to prevent Raspberry Pi from becoming full-fledged servers. Despite these points, there are numerous application areas for web servers that are operated using Raspberry Pi: it can be used as a home server with internet connection or also used for a company network. What’s more, internet presences can also be tested while online and small websites experiencing modest amounts of traffic can also be hosted. Additionally, you can also operate your own cloud with this technology or set up programs designed for house automation (controlling lighting, heating, etc.). A further advantage comes in the form of the low costs associated with running a Raspberry Pi web server: it only requires an internet connection and power. The latter component doesn’t normally go above the 5W mark, making constant operation hardly anything to lose sleep over. Additionally, the one-time installment fee as well as the other required components also constitute fairly negligible costs.
Requirements 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 connection— through a network cable (recommended) 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 smartphone, or other devices. For those who have decided to administer 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 alternatively with ifconfiq). For those using a headless Raspberrry 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 application, 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 permanently 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 constantly access them. Generally, a home server’s internet connection 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 possibilities 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 possibilities available. Here, domain names are registered and then connected with a computer or router. Via software, the domain name is always automatically allocated the current IP address of your internet access, which allows the server to remain continuously 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 components: 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 phpMyAdmin 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 individual LAMP components. This tutorial takes you through each process step by step. Here, some connections are explained as well as test processes that allow you inspect the functionality of each installation.
Setting up Apache
Installing 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 components 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 installation 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 additional 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, JavaScript 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 corresponding page should then appear. Once this happens, the PHP on your Raspberry Pi is then fully set up.
Setting up MySQL
phpMyAdmin can be used to administer 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 installation:
sudo apt-get install phpmyadmin
In the screen that appears following installation, select Apache 2 as the web server. Next, you’ll be asked if the phpMyAdmin management database should be installed; answer this question with a ‘Yes.’ After this, decide on a password for phpMyAdmin (it can be the same one as the MySQL password) and the installation is now complete.
Finally, you need to connect the newly installed phpMyAdmin with the Apache web server. This is done with the following command above which you can edit the configuration file, apache2.conf, with the Nano editor.
sudo nano /etc/apache2/apache2.conf
Now, using your cursor, go to the end of the configuration file (you can also continuously press the key combination ‘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 configuration 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 administrable in the web browser via phpMyAdmin. To this end, just enter the Raspberry Pi IP address and then the /phpmyadmin ("http://192.168.X.X/phpmyadmin") into the URL bar and register on the phpMyAdmin 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 additional functions, all within phpMyAdmin.
Raspberry Pi web servers in application
Your web server has now been fundamentally configured 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 individual web pages can either be created directly onto Raspberry Pi or under the directory /var/www (Raspbian Wheezy). Alternatively, 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 programmes like FileZilla, WinSCP, PuTTy, or OpenSSH. Settings for the Apache web server can be adjusted with the configuration file, .htaccess. Via this file, users are able to create individual 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 administer 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 synchronise 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 especially rise when the server is constantly 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.