Many people are using their Raspberry Pi as a home web server. It's a great way to host a small site without having to leave a PC running 24/7. If you want people to see your Raspberry Pi web site on the internet, you need to forward a domain name to your home IP address, and then forward a port on your router to your Pi. In this article I'm going to assume that you've already set up a web site and you need to just need to make it accessible on the web.
The diagram on the right shows a typical network set up for a Raspberry Pi. The Pi is connected to a router by wifi or ethernet. I use ethernet for my server as it's more stable.
The router has an external IP address which it uses to communicate with the internet, and it has another IP address which it uses to communicate with your home network. Let's say the router's external IP address is 126.96.36.199. The router's internal address acts as a gateway for all the devices on your network. When your PC requests data from the internet, requests are automatically sent to the gateway address, and the router forwards the request to the internet. The gateway address is usually something like 192.168.0.1.
Most home routers have a firewall built-in. This stops people from outside of your home network connecting to your PC, but it allows your PC to make outgoing connections to web servers.
The external IP address of your home router is usually assigned by your ISP's DHCP servers. If you need to restart your router, it will make a new DHCP request to your ISP, and it may be given a new IP address.
How are web requests processed?
When a user visits a web site, they use the site's domain name to address the site. Their browser then sends a request to a DNS server to translate the domain name into an IP address. Once the browser has the site's IP address, it sends a request to the server.
There are two problems with this:
- 1. the external IP address of your home network can change at any time, and
- 2. your Pi is behind a firewall and can't be accessed directly from the internet.
The first problem can be solved by using a dynamic DNS service. These services act as DNS servers that update automactically if your IP address changes. There are numerous dynamic DNS services available. Most of them provide a free service where you can choose a subdomain name.
You need to install a small piece of software on your Pi. This software is supplied by your dynamic DNS provider, and it monitors the external address of your router. If that address changes, the software sends a message to the dynamic DNS server to update its records.
A dynamic DNS system specifically for Raspberry Pi servers is due to be launched in October/November 2013. You can read more about this at rpidns.co.uk.
When a user on the internet wants to see your web site, their browser can get the IP address of your home network from your dynamic DNS service. But requests still need to find a way through your router's firewall.
By default web servers listen for connections on port 80. When a user's request to see a page on your site reaches your router, the router must forward that request to your Pi's IP address. You don't want to forward all incoming traffic to your Pi, just the traffic for the web server which is on port 80. Most routers allow you to set up port forwarding so that any traffic on port 80 will be sent to your Pi. Instructions on how to do this vary from one router to the next, but you can find some pointers at wikiHow.com.
In most routers there are options to use an existing forwarding rule for HTTP traffic on port 80. If the rule doesn't already exist, you'll need to create one. If you are prompted to choose between TCP and UDP, pick TCP. You'll need to specify your Pi's IP address, so you need to give your Pi a static IP address.
Note that some ISPs block port 80 to prevent their customers from running web servers (to reduce bandwidth usage). In this case, you need to configure your server to listen on a different port, say port 87. You would need to set up your router to forward traffic on port 87, and you would need to configure your dynamic DNS service to forward requests on port 80 to port 87.
You may need to restart your router for changes to take effect.
You can test your set-up by opening a web browser and going to your domain name. If all is well, you should see your site appear in your browser.
Once everything's working, you might want to make it easier for people to find it. You can start by setting up an account in Google Webmaster tools and submitting a sitemap. You could also register your site on pirepository.com, a site that lists web sites that run on Raspberry Pi servers.