146

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?

1
221

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.

6
  • 1
    This is exactly what I've been looking for! Danke! 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
  • 16
    The python 3.x equivalent of this is python3 -m http.server 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
29

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.

3
  • 3
    Note that the built-in server feature was added in 5.4.0 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
  • 1
    Perfect ! Just what I was looking for ! Nov 3 '15 at 15:36
18

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

serve

or for i different port:

serve 9000

install with gem install serve

2
  • This works fine, but it requires ruby >= 1.9.3 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 Oct 19 '15 at 13:27
16

This is also possible in Ruby without installing a gem.

ruby -run -e httpd . -p5000

4
  • Didn't work for me. Which version of ruby did you try ? Oct 19 '15 at 13:09
  • Works as advertised with Ruby 2.5.1.
    – Raphael
    Jul 13 '18 at 13:17
  • Only answer on this page that supports byte-range requests! Dec 2 '19 at 10:53
  • Thanks. Never used Ruby in my life, but this worked as-is. Mine seems to be 2.5.1.
    – Starman
    Mar 4 at 2:43
8

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.

1
  • 1
    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. Oct 2 '18 at 15:41
3

You can use Nginx for that: https://gist.github.com/asterite3/89236d1753a669e173531aca4b87afdc

This is not single-threaded (server won't hang for other clients if accessed from, say, google-chrome) and is very configurable and effective.

3

On Ubuntu (and probably almost all other Linux distos) you already have BusyBox installed. So you can run httpd:

busybox httpd -f -p 8080

then open http://localhost:8080

In sources httpd.c you may find more details. It is very limited but have almost all basic functionality like Basic Auth, gzip compression and CGI scripts.

BTW the BusyBox is also widely used in embedded devices: WiFi routers, TV boxes etc. But OpenWrt which is open source firmware for WiFi routers uses their own http server: uhttpd. And you can compile and install it on any other Linux. Yes, it's not built-in into Ubuntu and not so easy to install but this may be useful if you still want small footprint web server but bb-httpd doesn't fit your needs.

2

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.

0

You want JS Lightning. It is just what the doctor ordered. It's a Node JS app.

Install it. Type "js-lightning" in any directory with servile files and it will serve on port 7000. Give it another port and you're good there instead. Lots of good features to make it useful.

For good measure, it will serve anything in the directory that it can "require()". Which is to say, it serves .js files in a manner inspired by PHP.

http://jsLightning.com

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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