Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sblack4/0f083a798f6dfd40640a62162785c2cb to your computer and use it in GitHub Desktop.
Save sblack4/0f083a798f6dfd40640a62162785c2cb 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"
}
# try go
# with this super-simple server
# default port is 8000
function goserver() {
go get -u github.com/fogleman/serve
port=${1:-8000}
dir=${2:-"."}
serve -port "$port" -dir "$dir" & echo open 'localhost:"$port" to see the contents of "$dir'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment