← Back to all snippets
BASH

Start a Quick Local Web Server

Spin up a simple local HTTP server instantly using Python's built-in module, ideal for testing static web pages and development.

#!/bin/bash
# Usage: ./start_server.sh [PORT]

PORT=${1:-8000}

echo "Starting simple HTTP server on port $PORT..."
echo "Serving directory: $(pwd)"

if command -v python3 &> /dev/null; then
  python3 -m http.server "$PORT"
elif command -v python &> /dev/null; then
  python -m SimpleHTTPServer "$PORT" # For Python 2
else
  echo "Error: Python not found. Please install Python to use this script." >&2
  exit 1
fi
How it works: This script provides a quick way to start a static file server in the current directory. It first checks for `python3` and uses its `http.server` module, falling back to `python2`'s `SimpleHTTPServer` if `python3` is not found. The server defaults to port 8000 but allows a custom port to be passed as an argument. This is invaluable for testing local HTML, CSS, and JavaScript files without needing to configure a full web server like Apache or Nginx.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs