Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Big list of http static server one-liners

Each of these commands will run an ad hoc http static server in your current (or specified) directory, available at http://localhost:8000. Use this power wisely.

Discussion on reddit.

Python 2.x

$ python -m SimpleHTTPServer 8000

Python 3.x

$ python -m http.server 8000

Twisted (Python)

$ twistd -n web -p 8000 --path .

Or:

$ python -c 'from twisted.web.server import Site; from twisted.web.static import File; from twisted.internet import reactor; reactor.listenTCP(8000, Site(File("."))); reactor.run()'

Depends on Twisted.

Ruby

$ ruby -rwebrick -e'WEBrick::HTTPServer.new(:Port => 8000, :DocumentRoot => Dir.pwd).start'

Credit: Barking Iguana

Ruby 1.9.2+

$ ruby -run -ehttpd . -p8000

Credit: nobu

adsf (Ruby)

$ gem install adsf   # install dependency
$ adsf -p 8000

Credit: twome

No directory listings.

Sinatra (Ruby)

$ gem install sinatra   # install dependency
$ ruby -rsinatra -e'set :public_folder, "."; set :port, 8000'

No directory listings.

Perl

$ cpan HTTP::Server::Brick   # install dependency
$ perl -MHTTP::Server::Brick -e '$s=HTTP::Server::Brick->new(port=>8000); $s->mount("/"=>{path=>"."}); $s->start'

Credit: Anonymous Monk

Plack (Perl)

$ cpan Plack   # install dependency
$ plackup -MPlack::App::Directory -e 'Plack::App::Directory->new(root=>".");' -p 8000

Credit: miyagawa

Mojolicious (Perl)

$ cpan Mojolicious::Lite   # install dependency
$ perl -MMojolicious::Lite -MCwd -e 'app->static->paths->[0]=getcwd; app->start' daemon -l http://*:8000

No directory listings.

http-server (Node.js)

$ npm install -g http-server   # install dependency
$ http-server -p 8000

Note: This server does funky things with relative paths. For example, if you have a file /tests/index.html, it will load index.html if you go to /test, but will treat relative paths as if they were coming from /.

node-static (Node.js)

$ npm install -g node-static   # install dependency
$ static -p 8000

No directory listings.

PHP (>= 5.4)

$ php -S 127.0.0.1:8000

Credit: /u/prawnsalad and MattLicense

No directory listings.

Erlang

$ erl -s inets -eval 'inets:start(httpd,[{server_name,"NAME"},{document_root, "."},{server_root, "."},{port, 8000},{mime_types,[{"html","text/html"},{"htm","text/html"},{"js","text/javascript"},{"css","text/css"},{"gif","image/gif"},{"jpg","image/jpeg"},{"jpeg","image/jpeg"},{"png","image/png"}]}]).'

Credit: nivertech (with the addition of some basic mime types)

No directory listings.

busybox httpd

$ busybox httpd -f -p 8000

Credit: lvm

webfs

$ webfsd -F -p 8000

Depends on webfs.

IIS Express

C:\> "C:\Program Files (x86)\IIS Express\iisexpress.exe" /path:C:\MyWeb /port:8000

Depends on IIS Express.

Credit: /u/fjantomen

No directory listings. /path must be an absolute path.

Meta

If you have any suggestions, drop them in the comments below or on the reddit discussion. To get on this list, a solution must:

  1. serve static files using your current directory (or a specified directory) as the server root,
  2. be able to be run with a single, one line command (dependencies are fine if they're a one-time thing),
  3. serve basic file types (html, css, js, images) with proper mime types,
  4. require no configuration (from files or otherwise) beyond the command itself (no framework-specific servers, etc)
  5. must run, or have a mode where it can run, in the foreground (i.e. no daemons)
@MattLicense

For PHP >=5.4 you can run

$ php -S localhost:8000
@nivertech

Erlang:

erl -s inets -eval 'inets:start(httpd,[{server_name,"NAME"},{document_root, "."},{server_root, "."},{port, 8000}]).'
@aschmoe

If you ever use Drupal, drush 5+ has

$ drush rs 8080
@twome

Another Ruby:

$ gem install adsf
$ adsf
@willurd
Owner

Thanks for the suggestions everyone!

@lvm
lvm commented

busybox edition:

busybox httpd -p $PORT -h $HOME [ -c httpd.conf ]

Note that the config file is not required but optional.

@willurd
Owner

@lvm Thanks. That violates requirement #4 though (because it needs an httpd.conf file).

@lvm

Oh well, let me rephrase it then :)

@willurd
Owner

@lvm Added, thanks!

@Peeja

This is awesome! One note though, re: http-server:

