Hosting a Website on a Raspberry Pi

by | Jul 20, 2016

The Raspberry Pi is all about learning and education, hosting your own website right from your Pi can be a great way to learn more about creating your own webpages and the tools involved. If you haven’t already then the first step is to set your Pi up as a Web Server.

While you can host your website on your own home network, if you want it found by the rest of the world you will need a Domain Name and a Domain Name Server (DNS).

In this tutorial I am assuming that you are running the latest version of Raspbian and have the ability to connect to your Pi either through SSH with putty and FTP with Filezilla or directly with a keyboard and monitor. If you haven’t set-up your Pi yet then check out my getting started section.


Domain Names


A Domain name is the first thing you are going to need to start hosting a website on your Pi. Domain names are the friendly naming convention used for addressing web servers and web pages on the internet. The domain name gives every server a memorable and easy to spell address. At the same time domain names hide the more technical IP addresses which most viewers aren’t interested in.

In the case of getting a new website address, while you technically can’t buy domain names outright or permanently, what you can do is to register a name. This is like purchasing a lease from the organization that runs whatever registry the extension is associated with.

To obtain a domain name you need to buy one from a domain name registrar such as Godaddy or Namecheap. Buying a domain name is a fairly simple process, I personally use Namecheap to register my self hosted domains and below I will show you the steps.

First head to the Namecheap website, here you will be greeted with the main page where you can enter the domain that you would like to registrar


Namecheap Domains Front Page


Enter a domain and select search and the website will go and search for that domain name and check to see if it already. If the name you were looking for is already taken then you can always try another name or choose from the suggestions that are presented.


Namecheap website availability and suggestions


You can see that depending on the domain name extension, .com, .online, etc, there is a range of price points, deciding on which one to choose is very much personal preference and what sort of website you are planning to use it for. Add your preferred name to your cart and view cart, here you will be presented the final price of your domain name which will include an ICANN fee. You can also select how long you would like to register the name for and several other options. Namecheap provides WhoisGuard free for 1 year with any purchase of a domain name through them.

When you purchase a domain name you need to provide some personal details that will be recorded in the ICANN Whois database against your domain name, WhoisGuard prevents the domain owner’s information from being harvested by spammers from the publicly available Whois database to send spam.


Namecheap Shopping Cart


The checkout page also offers a range of upsells of additional items for your website, these again are personal preference but should not be required for your own hosting. When you select checkout you will be requested to either login or create an account with Namecheap. Fill in all the requested information in the following pages and complete your purchase through the checkout.


Namecheap checkout page


All going well you should now be logged into your Namecheap account and have completed your purchase of a Domain name.


Domain Name Servers


Now that you have a domain name we need to let the world know that it exists, to do this we need to associate a Domain Name Server (DNS) with the domain name. When someone types your web site into a browser it is the DNS that lets the browser know the IP address that it can be found on. Once you have logged into your Namecheap account in the top left-hand corner of the page you will see your user name, from the drop-down menu select the dashboard and you will see a screen with the domain name you have registered.


Namecheap Dashboard page


In the dashboard screen, select “manage” from the right-hand side of the domain you want to associate with your Pi. Once in the Domain List page, if not already set, choose the Namecheap Basic DNS from the name servers section. Next, you need to select “Advanced DNS” from the top menu. When you are first setting up your DNS, Namecheap doesn’t know the IP of your website and so presents a simple temporary holding page.


Namecheap personal domain list


If your Pi is currently connected to your home network then your IP address to the outside world is most likely provided by your ISP and is not static. This means that your IP address can change for several reasons over time (eg. restarting your router, power failure, ISP changes). To overcome this problem we need to enable Dynamic DNS, this will allow us to create a script that will update the DNS with the new IP address whenever it changes. To enable this, activate the Dynamic DNS option, this will provide you with a password that we can use later for making updates.


Namecheap DNS setup initial page


Next, we need to add some records for the Pi web server to replace the holding page. Select the “Add New Record” button and create an “A Record”, set the host to @ and enter an IP of (we will set the correct IP address later from the PI). Update the “CNAME Record” value to your new domain name. Finally, delete the “URL Redirect Record” from the list.


Namecheap DNS setup page


There is a range of ways to update the “A Record” of the DNS with the current IP address of your network, given that we are doing this on a Raspberry Pi I’ll use a short Python script. Create the following python code using notepad or a similar editor and load it to the home directory of your Pi.


#!/usr/bin/env python

from urllib2 import urlopen

        .format("@", "Your_Domain_Name", "Your_Dynamic_DNS_Passwd"))


Executing this code will not produce any screen output but will update the Namecheap DNS with your current IP address. You can confirm this by logging back into your Namecheap account and checking the “A Record” that we entered earlier, it should now have changed from the generic IP we put in originally to your networks IP address. Entering your domain name into a browser should now show you the webpage that is on your Pi.

While this is fine for getting setup initially we need a way to automatically update the DNS whenever our home network IP Address changes, the Raspian OS on the Pi provides us with a simple way of doing this with the Crontab file. The Crontab will execute instructions at any specified time interval on the PI. To edit the file enter the following command.


sudo crontab -e

If this is the first time you have run crontab it will ask what editor you would like to use, select it’s default recommendation and at the bottom of the file add the following line of code.


*/30 * * * * python /home/pi/ &


This will tell your Pi to update the current IP address with your DNS every 30 minutes.

Finally, now the DNS is pointing to the correct address it is necessary to tell your home router to send incoming website requests to your Pi. To achieve this you need to do what is called “Port Forwarding”, setting this up is different depending on the home router that you are using but the basic process is as follows:

  • Log into your router
  • Select the advanced setup option
  • Select “Port Forwarding” or similar
  • Add an entry to forward “Port 80, HTTP requests” to the IP address of your Pi

With your router set up to forward webpage requests to your Pi you should now have your hosted website fully configured and visible on the internet.

The python code shown here is available on my Hydropi Github Repository.


If you have any thought’s about this article, improvements or errors let me know in the comments below and if you found this helpful, why not share it with others.

Pin It on Pinterest

Share This