Data centre concept image

How to run a web server on Raspberry Pi 4B

The Raspberry Pi can be used as a web server on your main local network or the internet at large. It is a great selection in a case where you want an intranet for the office or a web development server.

You can create a local Pi web server to deliver various contents while you are surfing over the internet. The Raspberry Pi should be connected to the local network to make the web server operational. And, ensure that it has the latest version of the operating system of Raspbian. These are the defined instructions you need for any model to work even on the pocket-sized Raspberry Pi Zero W and the very powerful Raspberry Pi 4.


For you to follow through, you assumed to have an installed operating system with the Pixel window manager. In summary, you need to have:

  1. Raspberry Pi Zero W/ Raspberry Pi 4B (4 to 8 GB variants)
  2. Installed Apache web server
  3. Internet connection
  4. SSH module enabled


Various web servers can be related to the Raspberry Pi, and each has utilization advantages. This tutorial uses the Apache web server application.

Apache is an open-source web server that provides content via the internet. It can be installed on a Raspberry Pi and used to deliver web pages. Apache can handle HTML files through HTTP, and with extra modules, it can help with dynamic web pages written in languages like PHP.

Apache offers the advantage of being able to handle high traffic with less configuration. You can configure it to conduct an operation you wish and the approach to use. To increase its efficiency on the Raspberry Pi, you can eliminate modules that are not required.

Setting up Apache on Raspberry Pi


Step 1: Accessing the terminal

This step will describe how to access the Raspberry Pi’s terminal. This can be achieved in two main ways as shown below:

  1. You can access the command terminal by clicking  CLRL+ALT+T from the Raspbian desktop.
    Image of terminal window
  2. You can also opt to connect remotely through SSH in case it is enabled on the Raspberry Pi. This is possible through a tool such as Putty or Termius and then using raspberry’s username and password to log in.

Note: The default login credentials for Raspberry Pi using SSH are:
Username: pi
Password: raspberry

Termius SSH window

Step 2: Updating packages

In this step, we will make sure that the available packages in our  Raspberry Pi are updated by typing the command below into the terminal. This ensures you have the current versions of any file you download afterward.

sudo apt update

image of sudo apt -update command execution

Step 3: Installing Apache

In the third stage, we will install apache. To install apache2 , we run the following command:

sudo apt install apache2 -y

Installing Apache Server

Step 4: Installing PHP

This is the point where we install PHP. PHP helps in making sure that the web pages function as expected and also communicate with the database. Add PHP using the command:

sudo apt-get -y install php5-common php5-cgi php5

image of installing PHP

Step 5: Downloading MySQL

After PHP has been successfully installed, we can now download our desired database. In this tutorial we are using the MySQL database, however for production I would recommend MariaDB10 as its faster. To download MySQL, run the following command in the terminal:

sudo apt-get install MySQL-server

Image of downloading MSQL database

Note: When prompted to change the root password, please choose a strong password for MySQL. Make sure to write it down somewhere you can refer to later on.

Step 6: Installing MySQL

This stage describes the installation process of the already downloaded database. When the download is finished, formal installation is required and can be done using the command:

sudo mysql_secure_installation

Image of Installing MySQL server

Step 7: Restarting 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:

sudo service apache2 restart

Image of restarting Apache server

Step 8: Testing the web server

We will now put the apache web server to test. Since Apache has an HTML file in the Apache web subdirectory, you serve it on the raspberry when you browse HTTP://[Host_IP_Address]/. When you browse to the default page of the web server on Pi or using another computer, the following page will be displayed if the Apache web server is working properly.

Image of Apache server default page

Yo can build your website by locating the PHP files or the HTML in the /var/www/HTML directory. To make the folder easily accessible to the default user, that is. the pi, run the command:

sudo chowm -R pi/var/www/html

Step 9: Changing the host name of the web server

The hostname of the Raspberry Pi will be raspberry by default. Since you might have other Raspberry Pi devices with the same name on your network, you can make changes using following steps:
Type sudo raspi-config in the terminal and then select the hostname option 

image of server settings menu

Alternatively, use the start menu to navigate to Preferences->Raspberry Pi Configuration to launch the graphical version.

Image of graphical server interface

Choose the Hostname you wish to use, for instance, “exowebstudioserver“, then click OK.

Graphical Interface of renaming host server

Note: This will assist you in getting over the warning about not using characters other than numbers, hyphens, or letters. The hyphen is only allowed in situations where it is in between a name.

Choose “YES” when prompted to restart.

Once you have allowed your device to reboot, the Raspberry Pi will now be shown with its changed name.

Image of prompt to reboot server

Step 10: Final words

Web Hosting offered by providers of such services is given at a price and in some case, it can be very expensive. Hence, it is important to find ways of cutting such costs, especially if the hosted service is 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 through an internet and is more cost efficient. This example can be used for Internal hosted CRM solutions or more.

For more options, please feel free to contact us here at EXOWEB Studio Calgary. We are very customer focused and your success is our priority.

1 thought on “How to run a web server on Raspberry Pi 4B”

  1. Pingback: Umbrel install on Ubuntu Server - EXOWEB STUDIO

Leave a Comment

Your email address will not be published. Required fields are marked *