Note: This server does funky things with relative paths. For example, if you have a file /tests/index.html, it will load index.html if you go to /test, but will treat relative paths as if they were coming from /.

That should be true for any of these. That's just how HTTP/HTML works. If you go to /test/ (note the trailing slash), then relative URLs will be relative to /test/. Relative URLs are always relative to the last slash in the URL.

The server can't control that; resolving relative URLs is the browser's job, and the browser doesn't know that /test is meant to be a directory. What some servers will do for you is redirect to the trailing-slash URL automatically, which other servers listed here may do—I'm not sure.

@nobu

Ruby 1.9.2+:

$ ruby -run -ehttpd . -p8000
@willurd
Owner

@nobu Added, thanks!

@willurd
Owner

@Peeja Automatic redirection is probably what's happening. Thanks!

@sudodoki

Just in case you can't install with -g flag in Node.js and node is all you got, try
npm install connect && echo "var c = require('connect');c.createServer(c.static(__dirname)).listen(8080);" | node
Note: may get you really weird results + might pollute your folder with node_modules

@sshtyck

Perl

$ perl -MIO::All -e 'io(":8080")->fork->accept->(sub { $_[0] < io(-x $1 +? "./$1 |" : $1) if /^GET \/(.*) / })'

Thanks to:

http://stackoverflow.com/questions/8058793/single-line-python-webserver

@ahmedelgabri

Created a shell function for this https://gist.github.com/ahmedelgabri/8122545 "I only needed Python, Ruby & PHP", maybe someone will find this useful.

@inconshreveable

I just created srvdir which solves this problem elegantly. It is open source on github and available as a simple binary download with no dependencies for all major operating systems:

srvdir homepage
srvdir code on github

Unlike all of the other solutions, srvdir serves your directory on a public HTTPS URL with an optional password. This means you can securely share the URL with anyone on the internet and are not restricted to just your local network.

It's also ridiculously simple to use. Serve files in the current directory:

./srvdir

Serve files from multiple named directories simultaneously:

./srvdir /usr/lib /var/www/
@moserrya

gem install knod to install dependency
knod -p 8000

Warning! Knod can also write to and delete from the directory you specify.

@GordonDiggs

Wrote a go version. It's complicated by the fact that you can't interpret go code from the command line without a file (like you can with ruby) so it makes a file that deletes itself upon exiting.

@sesm

Clojure (via Leiningen project management tool):

$ lein simpleton 8080

Note: you should install lein-simpleton plugin first by putting [lein-simpleton "1.3.0"] into the :plugins vector of your :user profile. Profiles are configured in ~/.lein/profiles.clj, if you don't have this file yet, you can start with

$ echo "{:user {:plugins [[lein-simpleton \"1.3.0\"]]}}" > ~/.lein/profiles.clj
@phai

Spark (Go)

$ go get github.com/rif/spark
$ spark -port 8000 .
@obscuresec

PowerShell

$Hso=New-Object Net.HttpListener;$Hso.Prefixes.Add("http://+:8000/");$Hso.Start();While ($Hso.IsListening){$HC=$Hso.GetContext();$HRes=$HC.Response;$HRes.Headers.Add("Content-Type","text/plain");$Buf=[Text.Encoding]::UTF8.GetBytes((GC (Join-Path $Pwd ($HC.Request).RawUrl)));$HRes.ContentLength64=$Buf.Length;$HRes.OutputStream.Write($Buf,0,$Buf.Length);$HRes.Close()};$Hso.Stop()

PowerShell from cmd.exe

