The Raspberry Pi can be used as a web server on your main local network or on the Internet in general. It’s a great selection in cases where you want an intranet for the office or a web development server.
You can create a local Pi web server to deliver various content while browsing the Internet. For the web server to be operational, the Raspberry Pi must be connected to the local network. And, he assured that he has the latest version of the Raspbian operating system. These are the defined instructions you need for any model to work even on the pocket Raspberry Pi Zero W and the very powerful Raspberry Pi 4.
Table of Contents
Prerequisites
- Introduction
- Configuring Apache on Raspberry Pi
- Changing the server hostname
- Conclusion
- Further reading Prerequisites
For readers to move forward, they are assumed to have an operating system installed with the Pixel window manager. In short, you must have:
- A
- Raspberry Pi Zero W
- Apache
- Internet
- Modules such as SSH enabled.
web server.
.
.
Introduction
Several web servers can be related to the Raspberry Pi, and each has advantages of use. This tutorial uses the Apache web server application.
Apache is an open source web server that provides content over the Internet. It can be installed on a Raspberry Pi and can be used to deliver web pages. Apache can handle HTML files over HTTP, and with additional modules, it can help with dynamic web pages written in languages like PHP.
Apache offers the advantage of being able to handle a lot of traffic with less configuration. You can configure it to perform an operation you want and the approach to use. To increase its efficiency on the Raspberry Pi, you can remove modules that are not needed.
Configuring Apache on Raspberry Pi
Step 1: Access
the terminal
This step will describe how to access the terminal of the raspberry pi. This can be achieved in two main ways, as shown below:
- You can access the command terminal by clicking CLRL+ALT+T from the Raspbian desktop.
<img src="https://www.section.io/engineering-education/hosting-a-webserver-using-a-raspberry-pi/terminal.png" alt="Raspberry
Pi Terminal” />
- You can also choose to connect remotely via SSH in case it is enabled on the Raspberry Pi. This is possible through a tool like Putty and then use the raspberry username and password to log in.
Note: The default login credentials for a raspberry Pi using SSH are: Username: pi, Password: raspberry
.
Step 2: Update
packages
In this step, we will make sure that the packages available on our Raspberry pi are updated by typing the following command in the terminal. This ensures that you have the current versions of any files you download later.
Step 3: Install Apache
In the third stage, we will install apache. To install apache2, run the following command
:
Step 4: Install
PHP
This is the point where we install PHP. PHP helps make sure that web pages work as expected and also communicate with the database. Add PHP using the command
: <img src="https://www.section.io/engineering-education/hosting-a-webserver-using-a-raspberry-pi/installphp.png" alt="PHP
Installation” />
Step 5: Download MySQL
After
PHP has been successfully installed, we can now download our desired database. In this tutorial we are using the MySQL database. To download MySQL, run the following command in the terminal
:
Note: When prompted to change the root password, choose a strong password for MySQL. Be sure to write it somewhere you can refer to later.
Step 6: Install MySQL
This stage describes the installation process for the already downloaded database. When the download is complete, a formal installation is required and can be performed using the command
:
Step 7: Restart
the server
At this point, we will restart the web server, apache2, to confirm that the changes take effect and are running. Use the command
:
Step 8: Test
the web server
Now we will test the apache web server. Since Apache has an HTML file in the Apache web subdirectory, you can serve it on the raspberry when browsing HTTP://[Host_IP_Address]/. When you go to the default web server page on the Pi or using another computer, the following page will be displayed if the Apache web server is running.
You can build your website by placing the PHP or HTML files in the /var/www/HTML directory. To make the folder easily accessible to the default user, i.e. the pi, run the command: Sudo chown -R pi/var/www/html
Changing the
server
hostname
The hostname of the Raspberry Pi will be raspberry by default. Because you may have other raspberry pi names on your network, you can make changes by following these steps:
- Type sudo raspi-config in the terminal, and then select the hostname option.
<img src="https://www.section.io/engineering-education/hosting-a-webserver-using-a-raspberry-pi/renaminghost1.png" alt="Changing hostname using Terminal" />
Alternatively, use the start menu to navigate to the Preferences->Raspberry settings to launch the windowed version.
<img src="https://www.section.io/engineering-education/hosting-a-webserver-using-a-raspberry-pi/renaminghost2.png" alt="Host name change using GUI" />
- Choose the hostname you want to have, for example, “myraspserver”, then click OK.
Note: This will help you overcome the warning not to use characters other than numbers, hyphens, or letters. The hyphen is only allowed in situations where it is between a name.
Choose “yes” when prompted to reboot.
Once you have allowed your computer to restart, the Raspberry Pi will now be displayed with its name changed.
Conclusion on
Web hosting offered by the providers of such services is given at a price and, in some cases, can be very expensive. Therefore, it is important to find ways to reduce such costs, especially if the hosted service is only for a small group of people.
By following the steps presented above, it is possible to install and host a web server using a Raspberry Pi. The hosted web server can be used to serve a small office via an intranet and is more cost-effective.
Further reading
Streaming a
- private FM radio station using a Raspberry Pi
Peer review Contributions by: Dawe Daniel