Skip to content

Instantly share code, notes, and snippets.

@nathanlong
Last active May 31, 2018 04:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nathanlong/bc1d4923982ad45e9379 to your computer and use it in GitHub Desktop.
Save nathanlong/bc1d4923982ad45e9379 to your computer and use it in GitHub Desktop.
Local Servers from the Command Line
# Drop these functions into your ~/.bashrc or ~/.bash_profile, wherever you keep your bashy goodness.
# Usage: cd to directory you want to serve up, fire one of these commands, navigate to http://0.0.0.0:{port number here}
# Start an HTTP server from a directory, optionally specifying the port
function serverpy() {
local port="${1:-8000}"
open "http://localhost:${port}/"
# Set the default Content-Type to `text/plain` instead of `application/octet-stream`
# And serve everything as UTF-8 (although not technically correct, this doesn’t break anything for binary files)
python -c $'import SimpleHTTPServer;\nmap = SimpleHTTPServer.SimpleHTTPRequestHandler.extensions_map;\nmap[""] = "text/plain";\nfor key, value in map.items():\n\tmap[key] = value + ";charset=UTF-8";\nSimpleHTTPServer.test();' "$port"
}
# Start a PHP server from a directory, optionally specifying the port
# (Requires PHP 5.4.0+.)
function phpserver() {
local port="${1:-4000}"
local ip=$(ipconfig getifaddr en1)
sleep 1 && open "http://${ip}:${port}/" &
php -S "${ip}:${port}"
}
# Start webrick server from current directory
function server {
local port=$1
: ${port:=3000}
open "http://localhost:${port}/"
ruby -rwebrick -e"s = WEBrick::HTTPServer.new(:Port => $port, :DocumentRoot => Dir.pwd, :MimeTypes => WEBrick::HTTPUtils::load_mime_types('/etc/apache2/mime.types')); trap(%q(INT)) { s.shutdown }; s.start"
}
@sblack4
Copy link

sblack4 commented May 31, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment