Tools: Python HTTP Server on Ubuntu
Running a lightweight HTTP server is one of the easiest ways to test, present, or prototype your work on Ubuntu. Python makes this process simple. Its built-in HTTP server provides a fast way to serve files, test APIs, or validate network behavior without heavy web frameworks or external services.This article walks through how to set up and run a Python HTTP server on Ubuntu, how to secure and customize it, and how to use it in different real-world scenarios. Why Use Python's HTTP Server?Ubuntu already includes tools like Apache or Nginx, so why use Python instead?The Python HTTP server is ideal when you need:• A quick test environment – Serve a directory over HTTP with a single command. No config files, no services, no packages.• A simple way to demo files – Colleagues can open your work through their browser without SSH or shared drives.• A sandbox for API experiments – You can define a custom handler that responds to GET, POST, PUT, or DELETE requests.• A portable tool – Works the same on Ubuntu, macOS, or Windows. If Python is installed, you’re ready.In short, it’s perfect for learning, teaching, prototyping, and troubleshooting. Setting Up Your Ubuntu EnvironmentMost modern Ubuntu distributions include Python 3 by default. Check your version with: If Python isn’t installed or you want to update it: sudo apt updatesudo apt install python3 For development or testing, it’s helpful to install: sudo apt install python3-pipsudo apt install python3-venv These tools let you create virtual environments or extend the simple HTTP server with Python packages. $ cd /path/to/your/folder$ python3 -m http.server Http Server will start. Linux Console will show: Go to browser, and type: http://localhost:8000. Browser will show: Since there is no index.html, the page shows a list of files in the directory from which the command was typed. To use a different port, type: python3 -m http.server 8080 To allow access from other devices, type: python3 -m http.server 8000 --bind 0.0.0.0 Now the server is live, and anyone who can reach your IP and port can view your directory. To allow external access, type: The command sudo ufw allow 8000 in Ubuntu tells the firewall (UFW) to open TCP/UDP port 8000 for incoming connections, allowing external clients to reach services running on that port. The above command reloads the firewall rules without disabling or re-enabling UFW. For local-only access: python3 -m http.server 8000 --bind 127.0.0.1 `from http.server import BaseHTTPRequestHandler, HTTPServerimport json class SimpleAPI(BaseHTTPRequestHandler): def do_GET(self): if self.path == "/status": self.send_response(200) self.send_header("Content-Type", "application/json") self.end_headers() response = {"server": "running", "path": self.path} self.wfile.write(json.dumps(response).encode()) else: self.send_response(404) self.end_headers() def run_server(): server = HTTPServer(("localhost", 8000), SimpleAPI) print("Server running at http://localhost:8000/") server.serve_forever() if name == "main": run_server()` Test it in your browser: http://localhost:8000/status This structure lets you build mock APIs or test behavior without deploying a full web framework. nohup python3 -m http.server 8000 & Example with systemd: sudo nano /etc/systemd/system/python-http.service Add:[Unit]Description=Python HTTP ServerAfter=network.target [Service]ExecStart=/usr/bin/python3 -m http.server 8000 --directory /var/wwwWorkingDirectory=/var/wwwRestart=always [Install]WantedBy=multi-user.target Enable and start:sudo systemctl enable python-httpsudo systemctl start python-httpsudo systemctl status python-http The server will now start automatically on boot. Python’s default server doesn’t support HTTPS, but you can add it using Nginx as a reverse proxy. sudo apt install nginx Configuration example: server { listen 80; location / { proxy_pass http://127.0.0.1:8000; }} Reload Nginx:sudo systemctl reload nginx Add HTTPS with Let’s Encrypt: sudo apt install certbot python3-certbot-nginxsudo certbot --nginx Now your Python server is securely accessible via Nginx. This command will give process id of program running in port 8000. Stop the running program using the process id using the command below: • Permission denied: Use accessible directories or adjust permissions.
• Cannot access from other devices: Check firewall, bind address, and router isolation. Templates let you quickly answer FAQs or store snippets for re-use. Hide child comments as well For further actions, you may consider blocking this person and/or reporting abuse