Wednesday, June 17, 2015

Webserver on Raspberry Pi

There could be tons of reasons to configure a webserver in your home. From a simple blog (personal, just like a diary, or public if you wish to share your thoughts with the web) to host for an home software repository, a web developing ambient and so on.

For an home automation system, a webserver is probably the easiest way to access your house when you are away.

A Raspberry Pi is a quite useful piece of hardware if you need a small webserver. It shouldn't be used for big systems, where hundreds of people needs to connect at the same time and a lot of data is transferred through the network, but for small tasks is perfect!

Now with Raspberry Pi 2, things are getting even better as this new version is much more powerful than the original one.

Depending on your needs, there are several different solutions for a installing a webserver on a Raspberry Pi. Here an example (and not comprehensive) list of available systems:

All of these systems can be succesfully installed on a RPi. Every system has its own pros and cons of course, but I will not discuss them here.

I will show you how to install an Apache webserver on the RPi.

Apache is not the fastest, but it's quite complete and stable, so I think it would be the best choiche for my needs.

As usual be sure to have an up to date system:

sudo apt-get update
sudo apt-get upgrade

Now it's time to start installing Apache:

sudo apt-get install apache2

It should finish in a few minutes. Then you can test if the webserver is started by opening a web browser and putting the ip of the RPi in the address bar. This is what you should see:

This is the content of the file /var/www/index.html in your RPi. You can modify it or create subfolders for different websites. Just remember that every accessible web object should be put inside the /var/www folder.

Let's go a bit further. With a simple Apache installation you can use just the basic web tools, but sometimes it could be better to use a more powerful scripting language, so now I will install PHP:

sudo apt-get install php5 libapache2-mod-php5

Again few minutes to complete the installation and you can test if it works. This time you need to create a small php script inside the /var/www folder. Use any text editor you like and create a file named info.php (you can of course use whatever name you like). Put the following text inside the file:

<?php phpinfo();?>

Now save the file and put the address in the web browser, adding the name of the file. For example, using the ip of my RPi shown in the screenshot above I will type

If everything is working right you will see something like this:

The page is quite long and gives you all the configuration options of the webserver.

Finally, if you need a database you can install MySQL or SQLite on your system. SQlite is useful when you have to manage few data as it's lighter than MySQL. Anyway if you need to use lots of data, just go on with MySQL as I do:

sudo apt-get install mysql-server mysql-client php5-mysql

During the installation of MySQL it will be asked to add an administration password. Choose one and type it (two times) to go on.

At the end call again the info.php file from the browser and scroll down until you find the MySQL section:

The text "enabled" confirms that you have completed the installation and that MySQL is up and running.

As you can see installing an Apache webserver is quite easy. Now it's only matter to start creating your web applications!

1 comment:

  1. Try Tornado to serve Python scripts. Works fast.