Thuta Learning
AdvancedHardwarebeginner

Setting Up a Web Server on Pi

Relax. We'll talk through this in plain words — no textbook voice.

What you'll walk away with

  • Understand Setting Up a Web Server on Pi without any of the intimidation
  • Be able to run the hardware wiring/code yourself
  • Apply this concept right away in a real project

Let's think about it this way for a moment

Run Flask (a lightweight Python-based web framework) on a Raspberry Pi and you can display sensor data, an LED control interface, or a home dashboard as a web page — you might recall the 'web server' concept from the Docker tutorial; the difference here is that it's running directly on the Pi, no container needed. Run the Flask app with host='0.0.0.0' and any device on the Pi's local network can reach it via its IP address (not just localhost).

Let's connect it to a real scenario

Install with pip install flask, create a Flask app object in your Python script, define a route (@app.route('/')), and wire an LED control button (an HTML form) into the route logic — then you can visit http://raspberrypi.local:5000 from a phone or computer browser and remotely control the LED. This is the basic pattern behind any IoT dashboard.

Let's look at it together

python
from flask import Flask
from gpiozero import LED

app = Flask(__name__)
led = LED(17)

@app.route('/')
def home():
    return '<h1>Pi LED Control</h1><a href="/on">Turn On</a> | <a href="/off">Turn Off</a>'

@app.route('/on')
def turn_on():
    led.on()
    return 'LED is ON'

@app.route('/off')
def turn_off():
    led.off()
    return 'LED is OFF'

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)
You should see
Visiting http://raspberrypi.local:5000 from a browser (phone/computer) should show the LED control page, and clicking the link should immediately switch the physical LED on/off.

5-minute try-it

Write your own Flask app and run it on the Pi (if you have one) — visit it from a phone browser and try remotely controlling the LED.

A quick word of caution

Only use Flask's debug=True mode for development — leaving debug mode on for a production/always-on Pi project is a security risk (the security misconfiguration concept from the Cybersecurity tutorial).

Easy traps

  • Leaving out host='0.0.0.0' (defaulting to 127.0.0.1), so the server only works from the Pi itself and phones/other devices can't reach it
  • Leaving port 5000 blocked when a firewall is enabled, so external devices can't access it (worth revisiting the ufw lesson from the Linux/Cybersecurity tutorial)

Now try it yourself

Write your own Flask app and run it on the Pi (if you have one) — visit it from a phone browser and try remotely controlling the LED.

You'll know it worked when: Visiting http://raspberrypi.local:5000 from a browser (phone/computer) should show the LED control page, and clicking the link should immediately switch the physical LED on/off.

Setting Up a Web Server on Pi | Thuta Learning