Raspberry Pi Apache2 Web Server on Raspbian, Part 2

If you followed along with my last tutorial, you should have a basic Apache HTTP server set up on your Raspberry Pi with a simple webpage visible when you enter the IP address of your Pi on a computer connected to your router. Today, we’re going to make that page visible to the world.

As things stand now, we have an HTTP server that is broadcasting our website to the world. The problem is the world doesn’t have a route to our server.

Part 1Part 3Part 3.5

Getting past the router.

I’m assuming that most readers are using a router to connect to the Internet. As of 2013 there were over 50 million wireless routers being used in the US alone, and that number has probably grown over the past 6 years. So, before we go any further, let’s take a look at what a router is and what it does.

Typical wireless router

At the most simple level, a router is a piece of hardware (typically) that manages network traffic. It is the interface between all of our home devices and the Internet. Because it sits between our devices — including our Raspberry Pi — and the wider Internet, it also acts as a first line of defense, protecting our devices from malicious outside attacks.

Although loading a web page is slightly more complicated than this, for this tutorial, this is all we need to know.

As our portal to the Internet, our router is given a public IP address by our Internet service provider (ISP). As the manager of network traffic, it also assigns a private (also known as local) IP address to all of the devices connected to it. When we visit a web page, google.com for example, our computer sends that request to the router which forwards that request to Google’s servers. Attached to that request is our public IP address so Google knows where to send the web page that was requested. Our router then receives that response and delivers it to our computer.

What are ports and why do we care?

When computers (cell phones, smart TVs, laptops) communicate with each other, they need to rely on and follow established rules of communication. Email and web requests are very different from each other from a digital perspective, and when our computer receives this information, there needs to be a way for it to know what to do with the information. This is where ports come in.

Services running on our computers have a port number assigned to them. Those services listen for network data that carries a matching port number. Commonly used Internet protocols have port numbers assigned to them so that our software services can work. HTTP, for example, traditionally uses port 80 for communication. When we installed our Apache server, it was set by default to listen for HTTP requests on port 80. To put it another way, whenever our computer receives data from the network, that data has a destination port attached to it. If that port is 80, our Apache server knows that the incoming data is for it, and it knows how to process it.

Other common ports that you may work with in the future include:

  • Ports 20 and 21: FTP (file transfers)
  • Port 22: SSH (secure shell)
  • Port 23: Telnet
  • Port 25: SMTP (email)
  • Ports 88 and 3074: Xbox Live
  • Port 25565: Minecraft

This is a good time to check if our ISP allows traffic on port 80. If they don’t, the procedure for getting around it will be beyond the scope of this article. Visit this link to check if port 80 is open on your network.

So how does this help us make our server visible?

When we type in our Raspberry Pi’s IP address into a web browser that’s not connected to our network, the web browser won’t be able to find it because it’s a private IP address that exists only on our network and isn’t visible to computers outside of our network. If we find our public IP address, using a tool like whatismyip.com, and put that into our web browser we still won’t be able to connect, even though our server is up and running. We could even type in the public IP address (let’s say 124.277.36.197) and specify which port to connect to (124.277.36.197:80) and still not be able to connect.

This is because when the router (the public face of our network) receives a request for data on port 80 it doesn’t know what to do with it. This is where port forwarding, or port mapping, comes into play. When our router receives a request for data on port 80, we need to tell it to forward that request to our Raspberry Pi.

There’s still one more thing we need to do before we can set up port forwarding on our router. To set up port forwarding we have to know which local IP address to send the requests to. In our last tutorial we learned the command hostname -I to find the IP address of our Pi. Unfortunately, that IP address is assigned by the router and there’s no guarantee it will be the same from day to day. We need to tell the router to assign the same address to our Pi every time it connects to the network.

To do this, we need to know about two things: DHCP and MAC addresses.

Our router uses a system called DHCP (Dynamic Host Configuration Protocol) to assign IP addresses to devices as they connect to the network. The router typically doesn’t care which device gets which IP address.

Every device that connects to the Internet has a unique identifier called a MAC address (Media Access Control). The MAC address takes the form of six sets of 2-digit hexadecimal numbers, typically separated by colons. Our Raspberry Pi has two MAC addresses, one for ethernet and one for Wi-Fi. To find the MAC address of the ethernet port type ip link show eth0 into the Pi terminal. For Wi-Fi, type ip link show wlan0.

The ip command is used to interface with the network options of your Pi. link is an argument that tells the ip command to look at a network device. show is an argument to link that makes the ip command show information on network devices. wlan0 and eth0 are the specific devices we’re asking ip link show to display. Type ip --help and ip link help into your terminal for more details on these commands.

Now that we have our Raspberry Pi’s unique Wi-Fi or ethernet MAC address, we can reconfigure our router to assign a permanent local IP address to our Pi whenever it’s connected to our network. I can’t tell you how to access your router configuration screen, you’ll have to check your documentation or do a web search: “how to access ROUTER BRAND’s configuration screen.” The configuration screen is often 192.168.1.1 but it varies between brands.

Once we’ve found the address to access the configuration screen, we’ll have to enter a username and password. Unless we’ve changed these setting ourselves, the user name will be predefined by the manufacturer and available online. The password will likely be our Wi-Fi password. Some routers set the default user name and password to “admin.” Search online to find out specifics on your brand.

If one has never explored the configuration page of their router, it can be a bit overwhelming at first. Take time to look around but don’t worry too much about the details. Routers run on a special type of software called firmware. Firmware is typically used to control hardware accessories like routers and printers. Each router manufacturer has their own firmware which makes their configuration screens look different. It’s even possible to download third-party firmware for your router.

This is what my Static IP Configuration page looks like on my Huawei router

What we’re looking for are the DHCP settings. Remember, DHCP is the protocol that the router uses to assign IP addresses to devices that connect to it. We’re looking for something with a name similar to “Static IP Configuration.” When setting this up, my router only asks for the MAC address of the device I want to assign a static IP address to, and the IP address I want to assign to it. Your router might be different. Don’t be afraid to experiment a bit with this setting.

Port Forwarding/Port Mapping

We’re almost done, I promise.

Now that we’ve locked down our local IP address, we can set up the port forwarding so that when our network receives a request on port 80, it knows to route that data to our Raspberry Pi machine that’s running the Apache HTTP server.

Go back to the router configuration page. This time we’re looking for something that says “port forwarding” or “port mapping.” Remember, this is going to be different for every brand of router so we might need to do some web searching if we’re having trouble. In general, we’re going to need:

  • the static IP address we just made for our Raspberry Pi
  • a name for the forwarding rule we’re about to create (something easy to remember)
  • the port we want to forward to the IP address we indicated

Again, don’t be afraid to experiment with these values if they don’t look like your expectations. There’s also a site, portforward.com, that gives basic instructions tailored to each brand of router. Once you think you’ve got the settings entered correct, check if it’s working by typing your public IP address into the address bar of a web browser. If you see your website, congratulations! You’re all set and it should be available in most of the world.

Next: Giving our website a name

3 thoughts on “Raspberry Pi Apache2 Web Server on Raspbian, Part 2

Comments are closed.