I'm looking for a relatively painless way to launch a web server with document root in any folder I specify (or better yet, where I'm launching).

I often try out new things like JS frameworks or so in a new folder somewhere here:

/home/alexander/code/angularjs/

It would then be convenient to just go into this directory and type something like:

start-webserver-here 

so that a web server starts listening on port 80 and serving this directory, and I can try out whatever I'm working on.

I've installed XAMPP but it seems that it's basic apache with a document root I'd have to change, with root privileges even, along with granting permissions for every folder etc.

Is there an easier way?

up vote 168 down vote accepted

Use

python2 -m SimpleHTTPServer 80

or

python3 -m http.server 80

to start a simple HTTP server.

Replace 80 with another number if you want it to listen on a different port. For ports < 1024 it needs to run with root privileges.

  • 1
    This is exactly what I've been looking for! Danke! – Alexander Rechsteiner Nov 15 '13 at 11:01
  • 1
    Can you run this in parallel to Apache? On a different port of course (Though I would welcome you to blow my mind by telling me they can use the same ports). – dibs Nov 15 '13 at 11:17
  • 3
    @dibs Yes they can both run in parallel. But no, not on the same port. – Dan Nov 15 '13 at 11:28
  • 11
    The python 3.x equivalent of this is python3 -m http.server – Stefano Palazzo Nov 19 '13 at 10:34
  • 1
    thats why i love stackoverflow so much .Came looking here for the exact problem I wanted to find a soultion for ... run angularjs from a webserver. Thanks much Florian Deisch – SK176H Aug 9 '15 at 1:14

I also like to use PHP for this purpose, as it enables me to run stuff like WordPress on the fly and develop themes more easily (you still need MySQL, though):

php -S 0.0.0.0:8000

In the same script that starts this I also start guard, which auto-refreshes the browser on file change.

  • 1
    Note that the built-in server feature was added in 5.4.0 – sierrasdetandil Nov 19 '13 at 12:03
  • Yes, you need one of the newer version. The version in the 13.10 repos if you do sudo apt-get install php5 is 5.5.3, so you should be OK. – metakermit Nov 19 '13 at 23:54
  • Perfect ! Just what I was looking for ! – Prakash Raman Nov 3 '15 at 15:36

if you are more ruby minded, the serve gem is great

serve

or for i different port:

serve 9000

install with gem install serve

  • This works fine, but it requires ruby >= 1.9.3 – Valerio Schiavoni Oct 19 '15 at 13:09
  • Yep, but also 1.9.3 is eol. Security patches are no longer backported. Would recommend rvm for managing legacy rubies. They should not be your system's default – will-ob Oct 19 '15 at 13:27

This is also possible in Ruby without installing a gem.

ruby -run -e httpd . -p5000

  • Didn't work for me. Which version of ruby did you try ? – Valerio Schiavoni Oct 19 '15 at 13:09
  • Works as advertised with Ruby 2.5.1. – Raphael Jul 13 at 13:17

Just use http-server, it's a zero-configuration command line server.

The easiest way to install it is through npm:

sudo npm install http-server -g

Usage:

http-server [path] [options]

[path] defaults to ./public if the folder exists, and ./ otherwise.

To see your server in action visit http://localhost:8080. Use -p option to set a different port.

For more options visit: https://www.npmjs.com/package/http-server.

  • The only reason not to use this is that it requires rpm that is not natively installed. The other solutions that mention ruby and python are pre-installed on almost every OS, so there is not a pre-requisite associated with installing the command to run the server. – jamescampbell Oct 2 at 15:41

Since there's angularjs in your folder's name, it seems like it's an angular js app. In such case, be sure to check yeoman out.

To quote it's site

Yeoman 1.0 is more than just a tool. It's a workflow; a collection of tools and best practices working in harmony to make developing for the web even better.

One of many other things is the bundled grunt server.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.