Raspberry Pi

How to convert Raspberry Pi to a personal web server

People thought that Raspberry Pi can only be used to build projects and play video games but they have never imagined that Raspberry Pi will go beyond that and transforming your Raspberry Pi into a web server is one of its capabilities that will increase your Raspberry Pi worth.

A web server stores and hosts the contents of a website such as images, HTML, CSS and JavaScript documents. It fulfills the queries of clients accessing web content and all this is done through various protocols like Hypertext Transfer Protocol (HTTP/HTTPS).

Convert Pi to a personal web server

Making your Raspberry Pi to act as a personal web server is an excellent option as it allows them to deliver their contents on their personal website. If you are eager to create your own web server, then you should read this article that will lead you to transform your Pi to a personal web server.

To successfully convert your Pi into a personal web server, you will need to look at the below given steps which you will have to execute through the terminal of your Raspberry Pi device.

Step 1: Turn on your Raspberry Pi device through an external power supply and make sure that the power supply will be good enough in order to make Raspberry Pi running for a longer time. If the power supply won’t provide enough voltage to the device, it might turn off and your installation will stop during that time.

Step 2: Now, the next step is to ensure that your Raspberry Pi has the latest version of packages and in order to do that you should execute the below mentioned command in the terminal of Raspberry Pi:

$ sudo apt update

Step 3: After the update, you will also be required to run the upgrade command to ensure that your packages will be upgraded and to perform this step, execute the below mentioned command in the terminal:

$ sudo apt upgrade

Step 4: After performing the above steps, you will now be required to install Apache in your Pi device as Apache will handle all your network traffic and ensure that you will run a website without any worry. In order to install Apache, you will need to execute the following command:

$ sudo apt install apache2

The above command will install the Apache in Raspberry Pi.

Step 5: Now, in order to ensure that your function on your website goes well, you will need to install PHP in your Raspberry Pi device as PHP will allow you the freedom to select your web server according to your choice and it will also help you to connect with your database. To successfully install PHP, you will be required to execute some commands which are given below.

Now, you will need to install GPG key which you will find in Repository of PHP and following command will be required to be executed in the terminal:

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

After successfully saving the key, you will now need to create a source file that will point towards this repository and to do this, execute the following command in the terminal:

$ 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

After adding the packages, it will be necessary to execute the update command in the terminal:

$ sudo apt update

Then, run the “upgrade” command to ensure packages are upgraded:

$ sudo apt upgrade

Now, you are ready to install PHP through its repository using the below mentioned command which will need to be executed in the terminal.

$ sudo apt install -y php8.1-common php8.1-cli

Step 5: Now you will need to integrate PHP with MySQL in the next step using the below mentioned command:

$ sudo apt install -y php8.1-mysql

Step 6: Next, integrate PHP with Apache through the below given command:

$ sudo apt install -y libapache2-mod-php8.1

Step 7: After performing the above steps without encountering an error, you will now need to download the MariaDB server, which will provide you with more storage engines. To have it downloaded on your Raspberry Pi device, you will need to execute the command mentioned below in the terminal:

$ sudo apt install mariadb-server

Step 8: After the installation of MariaDB server, you will need to secure your database through executing the below mentioned command in the terminal:

$ sudo mysql_secure_installation

When you execute the above command in the terminal, you will be asked to press “y” or “n” on some process, it’s up to your which you want to opt, in our case we first enter the password and in the next two steps we choose “n” option while in the rest option we select “y”. You can go with our choices if you are unaware of what to do. The images of our selections are provided below.

Step 9: Next, you will have to restart the Apache service using the below given command in the terminal:

$ sudo service apache2 restart

Step 10: In the next step, it is necessary to check whether Apache is running or not and to do this you will need to execute the given command in the terminal:

$ sudo service apache2 status

Step 11: Now, you are required to enter the Raspberry Pi device IP address in order to check your website. To find the device IP address, you will need to run the below given command in the terminal.

$ hostname -I

After finding the host IP address, open any browser whether from the device or from your laptop or PC and add the above IP “192.168.43.96” into the browser’s address bar. As soon as you enter the IP address on your browser, you will see the default apache’s web page as shown below.

Step 12: Now, the next thing is to start creating your personal web page and in order to do that, you will need to find the html directory into your Raspberry Pi system. Enter “/var/www/html” in the Raspberry Pi directory search box.

Step 13: The website developing on your Pi device is possible through PHP and you can create a PHP webpage in the folder where your html file is placed. To perform this step, you will need to add the command in the terminal which is given below:

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

When you press enter, a file will be open on your terminal screen with the name “index.php”. You will need to write PHP code in it so that it will be displayed on your website.

After entering, you will then need to save it using “Ctrl+X” then enter “Y” and press enter again to save the changes to the file.

Step 13: In the final step, enter the same IP address on any browser along with the file name after forward slash and this time, you will see that your website has changed and the message will appear on your browser which you have typed in the above file as shown below.

Conclusion

Raspberry Pi device is all in one a handy tool that can be pretty cool if used as a web server where you can easily build your own websites through PHP. The aforementioned methods will provide significant guidance in creating your own webpage in a few minutes. To turn Raspberry Pi into a web server you will need to ensure that different technologies are installed properly such as Apache web server (to host a webpage), database management system MariaDB (database management system) and PHP (server side language). After having these technologies on Raspberry Pi you can easily develop and host a webpage. If you want to view the webpage from another desktop browser, you should provide an IP address with the PHP file name to access the webpage.

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.