Using the Raspberry Pi as a home web server

Using the Raspberry Pi as a home web server

A Raspberry Pi used as a web server can be accessed via a web browser using HTTP. Here we show the full process of using the Raspberry Pi as a home web server.

A Raspberry Pi used as a web server can be accessed via a web browser using HTTP. Here we show the full process of using the Raspberry Pi as a home web server.

Introduction

A web server is a computer that can send and receive data via a web browser using the Hypertext Transfer Protocol (HTTP) — the basic network protocol used to distribute text-based information on the internet. A Raspberry Pi can be used to run the required software to be used as a small functional HTTP web server.

In this post, we 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.

Assumptions / requirements

  • Fully installed Raspberry Pi with the latest version of Raspbian
  • 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 standardise the address used to access the web server.

Don’t expect

  • High traffic server
  • High speeds
  • Great security
  • Accessibility from the internet

Sponsored links

Get the Catda Raspberry Pi 4B Starter Kit from BangGood Get the iUniker Raspberry Pi 4B 8GB Starter Kit from Amazon.com Get the iUniker Raspberry Pi 4B 4GB Starter Kit from Amazon.com

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 need 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

The default Apache Debian Page on a Raspberry Pi web server.

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

The mysql-server configuration screen where a password for the administrative root user password is set.

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 need 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 the latest version of PHP, PHP for Apache and PHP for MySQL, the following terminal command is used:

sudo apt-get install php libapache2-mod-php php-mysql

The following command can be used to see the version of PHP installed:

php -v

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.

php-dev is a package that provides supporting files for PHP module development.

sudo apt-get install php-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 to 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 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 and later).

Leave a Reply

Your email address will not be published. Required fields are marked *