173

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?

Anwar
  • 77,855

10 Answers10

256

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.

33

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.

metakermit
  • 2,650
20

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

serve

or for i different port:

serve 9000

install with gem install serve

18

This is also possible in Ruby without installing a gem.

ruby -run -e httpd . -p5000

16

On Ubuntu (and probably almost all other Linux distos) you already have BusyBox installed. If not then on Ubuntu you may install sudo apt install busybox-static. So you can run httpd:

busybox httpd -f -vv -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, cache validation by ETag, serving pre-compressed gzip files, 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, use own http server: uhttpd. It's inspired by bb-httpd and has more features like built-in Lua interpreter. 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 a small footprint web server but bb-httpd doesn't fit your needs.

For Ubuntu I created a PPA which contains the uhttpd.

10

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.

Luis Ma
  • 101
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.

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.

mreq
  • 4,932
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

tqwhite
  • 101
0

You can use ran a cross-platform language-agnostic, static http server