Hosting Your First Website on a Raspberry Pi: A Journey from Basement to the World(Part 2)

Hosting Your First Website on a Raspberry Pi: A Journey from Basement to the World(Part 2)
Photo by Urban Vintage / Unsplash

Configuring Network Settings for External Access

Before we start, ensure you have completed the first part of this series and have a sample app running and accessible on your Raspberry Pi's localhost.

Introduction

In the previous part, we set up a sample Flask application inside a Docker container and made it accessible on your local network. In this part, we’ll focus on configuring your network settings to allow external access to your Raspberry Pi.

This involves mapping your domain to your router's public IP, configuring your router to forward requests to your Raspberry Pi, and setting up dynamic DNS to handle changes in your public IP address.

Step 1: Configuring a Static IP for Your Raspberry Pi

Ensure your Raspberry Pi has a static IP address on your local network. This was covered in Part 1, but it's crucial for the upcoming steps.

Step 2: Mapping Your Domain to Your Router's Public IP

To make your website accessible via your domain name, you need to update your DNS settings to point your domain to your router's public IP address.

Creating an A Record

  1. Log in to your DNS provider’s dashboard (e.g., Cloudflare).
  2. Select your domain and navigate to the DNS settings.
  3. Create a new A record:
    • Type: A
    • Name: @ (or yourdomain.com)
    • Value: Your router's public IP address
    • TTL: Automatic or 1 hour

Step 3: Allowing Inbound Requests on Your Router

Many routers have inbound HTTP/S requests disabled by default for security reasons. You need to enable this setting to allow external access to your website.

Enabling HTTP/S on Your Router

  1. Log in to your router’s admin interface.
  2. Navigate to the settings related to security or firewall.
  3. Find the option to allow inbound HTTP/S requests on the WAN (public) IP address.
  4. Enable the setting for HTTPS, and optionally HTTP (though HTTPS is recommended for security).

Step 4: Configuring Port Forwarding

Next, configure your router to forward requests on specific ports to your Raspberry Pi.

Setting Up Port Forwarding

  1. Log in to your router’s admin interface.
  2. Navigate to the port forwarding section.
  3. Add a new port forwarding rule:
    • Service Name: HTTP
    • External Port: 80
    • Internal IP Address: Your Raspberry Pi’s static IP
    • Internal Port: 80
    • Protocol: TCP
  4. Add another rule for HTTPS:
    • Service Name: HTTPS
    • External Port: 443
    • Internal IP Address: Your Raspberry Pi’s static IP
    • Internal Port: 443
    • Protocol: TCP

Step 5: Setting Up Dynamic DNS

Since your router's public IP address can change periodically, it’s essential to keep your DNS records updated. We can automate this process using a dynamic DNS script.

Setting Up a Dynamic DNS Script

Use a script like in this repository to dynamically update your ip address in the dns.

Conclusion

In this part, we've configured your network settings to allow external access to your Raspberry Pi. We've mapped your domain to your router's public IP, enabled inbound requests on your router, set up port forwarding, and configured dynamic DNS to handle IP changes.

In the next part, we'll set up Nginx as a reverse proxy to forward requests from your domain to your Flask app.

If you have any questions or run into any issues, feel free to comment below, and I'll be happy to help.

Happy hosting!