Run a web server from any directory

Solution 1:

The simplest way I know of is:

cd /path/to/web-data
python3 -m http.server

The command's output will tell you which port it is listening on (default is 8000, I think). Run python3 -m http.server --help to see what options are available.

For more information:

  1. Python documentation on http.server
  2. Simple HTTP server (this also mentions the python2 syntax)

Solution 2:

If you have php installed you can use php built-in server to run html/css and/or php files :

cd /path/to/your/app
php -S localhost:8000

As output you'll get :

Listening on localhost:8000
Document root is /path/to/your/app

Solution 3:

What you want is called static web server. There are many ways to achieve that.

It's listed static web servers

One simple way: save below script as static_server.js

   var http = require("http"),
     url = require("url"),
     path = require("path"),
     fs = require("fs")
     port = process.argv[2] || 8888;

 http.createServer(function(request, response) {

   var uri = url.parse(request.url).pathname
     , filename = path.join(process.cwd(), uri);

   path.exists(filename, function(exists) {
     if(!exists) {
       response.writeHead(404, {"Content-Type": "text/plain"});
       response.write("404 Not Found\n");
       response.end();
       return;
     }

     if (fs.statSync(filename).isDirectory()) filename += '/index.html';

     fs.readFile(filename, "binary", function(err, file) {
       if(err) {        
         response.writeHead(500, {"Content-Type": "text/plain"});
         response.write(err + "\n");
         response.end();
         return;
       }

       response.writeHead(200);
       response.write(file, "binary");
       response.end();
     });
   });
 }).listen(parseInt(port, 10));

 console.log("Static file server running at\n  => http://localhost:" + port + "/\nCTRL + C to shutdown");

put your index.html in the same directory and run

 node static_server.js