Skip to content

Instantly share code, notes, and snippets.

@JeffreyWay
Created December 27, 2011 21:29
Show Gist options
  • Save JeffreyWay/1525217 to your computer and use it in GitHub Desktop.
Save JeffreyWay/1525217 to your computer and use it in GitHub Desktop.
Instant Server for Current Directory
alias server='open http://localhost:8000 && python -m SimpleHTTPServer'
@fleas
Copy link

fleas commented Dec 30, 2011

no python needed, here's the good ole emergency web server.

while true; do nc -l 127.0.0.1 8889 < foo.html; done

or

 nc -l 127.0.0.1 8889 < foo.html

@mathiasbynens
Copy link

Inspired by @padolsey’s snippet, I’ve replaced the server alias in my dotfiles repository with the following function:

# Start an HTTP server from a directory, optionally specifying the port
function server() {
    local port="${1:-8000}"
    open "http://localhost:${port}/" && python -m SimpleHTTPServer "$port"
}

@balupton
Copy link

balupton commented Jan 1, 2012

For a node.js version, made this a while ago: https://github.com/balupton/simple-server

@spudbean
Copy link

spudbean commented Jan 2, 2012

My version: defaults the port, but allows you to override it. And opens the browser with your real hostname, instead of localhost, for pasting to other people.

function server() { # via https://gist.github.com/1525217
    local host=`hostname`
    local port="${1:-8888}"
    (sleep 1 && open "http://${host}:${port}/")&
    python -m SimpleHTTPServer "$port"
}

@zeffii
Copy link

zeffii commented Jul 19, 2012

Thanks guys, from ubuntu I now do this:

function server() {
    local port="${1:-8000}"
    gnome-open "http://localhost:${port}/"
    python -m SimpleHTTPServer "$port"
}

@dweidner
Copy link

dweidner commented Sep 3, 2014

Overcomplicated?

function server() {
    local port="${1:-8080}"
    local phpfiles=$(find . -type f -iname "*.php" | awk 'END {print NR;}')
    local phpversion=$(php -v | grep "PHP 5" | sed 's/.*PHP \([^-]*\).*/\1/' | cut -d\  -f 1)
    local phpvmajor=$(echo ${phpversion} | cut -d. -f 1)
    local phpvminor=$(echo ${phpversion} | cut -d. -f 2)

    open "http://localhost:${port}/"

    if [ ${phpvmajor} -ge 5 -a ${phpvminor} -ge 4 -a ${phpfiles} -gt 0 ]; then
        php -S localhost:${port}
    else
        python -m SimpleHTTPServer "${port}"
    fi
}

@ahmedelgabri
Copy link

This supports simple native servers for Ruby, Sinatra, PHP & fallback to Python SimpleHTTPServer as default

server() {
    local port="${2:-8000}"
    open "http://localhost:${port}/"

    if [[ "$1" == "ruby" ]]; then
        ruby -run -ehttpd . -p$port
    elif [[ "$1" == "sinatra" ]]; then
        ruby -rsinatra -e'set :public_folder, "."; set :port, $port'
    elif [[ "$1" == "php" ]]; then
        php -S localhost:$port
    else
        # 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"
    fi
}

@wenerme
Copy link

wenerme commented May 21, 2015

My universal version, works with npm, python, php and can run under windows, linux and mac

# Instant Server for Current Directory
# https://gist.github.com/JeffreyWay/1525217
function server()
{
    local port=${1:-8000}
    iscmd python && {
        (sleep 1 && o "http://localhost:${port}/")&
        python -m SimpleHTTPServer ${port}
    }

    iscmd npm && (npm -g ls --depth=0 | grep server@) >/dev/null && {
        # Use npm server
        (sleep 1 && o "http://localhost:${port}/")&
        server ${port}
    }

    iscmd php && {
        (sleep 1 && o "http://localhost:${port}/")&
        php -S localhost:${port}
    }
}

Also in my dotfiles. the iscmd is defined in my dotfiles too.

@AustinMaddox
Copy link

What about running a PHP built-in development server in a Docker container?

server() {
  local port="${1:-8000}"
  local path="${2:-.}"
  (sleep 1 && open "http://localhost:${port}")&
  docker run --rm --interactive --tty --name="php-built-in-development-web-server-$port" --publish $port:$port --network="local-network" --volume "$PWD":/usr/src/myapp --workdir /usr/src/myapp --user $(id -u):$(id -g) php:7.2-alpine php -S 0.0.0.0:$port -t $path
}

Portable 😄 You don't really have to worry about Windows, Mac, Linux if it is running in Docker. 🐳

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