UMHack
Published in

UMHack

This is how I turned my Raspberry Pi into a Web Server

One day I was telling our new computer networks lecturer at the university of HELP that we have an entry-level web server that can be used by students to deploy their web apps (I was so happy), then he told me that anything can be a web server even the raspberry pi. Let’s discuss and see how this can be done.

What is a web server?

From a hardware standpoint, any piece of hardware with a network connection can be a web server. Web serves are not about hardware, although certainly some hardware is better suited to run large applications at scale. Instead, a web server is just a piece of software that serves a web content. To serve web contents, a web server needs to listen on a port for a request that is sent via transport protocol and returns a response contains a requested resources.

What does Raspberry Pi need to be a web server?

Step 1: Install Raspberry Pi and Apache Web Server

As we mentioned before, we need some pieces of software as well as to do some configurations in order to have a working web server. So step 1 is to install Raspberry Pi. I already installed the Raspberry Pi OS 32-bit (you can do from here), all I need now is to install a web server. I prefer to use Apache, the world’s most popular web server (here’s a list of other web servers you can use). Install Apache by running the following commands.

sudo apt-get update sudo apt install apache2 sudo service apache2 start

If it is successfully installed, you should see like the following pic when you type localhost or 127.0.0.1 in your browser:

Apache2 is working in localhost or 127.0.0.1

Step 2: Make the web server publicly accessible.

So far we have been serving our web page on local machine (both browser and server), now make it publicly accessible from anywhere. We should open up our router’s home page by typing the router’s IP address and logging in using the username and password (default is admin for both). Now go to the tab that says “Virtual Server” or “Port Forwarding”.

There will be a table-like form, and enter the following:

  • Service type: HTTP
  • External port: 80
  • Internal port: 80
  • Internal IP: The Raspberry Pi local IP (run ifconfig in the terminal)
  • Protocol: TCP

By doing this, we are actually telling our router to forward any incoming request on our public IP for port 80, to the Raspberry Pi. Let’s test this by checking our public IP and typing it in the web browser:

Checking the public IP address

Test it in the browser:

The Apache2 is working on the public IP address

Step 3: Get our free domain and bind it to our public IP.

Time to get a free domain name for our server. As of right now, we can get a free domain from a free DNS provider like no-ip. We should visit the website and sign up for a domain:

Sign up for a free domain

After that we can bind that domain to our public IP:

Bind the domain to the public IP
Apache2 is working using the free domain

Now we have this web server up and running, we can use it to host a static website like my portfolio website www.labibmansour.com, by adding the website files to the Apach2 directory /var/www/.

Step 4: Assign a static IP address to the Raspberry PI.

Each time we restart the Raspberry PI, the private IP address may change, for this reason we need to assign a static IP address to Raspberry PI, otherwise we need to change the router setting every time the private IP address gets changed.

Open the terminal and navigate to /etc/dhcpcd.config and edit it.

sudo nano /etc/dhcpcd.config 

Conclusion

This worked with me using my Raspberry PI, it can be applied on any pc or laptop though. Let me know in the comments how it worked out for you.👌

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Labib Mansour

Labib Mansour

Associate Data Engineer | Builder 🚀