Raspberry Pi

How to Set up the Apache Web Server on the Raspberry Pi

Apache is an open-source web server launched and maintained by the Apache foundation and it is used to access different web pages. For the readers who do not know about the working of the web server, a web server finds the webpage according to the provided URL or HTTP and after clearing the security checks it displays the webpage.

In this write-up, we will learn the installation procedure of the Apache web server on the Raspberry Pi operating system and also learn how a web page is accessed using the Apache web server.

How to install an Apache web server on Raspberry Pi

Before setting up the Apache server, we will first update and upgrade all the packages up to date of the Raspberry Pi using the apt package manager command:

$ sudo apt update && sudo apt upgrade -y

Now, we will check the status of the Apache server using the systemctl command:

$ sudo systemctl status apache2

The output means that the Apache server has not been pre-installed, so we will install it using the apt package manager:

$ sudo apt install apache2 -y

To confirm the installation of the Apache server on the Raspberry Pi, we will find out the status of Apache2 using the command:

$ sudo systemctl status apache2

Now, we will type the IP address of the Raspberry Pi device in the URL bar of the web browser to check the running status of the Apache2, to know the IP address of the device, use the command:

$ hostname -I

Type the 192.168.18.218 (or simply type “localhost”) in the URL bar of the Chromium web browser of the Raspberry Pi:

The default Apache2 web page is displayed and confirms the installation of the Apache2 on Raspberry Pi.

How to set up the Apache2 server on the Raspberry Pi

For the configuration of the Apache2, we have to make the changes in the file /var/www/html, but before making the changes, we will add our Raspberry Pi user, Pi, to the www-data group(default group of Apache2) using the command:

$ sudo usermod -a -G www-data pi

After adding the user “Pi” to the group “www-data”, we will transfer all the ownership privileges of /var/www/html to the “www-data” group using the command:

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

To save the changes, reboot the Raspberry Pi using the command:

$ reboot

How to install the PHP on Raspberry Pi

We can make websites using HTML and CSS only but those will be static websites whereas PHP is used to create dynamic websites, moreover, we cannot run it on our local machine therefore we create a virtual server in our local machine using Apache or xampp. It is primarily used to manipulate databases. It is popular because it is platform independent and can easily be integrated with many database management systems.package of the Raspberry Pi repository using the command:

$ sudo apt install php libapache2-mod-php -y

Restart the Apache2 server using the systemctl command:

$ sudo systemctl restart apache2

Now we will create a Webpage with the help of PHP with the name of “linuxhint.php” using the nano editor:

$ sudo nano /var/www/html/linuxhint.php

We will display the “Welcome to the LinuxHint” and for this we will type the following php script in the file opened:

<?PHP
    echo "Welcome to the LinuxHint”;
?>

Exit the nano editor by saving the file using the shortcut key CTRL+X, and then go to the chromium browser and type the following address in the URL bar:

http://localhost/linuxhint.php

We can see the web page which was created by us has been opened.

Conclusion

The users can deploy their websites using the Apache web server as it is the oldest and most used web server for hosting the websites. In this write-up, we have installed the Apache web server on the Raspberry Pi operating system and displayed the newly created web page using the Apache web server.

About the author

Hammad Zahid

I'm an Engineering graduate and my passion for IT has brought me to Linux. Now here I'm learning and sharing my knowledge with the world.