160

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

10 Answers 10

239

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.

7
  • 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, 2013 at 11:17
  • 4
    @dibs Yes they can both run in parallel. But no, not on the same port.
    – Dan
    Nov 15, 2013 at 11:28
  • The python SimpleHTTPServer is awesome - but do remember that this can only serve "static" files, not execute PHP/Python/"backend" requests. But it is really handy in a cinch for your use case!
    – Suman
    Nov 15, 2013 at 15:45
  • 19
    The python 3.x equivalent of this is python3 -m http.server Nov 19, 2013 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, 2015 at 1:14
32

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, 2013 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, 2013 at 23:54
  • 1
    Perfect ! Just what I was looking for ! Nov 3, 2015 at 15:36
19

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, 2015 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, 2015 at 13:27
17

This is also possible in Ruby without installing a gem.

ruby -run -e httpd . -p5000

5
  • Didn't work for me. Which version of ruby did you try ? Oct 19, 2015 at 13:09
  • 1
    Works as advertised with Ruby 2.5.1.
    – Raphael
    Jul 13, 2018 at 13:17
  • 1
    Only answer on this page that supports byte-range requests! Dec 2, 2019 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, 2021 at 2:43
  • @neopostmodern the gem way supports byte-range requests as well.
    – jarno
    Oct 11, 2021 at 22:30
9

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.

2
  • 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.
    – james-see
    Oct 2, 2018 at 15:41
  • @jamescampbell I suppose you meant npm. Ruby is not pre-installed in Ubuntu.
    – jarno
    Oct 11, 2021 at 20:55
8

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.

3
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
  • 1
    I get some permission denied errors with nginx. Does it require running as superuser?
    – jarno
    Oct 11, 2021 at 21:23
  • No, it should not require running as superuser. If you get an error nginx: [alert] could not open error log file: open() "/var/log/nginx/error.log" failed (13: Permission denied) then it's OK - Nginx will indeed try to open "/var/log/nginx/error.log" and fail, but it should proceed and launch normally. Error log won't be lost (it will be written to stderr due to error_log /dev/stderr;). Or do you get a different error? (if yes, what is it?)
    – asterite
    Oct 14, 2021 at 9:38
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

0

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

0

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.