🛠 Héberger un serveur web léger
Objectives
Transform the Raspberry Pi 3 Model B into a permanent small web server: serve a static site, wrap it in a systemd service (automatic startup + restart on crash), and respect the 1 GB memory constraint which requires lightweight servers. This is the building block for a home dashboard, a Pi-hole-like setup, or a connected mirror.
Hardware
- Raspberry Pi 3 Model B running Raspberry Pi OS Lite, connected via Ethernet (preferred over the 3B's single 2.4 GHz Wi-Fi)
- Stable 2.5 A power supply
- A folder of static files (index.html, CSS)
- Python 3 (included) or
nginx-lightfor a more robust option
Complete code
# 1) Dossier du site
mkdir -p ~/site && echo '<h1>Pi 3B opérationnel</h1>' > ~/site/index.html
# 2) Service systemd — serveur Python intégré, port 8080
sudo tee /etc/systemd/system/pisite.service <<'EOF'
[Unit]
Description=Site statique du Pi 3B
After=network-online.target
[Service]
User=pi
WorkingDirectory=/home/pi/site
ExecStart=/usr/bin/python3 -m http.server 8080 --bind 0.0.0.0
Restart=always
RestartSec=3
MemoryMax=200M
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl daemon-reload
sudo systemctl enable --now pisite
sudo systemctl status pisite --no-pager
# 3) Test local puis distant
curl -s http://localhost:8080/ | head -1
# depuis un autre poste : curl http://raspberrypi.local:8080/
Expected verification
systemctl status pisite displays active (running). curl http://localhost:8080/ returns the site's <h1>, and from another machine on the network, http://raspberrypi.local:8080/ displays the page in the browser. After killing the process, systemd restarts it within 3 seconds. systemd-run/free -m shows the memory footprint below the MemoryMax=200M limit. After a full reboot, the service starts automatically.
Common errors
- Choosing full Apache on 1 GB: the machine saturates; prefer
python3 -m http.server(troubleshooting) ornginx-light(permanent). - Serving over HTTP on port 80 as root: prefer 8080 without privileges, or use
authbind/cap_net_bind_service. Address already in use: another service is occupying the port; check withss -tlnp | grep 8080.- Publishing over congested 2.4 GHz Wi-Fi: the 3B only has a single-band 802.11n antenna; use a 100 Mbit Ethernet cable for a stable service.
- Forgetting
enable: the service starts now but not after a reboot. - Exposing the server to the Internet without a tunnel or firewall: stay local or use a reverse proxy with TLS.
Other tutorials for this board
Code examples for this board
led_blink.pyPythonbutton_led.pyPythonservo_sweep.pyPythondht_temp.pyPythonoled_i2c.pyPythonmcp3008_read.pyPython