Apache website hosting

An Apache webserver on your Raspberry PI

1. Before we install Apache to our Raspberry Pi, we must first ensure the package list is up to date by running the following two commands.

sudo apt-get update

sudo apt-get upgrade

2. First, we will need to install the Apache2 package on our Raspberry Pi.

For those who don’t know what Apache is, it is a server software that serves the HTML files from a computer to the web browser.

To install apache2 on your Raspberry Pi, enter the following command into the terminal.

sudo apt install apache2 -y

3. With Apache2 installed to our Raspberry Pi, we now have an extremely basic web server up and running. The server will be able to provide non-dynamic content such as HTML files.

In the next section, we will be extending this basic Apache web server by installing PHP to the Raspberry Pi.

To check that Apache is up and running on your Raspberry Pi, you can enter the Raspberry Pi’s IP address into a web browser. The server should return a webpage with some simple text on it.

If you do not know the IP, you can enter the hostname command into the terminal to retrieve it.

hostname -I

4. In a web browser, enter your Raspberry Pi’s IP Address, it should connect and load a page like the one below.


5. To be able to make changes to the files within the /var/www/html without using root we need to setup some permissions.

Firstly, we add the user pi (our user) to the www-data group, the default group for Apache2.

Secondly, we give ownership to all the files and folders in the /var/www/html directory to the www-data group.

sudo usermod -a -G www-data pi

sudo chown -R -f www-data:www-data /var/www/html


or


sudo chown -R -f pi: /var/www/html


Once you have run that command, you will need to logout and then log back in for the changes to take effect.

6. You can now make changes to the default web page by running the following command.

This command will use the nano text editor to modify the index.html file.

The web server will serve all files within the /var/ww/html/ directory.

nano /var/www/html/index.html

Apache is a basic web server and is great if you want to learn HTML, JS, or CSS.