BASH
Start a Quick Local Static File Server
Launch an instant local web server using Python's http.server or PHP's built-in server from any directory, perfect for quickly testing static web pages.
#!/bin/bash
# This script starts a simple static file server in the current directory.
# It prioritizes Python 3's http.server, then Python 2's SimpleHTTPServer,
# and finally PHP's built-in web server.
PORT=${1:-8000} # Default port is 8000, can be overridden by argument
echo "Attempting to start a static file server on port $PORT..."
# Option 1: Python 3's http.server
if command -v python3 &> /dev/null; then
echo "Using Python 3's http.server..."
echo "Server running at http://localhost:$PORT/"
python3 -m http.server "$PORT"
exit 0
fi
# Option 2: Python 2's SimpleHTTPServer
if command -v python &> /dev/null; then
echo "Using Python 2's SimpleHTTPServer..."
echo "Server running at http://localhost:$PORT/"
python -m SimpleHTTPServer "$PORT"
exit 0
fi
# Option 3: PHP's built-in web server
if command -v php &> /dev/null; then
echo "Using PHP's built-in web server..."
echo "Server running at http://localhost:$PORT/"
php -S "0.0.0.0:$PORT"
exit 0
fi
echo "Error: No suitable static file server (Python 3, Python 2, or PHP) found."
echo "Please ensure Python or PHP is installed and available in your PATH."
exit 1
How it works: This versatile bash script provides a rapid way to spin up a local static file server from any directory. It intelligently detects available tools, prioritizing Python 3's `http.server`, then Python 2's `SimpleHTTPServer`, and finally PHP's built-in web server. It defaults to port 8000 but allows a custom port to be specified. This is incredibly useful for quickly testing HTML, CSS, and JavaScript files without needing a full-fledged web server setup.