A web server is a computer that can send and receive data via a web browser. It uses, the very popular, Hypertext Transfer Protocol (HTTP), the basic network protocol used to distribute information on the internet.
The Raspberry Pi is more than capable to run the required software to be used as a small functional HTTP web server. Using the Raspberry Pi as a home web server will show how to install and configure a LAMP web server (running Linux, Apache, MySQL and PHP) on Raspbian. Ultimately, it will be configured to host a website with a start page. This website will be accessible by any device with a web browser that is connected to the same local network (LAN).
Almost anything that can be hosted on a regular web server can also be hosted on a Raspberry Pi web server. Home web servers can be very handy for learning, home automation projects and website development and testing purposes.
- Fully installed Raspberry Pi with Raspbian (Wheezy or Jessie)
- An active Ethernet or Wi-Fi connection to a local network with access to the internet
- Access to the Raspberry Pi, either directly, or by using PuTTY
Although a static IP address for the Raspberry Pi is not required, it is recommended. A static IP will standarise the address used to access the web server.
- High traffic server
- High speeds
- Great security
- Accessibility from the internet
Installing the required packages
For a Raspberry Pi to be used as a web server it needs the correct software. For a LAMP web server, Apache, MySQL and PHP needs to be installed. All these are free, open-source packages.
Apache 2 HTTP server (Apache) is a very popular web server software that can be installed on various operating systems. According to Wikipedia, Apache is globally the most used software for HTTP servers. At the time of writing, the package was in its second version with distributions for most UNIX-based operating systems, including Raspbian. Apache is also needed to be able to use phpMyAdmin (see later).
Important features of Apache include:
- Listening for requests on TCP/UDP port 80 (more ports can be configured)
- Translating the path to the hostname/IP address
- Supports service side scripting for which various scripting languages can be used
To install Apache, the following terminal command is used:
sudo apt-get install apache2
After Apache has been installed it will start to run automatically. It can be tested by using the IP as the URL in a networked computer’s web browser (e.g. Chrome, Firefox or Internet Explorer). To get the latest IP address of the Raspberry Pi web server, the following command in the terminal can be used:
After the web server’s IP address is inserted, the browser will automatically go to the start page (see below). If the words ‘It works!’ appear on the page, it means the Raspberry Pi Apache home web server is up and running.
Apache will start automatically each time the Raspberry Pi boots up, but can be started and stopped with the following terminal commands:
sudo service apache2 start
sudo service apache2 stop
The start page (index.htm)
The start page, also referred to as the main or index page, is the first or highest indexed web page of a website. It is where everything begins. When the web server address is typed into a connected web browser, the start page will appear.
The start page is generally used as a portal to the rest of a website and supplies links to other pages of the website. Depending on the purpose of the website, the start page can also be used to give the most important information about the website and/or supply direct access to the most relevant functions of the web server or website.
After installing the a LAMP server, the start page (
index.html) was automatically created in the default www directory (see below).
MySQL, short for Structured Query Language, is a database management system. By storing and securing its information, MySQL can be a central component of the LAMP web server.
Not all developers will require MySQL. It must also be said that data stored in a SQL database can also be obtained from a different/remote server (e.g. one that is on a different Raspberry Pi that is connected on the same LAN). The following steps (and the MySQL related steps mentioned later on) can, for example, also be used to only install MySQL on a Raspberry Pi.
To start installing MySQL, the following terminal command is used:
sudo apt-get install mysql-server
Supply and confirm a “root” password when prompted. This is the password that will be used to make requests to the MySQL database(s). Note that this password has nothing to do with the username and password used to gain access to the Raspberry Pi. Choose a (separate) secure password and keep it safe.
Once the password has been confirmed, MySQL will finish installing and the service should start automatically.
MySQL will also start automatically each time the Raspberry Pi boots up, but can be started and stopped with the following terminal commands:
sudo service mysql start
sudo service mysql stop
PHP, short for Hypertext Preprocessor, is a server-side scripting/programming language designed primarily for web development. It is more dynamic than HTML, can be embedded into HTML and can be used to make requests to MySQL databases.
To allow Apache to process PHP programming, PHP and a PHP library package needs to be installed. To allow connections to be made to a MySQL Server (local or remote) through PHP, a package for MySQL also needs to be installed. The latest version of PHP is 5.
To install PHP5, PHP5 for Apache and PHP5 for MySQL, the following terminal command is used:
sudo apt-get install php5 libapache2-mod-php5 php5-mysql
There are various additional packages that can be used to add to or extend the functionality of LAMP web servers. These packages are not required for the initial server installation, but might become necessary at a later stage.
php5-dev is a package that provides supporting files for PHP5 module development.
sudo apt-get install php5-dev
The mysql-client package adds functionality to allow a connection to the MySQL server through the CLI.
sudo apt-get install mysql-client
phpMyAdmin is a package that allows connections to its MySQL server through a web browser. The connection to the MySQL server can be made through any computer that is connected on the same local network. phpMyAdmin makes database queries, management and backups easy.
For phpMyAdmin to function, it needs Apache to be installed too (see earlier). phpMyAdmin is only useful/functional when installed on the local MySQL server (i.e. these related steps are not required locally when a remote MySQL server is going to be used).
sudo apt-get install phpmyadmin
During the installation, choose apache2 when prompted and then Yes to configure.
When asked to ‘Configure database for phpmyadmin with dbconfig-common?’, choose Yes. The administrative account password (i.e. the “root” password) is the the same password that was used to secure MySQL above. This password will also be used to log into phpMyAdmin.
After installing phpMyAdmin, its config file needs to be added to Apache’s config file. To make a backup of Apache’s original config file, use the following terminal command:
sudo cp -p /etc/apache2/apache2.conf /etc/apache2/apache2.conf.original
To add the phpMyAdmin config file to Apache’s config file, open Apache’s config file:
sudo nano /etc/apache2/apache2.conf
and add the following two lines at the bottom:
# Manually added phpMyAdmin code Include /etc/phpmyadmin/apache.conf
After exiting and saving (Ctrl + x and y), Apache should be restarted with:
sudo /etc/init.d/apache2 restart
To open phpMyAdmin in a web browser, type in the IP address of the Raspberry Pi web server, followed by
The “root” username will be able to log in with the confirmed password supplied above.
The www directory
The Apache installation will automatically create a start page (
index.html) in the default www directory. This directory will also be used to store other related pages of the website. The default www directory for Raspbian Jessie is
/var/www/ for Raspbian Wheezy).