Raspberry Pi

How to make Raspberry Pi a web server

A web server is a computer which provides its service to other users that can be on your network or outside your network. A web server has the capability to run different software and it can easily store HTML docs, videos, images and other files that can be accessed from anywhere.

If you are really passionate about creating a web server but you are finding difficulty in creating it then look for the steps in this article, which will help you in making your Raspberry Pi a web server.

How to make Raspberry Pi a web server

Here, we will tell you how you can make Raspberry Pi a web server if you carefully do each step given below and you can then be able to do this complex task in an hour. If you miss any of the steps below then you won’t be able to make Raspberry Pi a web server.

Step 1: The first thing you should do is to power your Raspberry Pi and ensure that the power system is capable of providing enough current to Raspberry Pi.

Step 2: Ensure that you have installed your Raspberry Pi operating system.

Step 3: Now make sure that your Raspberry Pi operating system is connected with the internet and speed of internet is good enough to install the packages.

Step 4: Now after connecting your Raspberry Pi operating system with the internet, you have to ensure that your packages are updated. If not , you can update through the below given command in the terminal.

$ sudo apt–get update

Step 5: Now you have to install Apache in your Raspberry Pi as it’s an open-source server with the capability of handling your traffic on the web. You can install Apache by simply entering the below given command in the terminal of your Raspberry Pi.

$ sudo apt install apache2 -y

Step 6: After installing the Apache in your Raspberry Pi operating system, you are now required to do full upgradation. This upgrade is necessary as it helps you to install the missing packages on your Raspberry Pi, which are required for the installation of other packages. The full upgrade may take time depending on your internet speed.

$ sudo apt-get full-upgrade

Step 7: Now the next thing you should do is to install PHP as it ensures your proper web pages functioning as well as it helps to communicate with the database.

In order to install PHP, you are first required to install some packages. You have to install the key for the PHP repository first through the below command.

$ curl https://packages.sury.org/php/apt.gpg | sudo tee /usr/share/keyrings/suryphp-archive-keyring.gpg > /dev/null

Then, you have to create a source file with the link to the repository using the below command.

$ echo “deb [signed-by=/usr/share/keyrings/suryphp-archive-keyring.gpg] https://packages.sury.org/php/ $(lsb_release -cs) main” | sudo tee /etc/apt/sources.list.d/sury-php.list

Now, you need to update the package again.

$ sudo apt-get update

Once the updates are finished, you can then add the PHP repository from the below mentioned command:

$ sudo apt-add-repository ppa:ondrej/php

Now, through the command given below you can install PHP on your Raspberry Pi.

$ sudo apt install php8.1-cli

Step 8: Now you have to download mariadb-server in order to begin the installation of SQL database. You can add mariadb-server by adding the below command.

$ sudo apt-get install mariadb-server

Step 9: After successfully installing the mariadb-server, you can now secure SQL databases using the below command in the terminal.

$ sudo mysql_secure_installation

After the completion of the command, you need to enter your password. You can leave it blank and in the next step you can enter “n” to not change your root password.

You need to do some things as they are required to successfully run the Raspberry Pi web server.

After completing the above steps, it will provide you with a message about the successful installation of MariaDB.

Step 10: Now after the installation is complete, you now restart apache2 service using the below mentioned command in the terminal.

$ sudo service apache2 restart

Step 11: Now the next thing to do is to change the host name of the server, which can be done by typing the below command in the terminal.

$ sudo raspi-config

It will open the Raspberry Pi configuration for you.

Step 12: Go to the interface option and enable SSH there.

Step 13: Go to system options, and then select hostname.

Step 14: Now, you are required to enter a hostname. Write any name you want.

Step 15: Press Ok and then click on Finish. It will ask you to reboot. Go for the reboot by clicking on the “Yes” option.

Step 16: After the system reboot, you can then go to the menu option, you will see the “Preference” option there go to Raspberry Pi configuration and in the system you will be able to see that your hostname is changed.

Step 17: You can check the status of your Apache whether it is running or not through the following command.

$ sudo service apache2 status

Step 18: You can also check whether your server is running or not by typing the host IP in your browser. You can find your host IP by entering the below command in the terminal.

$ hostname -I

Step 19: Write the IP address in your browser and you will see the default apache webpage.

Step 20: Now to build your website, you need to locate the html directory in your system. You need to type “/var/www/html” in the directory search box to open the html file.

You can build the websites by putting or replacing the html files in the directory.

Step 21: Next, you need to start developing a webpage using PHP. For that you need to enter the below given command in the terminal which will let you create a php page in the html folder.

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

After entering the command, you will then be able to write web page code, for example:

<?php
echo “This is LinuxHint”;
?>

Step 27: Now, go to the address “10.0.2.15” and there you will see your own webpage.

Conclusion

Why should you be worried when you have a golden opportunity to build a website and you should take advantage of it without delay? Using the Raspberry Pi as a web server allows you to create your own personal website without having to look for other options. So, give that a try, turn your Raspberry Pi into a web server, and you’ll have your personal websites up and running in a matter of hours.

About the author

Awais Khan

I'm an Engineer and an academic researcher by profession. My interest for Raspberry Pi, embedded systems and blogging has brought me here to share my knowledge with others.