Ways to start a Webserver in your current Directory

Lately I searched for a way to open a web server, mainly for static content, from the current directory under Linux. In the java world, I am used to do this with maven or gradle, starting an embedded jetty, tomcat or similar. I did not want to do this for a project where I don't have any java at all, and I found more than one way, a lot of them listed here, and, using Docker, there are way more possibilities than those I found. So I started to list some of them.

I assume we start those servers on port 8000 as this does not require root.

You have Node/npm installed?

Install and start npm's serve.

npm install serve -g
serve -p 8000

Directory listing is supported. It supports Accept headers and is able to deliver the directory listing in HTML, plain text or JSON.

You are using Grunt?

There is grunt-serve.

You have PHP installed and want a server supporting PHP?

php -Slocalhost:8000

This server does not support directory listing, but will interpret php scripts.

You have python installed?

This seems to be one of the most common ad-hoc server commands:

python -m SimpleHTTPServer 8000

Or, with Python 3:

python -m http.server

You have ruby?

ruby -run -ehttpd . -p8000

Works great, supports directory listing.

You have ruby and want something more sofisticated?

There is serve. I did not get this one work out of the box on my Ubuntu (Ruby gems version loading problems), but it looks promising, as it supports template languages as Compass, HAML, Markdown, ... out of the box. Unfortunately the binary does have the same name as npm's serve.

sudo gem install serve
serve

This will start a server on port 4000.

You can even use Chrome, the browser

There is an Addon acting as a webserver: https://chrome.google.com/webstore/detail/web-server-for-chrome/ofhbbkphhbklhfoeikjpcbhemlocgigb?hl=en

You have Docker installed?

With docker you have more possibilities, although installing will need a bit longer as you will have to pull the images from the repository. Starting and stopping the container is as fast as starting the other servers. But you have the power and flexibility of a "real" webserver. You can enable modules and pass configuration files defining rewrite rules, proxys, and so on.

Starting an nginx webserver with docker

docker run --name my-nginx -p 8000:80 -v $PWD:/usr/share/nginx/html:ro --rm nginx

See https://registry.hub.docker.com/_/nginx/ for more details.

Starting an Apache webserver with docker

docker run -it --rm --name my-apache-app -v "$PWD":/usr/local/apache2/htdocs/ -p 8000:80 httpd:2.4

Directory listing is enabled by default. You can add your own apache configuration files (or even .htaccess files) to your container: https://registry.hub.docker.com/_/httpd/

Starting a webserver supporting php with docker

docker run -it --rm --name my-apache -v "$PWD":/var/www/html -p 8000:80 php:5.6-apache

You can start different servers than Apache. See https://registry.hub.docker.com/_/php/ for more details.