🛠 Serveur web léger : nginx ou Caddy sur le Pi 2
Objectives
Turn the Pi 2 into a home web server. Its 1 GB of RAM and quad-core give it real headroom for a light service, but its 100 Mbps Ethernet shared with the USB bus caps network throughput: choosing a lean server (nginx or Caddy, not heavy Apache+PHP) is strategic. You will install the server, a static site, a systemd service, and measure the real throughput.
Hardware
- Raspberry Pi 2 Model B running 32-bit Raspberry Pi OS, stably powered
- Ethernet cable (WiFi would require a dongle)
- SSH access (see the SSH/network tutorial, especially to pin the MAC on v1.2)
Full code
# Option A : nginx (leger et classique)
sudo apt install nginx
sudo bash -c 'echo "<h1>Bonjour depuis le Pi 2</h1>" > /var/www/html/index.html'
sudo systemctl enable --now nginx
# Option B : Caddy (HTTPS automatique, binaire unique)
curl -1 https://r.caddy.sh | sudo bash -s
sudo bash -c 'echo "Pi2.example.com { root * /srv/pi2 }" > /etc/caddy/Caddyfile'
sudo systemctl enable --now caddy
# Vérifications locales
curl -I http://localhost
systemctl status nginx | head -n 5
free -h # 1 GB de RAM : garder de la marge
# Débit réseau réel (attendu ~90 Mbit/s max)
sudo apt install iperf3 && iperf3 -c 192.168.1.10
Expected verification
curl -I http://localhost returns HTTP/1.1 200 OK; from another machine, http://IP-du-Pi displays the page. nginx consumes a few tens of MB, leaving 3/4 of the 1 GB available. iperf3 approaches 90 Mbit/s: beyond that, it is not the server but the 100 Mbps Ethernet port (shared USB bus) that limits.
Common errors
- Confusing the network ceiling with a slow server: at 100 Mbps shared with the USB ports, one large download saturates everything; size the uses accordingly.
- Forgetting a stable DHCP lease: on a v1.2 board with a random MAC, the server IP changes on its own — pin the MAC first.
- Stacking PHP, MySQL and WordPress on 1 GB: possible but fragile; for a light service, static or reverse-proxy first.
- HTTPS certificate failing behind a box: DNS must actually point at the server; locally, a reverse proxy with an internal certificate is simpler.
- Serial reboots under load: sign of an insufficient 5 V supply (undervoltage), not a nginx bug.
Other tutorials for this board
Code examples for this board
led_blink.pyPythonbutton_led.pyPythonservo_sweep.pyPythondht_temp.pyPythonoled_i2c.pyPythonmcp3008_read.pyPython