Hosting a Website on a Raspberry Pi
Raspberry Pi Website Hosting
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 a 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).
To configure the RPi I am assuming that you are running the latest version of Raspian 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 RPi yet then check out my getting started section.
Domain Names
A Domain name is the first thing you are going to need to 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 organisation 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
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.
You can see that depending on the domain name extension, .com, .online etc there are 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.
The checkout page also offers a range of up sells 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.
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 you 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.
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.
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.
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 127.0.0.1 (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.
There are 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 urlopen("https://dynamicdns.park-your-domain.com/update?" "host={}" "&domain={}" "&password={}" .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 127.0.0.1 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.
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.
This will tell your Pi to update the current IP address with your DNS every 30 minutes.
All going well you should now have your Raspberry Pi hosted website fully configured and visible on the internet.
The python code shown here is available for both 2.x and 3.x 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.
I had no Idea that you can host your own website on a raspberry PI. I have used a Pi for different things including, adding a front end GUI to a conference room at my job. I found this article extremely interesting, and I am looking forward to trying it in the near future.
Thanks Justin
Glad you found it helpful