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

Hosting Your First Website on a Raspberry Pi: A Journey from Basement to the World (Part 1)
Photo by Matt Howard / Unsplash

Introduction

Hosting a website from your basement on a Raspberry Pi (Rpi) and making it accessible to the world is an exciting and rewarding project. Whether you're a beginner or a seasoned developer, running your website on a Raspberry Pi introduces you to various aspects of web hosting, including containerization, networking, and security.

In this series, we'll walk you through setting up a sample website on your Raspberry Pi and making it accessible over the internet. We'll focus on using containerized services to ensure consistency across different environments and machines. By the end of this guide, you'll have a fully functional website running on your Raspberry Pi, accessible to the world.

What We'll Cover

  1. Setting Up a Sample Flask App Inside a Container: We'll create a simple Flask web application, containerize it using Docker, and expose it on the Raspberry Pi.
  2. Configuring Network Settings for External Access: We'll configure your router for port forwarding. We'll also set up dynamic DNS to handle changes in your public IP address.
  3. Setup Nginx for Reverse Proxy: We'll set up Nginx to forward requests from your domain to your Flask app.

Prerequisites

Before we start, ensure you have the following:

  • A Raspberry Pi with Raspbian OS installed.
  • Docker and Docker Compose installed on your Raspberry Pi.
  • Python installed on your Raspberry Pi.
  • Basic knowledge of Docker and Flask.
  • A registered domain and access to a DNS provider like Cloudflare.

If you haven't installed Docker or Python on your Raspberry Pi, there are numerous guides available online to help you get started. We recommend doing some research to complete these installations.

Setting Up a Sample Flask App Inside a Container

Step 1: Creating a Sample Flask Application

Let's start by creating a simple Flask application. Create a directory for your project:

mkdir my_flask_app
cd my_flask_app

Inside this directory, create the following files:

  • app.py:
from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello, World!'

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)
  • requirements.txt:
codeFlask==2.0.1
  • Dockerfile:
FROM python:3.8-slim

WORKDIR /app

COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt

COPY . .

CMD ["python", "app.py"]
  • docker-compose.yml:
version: '3.8'

services:
  web:
    build: .
    ports:
      - "5000:5000"

Step 2: Building and Running the Container

Navigate to your project directory and build and run your container using Docker Compose:

docker-compose up --build -d

This command builds the Docker image and starts the container in detached mode. Your Flask app should now be running and accessible at http://localhost:5000.

Step 3: Accessing the Flask App

You can verify that your Flask app is running by accessing it from your Raspberry Pi's web browser or by making a curl request:

curl http://localhost:5000

Since the Flask app is exposed on all interfaces (0.0.0.0), it will also be accessible on your Raspberry Pi's private IP address. To find your Raspberry Pi's IP address, run:

hostname -I

You can then access your Flask app from any device on your local network by navigating to http://<RPI_PRIVATE_IP>:5000.

Step 4: Configuring a Static IP for Your Raspberry Pi

To ensure your Raspberry Pi always has the same IP address on your local network, you need to configure a static IP. This can be done through your router's admin interface. The steps to set a static IP vary depending on your router model, but generally, you will:

  1. Log in to your router's admin interface.
  2. Find the DHCP settings or IP reservation settings.
  3. Add a new reservation for your Raspberry Pi using its MAC address and the desired IP address.

This ensures that your Raspberry Pi will always receive the same IP address from your router, making it easier to manage and access your services.

Conclusion

In this part, we've set up a simple Flask application inside a Docker container and made it accessible on your local network. We've also configured your Raspberry Pi to have a static IP address. In the next part, we'll set up Nginx as a reverse proxy to forward requests from your domain to your Flask app.

Stay tuned for the next part!

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

Happy hosting!