PowerShell.exe -nop -enc JABIAHMAbwA9AE4AZQB3AC0ATwBiAGoAZQBjAHQAIABOAGUAdAAuAEgAdAB0AHAATABpAHMAdABlAG4AZQByADsAJABIAHMAbwAuAFAAcgBlAGYAaQB4AGUAcwAuAEEAZABkACgAIgBoAHQAdABwADoALwAvACsAOgA4ADAAMAAwAC8AIgApADsAJABIAHMAbwAuAFMAdABhAHIAdAAoACkAOwBXAGgAaQBsAGUAIAAoACQASABzAG8ALgBJAHMATABpAHMAdABlAG4AaQBuAGcAKQB7ACQASABDAD0AJABIAHMAbwAuAEcAZQB0AEMAbwBuAHQAZQB4AHQAKAApADsAJABIAFIAZQBzAD0AJABIAEMALgBSAGUAcwBwAG8AbgBzAGUAOwAkAEgAUgBlAHMALgBIAGUAYQBkAGUAcgBzAC4AQQBkAGQAKAAiAEMAbwBuAHQAZQBuAHQALQBUAHkAcABlACIALAAiAHQAZQB4AHQALwBwAGwAYQBpAG4AIgApADsAJABCAHUAZgA9AFsAVABlAHgAdAAuAEUAbgBjAG8AZABpAG4AZwBdADoAOgBVAFQARgA4AC4ARwBlAHQAQgB5AHQAZQBzACgAKABHAEMAIAAoAEoAbwBpAG4ALQBQAGEAdABoACAAJABQAHcAZAAgACgAJABIAEMALgBSAGUAcQB1AGUAcwB0ACkALgBSAGEAdwBVAHIAbAApACkAKQA7ACQASABSAGUAcwAuAEMAbwBuAHQAZQBuAHQATABlAG4AZwB0AGgANgA0AD0AJABCAHUAZgAuAEwAZQBuAGcAdABoADsAJABIAFIAZQBzAC4ATwB1AHQAcAB1AHQAUwB0AHIAZQBhAG0ALgBXAHIAaQB0AGUAKAAkAEIAdQBmACwAMAAsACQAQgB1AGYALgBMAGUAbgBnAHQAaAApADsAJABIAFIAZQBzAC4AQwBsAG8AcwBlACgAKQB9ADsAJABIAHMAbwAuAFMAdABvAHAAKAApAA==
@karuna

Is there any that show directory listing instead of loading index.html?

@somlor
$ gem install serve
$ serve
@mauvm
$ npm install -g beefy
$ beefy

With build in support for browserify, watchify, and live-reload.

@Paxa

Also for ruby

$ gem install httpit
$ httpit
@kidoman
brew install kidoman/tools/serve

or

go get github.com/kidoman/serve

Provided you have serve under your $PATH somewhere:

serve .

This will serve the current directory at http://localhost:5000/

serve -p 9999 ~/my-awesome-blog

Will serve the contents of the folder ~/my-awesome-blog at http://localhost:9999/

serve -x /my ~/precious

You guessed it, http://localhost:5000/my is now wired up to ~/precious

serve -o ~/sesame

Wires up http://localhost:5000 to ~/sesame and opens the URL in your favorite browser while it is at it.

https://github.com/kidoman/serve

@jarofghosts

shameless plug for node: glance
has index file support, directory listing, error pages, etc etc.

npm install -g glance
glance
@speier

Harp, static web server with built-in preprocessing.

$ npm install -g harp
$ harp server
@Val

Rack (ruby)

gem install rack --no-ri --no-rdoc
rackup -b 'use Rack::Static, :index => 'README.html'; run Rack::File.new('.')"
@mbleigh

Superstatic, static web server with support for pushState, cache control, and more.

npm install -g superstatic
superstatic -p 8000
@lethalman

I'd like to request a bash http server using /dev/tcp, anybody has a one-liner for that?

@amitu
$ pip install djangothis
$ djangothis

Assumes html files in current folder are django templates, so inheritance etc work. Also files in static folder are served as static files with correct mime types.

views.py named file can contain importd/django views in case you want to grow.

@avsm

OCaml is

opam install cohttp async
cohttp-server-async

or

opam install cohttp lwt ssl
cohttp-server-lwt
@kingofsevens

Livereload

For python with live reload feature..

pip install livereload
livereload                         #serves current dir with port 35729 which livereload extensions use
livereload ~/testing -p 8000       # to serve specific directory from specific port

And you can setup shell commands and such for livereloading..

@Syskaw

Create an alias in your .bashrc

function serve {
  port="${1:-3000}"
  http-server . -p $port
}

Then just run

serve 5000
@xudafeng

nodejs

$ npm install startserver -g
$ server 8080
@mufid

This is very handy to stream media in my notebook to my phone. Awesome! Thanks!

@kishaningithub

Very coool gist!! Made my day B-)

@oboxodo

Add serve

$ gem install serve
$ cd my-project
my-project$ serve
@ailin-nemui

you can also start HTTPS server which may be important for WSS services, example with Mojo:
perl -Mojo -MCwd -E'app->static->paths->[0]=getcwd; app->start' daemon -l 'https://*:8000' (note the https)

@eadz

For any of these, it would be worth making an alias in your shell.

For example for the ruby example and zsh.

alias server="ruby -run -ehttpd . -p 8080"

in your .zshrc means you can just run "server"

@reider-roque

Perl

perl -MHTTP::Daemon -e '$d = HTTP::Daemon->new(LocalPort => 8000) or  +die $!; while 
($c = $d->accept) { while ($r = $c->get_request) { +$c->send_file_response(".".$r->url->path) } }'

HTTP::Daemon comes preinstalled on many Linux distribution and thus works out of the box.

Credit goes to Anonymous Monk

@mayrund

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.