Skip to content

Instantly share code, notes, and snippets.

@iamkominn
Last active March 24, 2025 03:17
Show Gist options
  • Save iamkominn/00264dcd7685a5fb7b23042640e17336 to your computer and use it in GitHub Desktop.
Save iamkominn/00264dcd7685a5fb7b23042640e17336 to your computer and use it in GitHub Desktop.
Quick and lightweight ways to run an ad-hoc web server directly from the Linux command line to serve static files or directories

1. Python’s Built-in HTTP Server

Python 3:

# Serve current directory on port 8000
python3 -m http.server 8000

# Bind to a specific interface (e.g., localhost only)
python3 -m http.server 8000 --bind 127.0.0.1

# Serve a specific directory
python3 -m http.server 8000 --directory /path/to/dir

Python 2 (deprecated but still works):

python -m SimpleHTTPServer 8000

2. PHP Built-in Server

# Serve current directory on port 8000
php -S 0.0.0.0:8000

# Serve a specific directory
php -S 0.0.0.0:8000 -t /path/to/dir

3. Ruby’s un (WEBrick) Server

ruby -run -e httpd . -p 8000

4. Node.js http-server (Requires npm)

Install once:

npm install -g http-server

Then:

http-server -p 8000

5. BusyBox HTTP Server

For systems with BusyBox (common in minimal Linux distros):

busybox httpd -f -p 8000

6. One-Liner with nc (Netcat)

Warning: Only serves a single file (no directory listing) and exits after one request:

# Serve a single file (e.g., index.html)
{ echo -ne "HTTP/1.0 200 OK\r\n\r\n"; cat index.html; } | nc -l -p 8000

7. webfs (Tiny HTTP Server)

Install:

sudo apt install webfs  # Debian/Ubuntu

Run:

webfsd -F -p 8000

8. darkhttpd (Minimalist Server)

Install:

sudo apt install darkhttpd  # Debian/Ubuntu

Run:

darkhttpd /path/to/dir --port 8000

9. socat (Advanced Networking Tool)

Serve a single file:

socat TCP-LISTEN:8000,reuseaddr,fork SYSTEM:"echo HTTP/1.0 200; echo Content-Type\: text/plain; echo; cat index.html"

10. SSL/TLS Support (Python with openssl)

Generate a self-signed cert:

openssl req -x509 -newkey rsa:4096 -nodes -out cert.pem -keyout key.pem -days 365

Run Python server with SSL:

python3 -m http.server 8000 --bind 0.0.0.0 --directory /path/to/dir --certfile cert.pem --keyfile key.pem

11. Caddy

Caddy is a full webserver but it can be used in a oneliner.

caddy file-server --listen :8080

With Browsing Enabled (Directory Listing)

caddy file-server --listen :8080 --browse

Use Cases

  • Quick Testing: Python or PHP servers (e.g., python3 -m http.server).
  • Single File: nc or socat.
  • Minimal Overhead: busybox httpd or darkhttpd.
  • SSL: Python with self-signed certificates or Caddy

Notes

  • Most servers bind to 0.0.0.0 (all interfaces) by default. Use --bind 127.0.0.1 (Python) or -l localhost (darkhttpd) for local-only access.
  • Stop the server with Ctrl+C.
  • These are not secure for production use (no auth, rate limiting, etc.).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment