Using the Raspberry Pi as a home web server

Using the Raspberry Pi as a home web server

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 serverUsing 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).

parrot Webserver
A Raspberry Pi web server can be accessed from any web browser connected to a LAN. This is a sample main page of my own home automation web server.

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.

Requirements

  • 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.

Read more about setting up a Raspberry Pi hereinstalling Raspbian here and setting up a static IP here.

Don’t expect

  • 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

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

Apache is working

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:

hostname -I

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

and

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

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.

MySQL server root password

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

and

sudo service mysql stop

PHP

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

Additional packages

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.

phpMyAdmin database configuration

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.

Configuring phpMyAdmin on a Raspberry Pi

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 /phpmyadmin, e.g.:

192.168.1.16/phpmyadmin

phpMyAdmin login screen

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/http/(and /var/www/ for Raspbian Wheezy).

Related product links

Please support Behind The Scenes by using these links to purchase these related products online from our partner websites. Links will open in a new window...

About the author
Renier busies himself with improving his English writing, creative web design and his websites, photoshopping, micro-electronics, multiple genres of music, superhero movies and badass series.
Behind the Scenes is a free, informative website. If you find value in any of out content, please consider making a donation to our cause.
Donate via PayPal

Save, share & Disqus

Use the buttons below, on the left or the bottom of this page to share this post. Your comment is important, but don't be a knob. Keep it constructive and polite.

Comment via Disqus

Disqus is a worldwide comment hosting service for web sites and online communities. This secure platform ensures a pleasant commenting environment which is manageable from one account. Use the Login button to sign up.

More Raspberry Pi web server related posts

Try one of Lynda.com’s online software and skills learning courses
Try one of Lynda.com’s online software and skills learning courses
16 July 2019
Ad: Improve your knowledge on anything from programming to business skills with Lynda.com’s huge range of professional video courses. More…
Understanding common network protocols
Understanding some common network protocols
7 September 2016 | Updated 17 November 2017
Deeper exploration into the world of inter-computer communications will reveal more and more types of communications (e.g. radio waves, Ethernet, Bluetooth, etc.), their interfaces and the network protocols they use. More…
Communicating between 2 Raspberry Pi's using sshpass
Communicating between 2 Raspberry Pis using sshpass
10 September 2016 | Updated 24 October 2018
sshpass is a Linux software package that can be used to establish a direct (secure) connection between two Linux operated computers. This connection allows ‘remote’ access to a ‘local’ computer to run commands ‘locally’. Communicating between 2 Raspberry Pi’s using sshpass will show you how. More…
Categories
Raspberry Pi