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?
Use
or
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.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):
In the same script that starts this I also start guard, which auto-refreshes the browser on file change.
if you are more ruby minded, the serve gem is great
or for i different port:
install with
gem install serve
This is also possible in Ruby without installing a gem.
ruby -run -e httpd . -p5000
Just use
http-server
, it's a zero-configuration command line server.The easiest way to install it is through npm:
Usage:
[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.
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: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.
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.
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
One of many other things is the bundled
grunt
server.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
You can use ran a cross-platform language-agnostic, static http server