The Raspberry Pi might look like a small, almost inconsequential computer, but don’t let looks deceive you. It is a fully functional Linux machine capable of performing a large variety of functions including acting as a local web server.
The most common setup for a Linux-based web server is a “LAMP” server, where LAMP stands for Linux, Apache, MySQL and PHP. The default operating system for the Pi is Linux, specifically Raspbian and it can also run Apache, as the web server; MySQL, as a database; and PHP, for server-side scripting.
Before installing the web server, it is best to configure your Raspberry Pi with a static IP address. Follow the “How to set up a static IP address on a Raspberry Pi” tutorial.
The LAMP server components can all be installed from the Raspbian online repositories using “apt-get.” On the command line type the following:
sudo apt-get install apache2 php5 libapache2-mod-php5 mysql-server mysql-client php5-mysql
During the installation, you will be asked to enter a password for the MySQL root user. This is the administrator’s password and stops unauthorized access.
When the install has finished, the Apache web server should be up and running. You can test it by opening the default web page at the IP address of you Raspberry Pi. Start a web browser on a PC and enter the “http://192.168.1.4” where “192.168.1.4” is the static IP address of your Pi. The resulting web page will look something like this:
You may have noticed an error message at the end of the installation process: “Starting web server: apache2apache2: Could not reliably determine the server’s fully qualified domain name, using 127.0.1.1 for ServerName.”
On the Internet, a web server needs a name to tell it what domain it is serving. During startup, Apache looks for that name, and if one isn’t configured, it displays a warning and uses 127.0.1.1. For a local web server running on your private LAN, this isn’t a problem, but to get rid of the annoying message, it is best to configure the name.
Edit the Apache configuration file (/etc/apache2/apache2.conf) using the following command:
sudo nano /etc/apache2/apache2.conf
Scroll to the bottom of the file and add the following line:
You can actually use any domain name you like, but since your Pi is running as a local web server, “pi.example.com” is as good as any! Exit nano with “Ctrl + X”. Finally the web server needs to be restarted:
sudo service apache2 restart
The default HTML directory for Apache is “/var/www”. To test PHP, create a file called “testphp.php” in that directory with just a single line, a call to the PHP test function – testinfo(). Create the file using nano:
sudo nano /var/www/testphp.php
And enter the following line:
<?php phpinfo() ?>
Exit nano. Start a web browser on a PC and enter the “http://192.168.1.4/testphp.php” where “192.168.1.4” is the static IP address of your Pi. The resulting web page will look something like this:
Most web platforms, like WordPress, Drupal, Roundcube, MediaWiki and so on, require a back-end database. When you install a web platform it is often necessary to provide the name of a database and a MySQL user for the software to use. To create a database and a user start the “mysql” command line tool using:
mysql -u root -p
You will be prompted for the root password you set during the install.
To create a database use:
create database mydb;
And to create a user use:
create user mydbuser identified by 'password';
Finally, to grant the “mydbuser” user full access to the “mydb” database use:
grant all privileges on mydb.* to mydbuser identified by 'password';
At this point your Raspberry Pi is running Apache, PHP and MySQL and you have the tools you need to create databases. Just be mindful of the amount of free space on your SD card and everything should work great. If you have any questions please feel free to ask in the comment section below.