How to Create a Web Server on Raspberry Pi Quickly
To create a web server on Raspberry Pi, you can use Python's built-in
http.server module by running python3 -m http.server in the terminal. This starts a simple web server serving files from the current directory accessible via your Pi's IP address.Syntax
The basic command to start a simple web server on Raspberry Pi using Python is:
python3 -m http.server [port]
Explanation:
python3: Runs Python version 3.-m http.server: Uses Python's built-in module to start a web server.[port]: Optional port number (default is 8000).
bash
python3 -m http.server 8000Example
This example shows how to start a web server on port 8080 and access it from another device on the same network.
bash
python3 -m http.server 8080Output
Serving HTTP on :: port 8080 (http://[::]:8080/) ...
Common Pitfalls
Common mistakes when creating a web server on Raspberry Pi include:
- Not running the command in the directory with your website files, so the server serves the wrong content.
- Using a port number below 1024 without
sudo, which requires admin rights. - Firewall or network settings blocking access to the chosen port.
- Not knowing your Raspberry Pi's IP address to access the server from other devices.
Always check your Pi's IP with hostname -I and ensure the port is open.
bash
Wrong: python3 -m http.server 80 Right: sudo python3 -m http.server 80
Quick Reference
| Command | Description |
|---|---|
| python3 -m http.server | Start server on default port 8000 |
| python3 -m http.server 8080 | Start server on port 8080 |
| sudo python3 -m http.server 80 | Start server on port 80 with admin rights |
| hostname -I | Show Raspberry Pi IP address |
| Ctrl+C | Stop the running server |
Key Takeaways
Use Python's built-in http.server module to quickly start a web server on Raspberry Pi.
Run the server command in the folder containing your website files to serve correct content.
Use ports above 1024 to avoid needing admin rights, or use sudo for lower ports.
Check your Raspberry Pi's IP address with hostname -I to access the server from other devices.
Ensure your network and firewall settings allow connections to the chosen port.