22

I am writing a small website, but I do NOT want to figure out how to install and configure complete LAMP stack to test the website from my ~/home directory. That will be completely disruptive and unnecessary.

All I want is to have a directory, e.g. ~/home/Documents/Website and run a small web server from that folder as the website's "home" folder.

I know Jekyll can do something similar, but it only seems to work with Ruby/Jekyll-based sites that it builds and configures.

Isn't there some small web server program that I can easily install and then just run very simply?

For instance, if I just needed to run something like e.g. simple-server serve ~/home/Documents/Website from a command line and then navigate to e.g. localhost:4000 or whatever to test the site, that would be perfect.

If this is already possible in Ubuntu and I just don't know how, please let me know.

Zanna
  • 72,312
etsnyman
  • 1,185

5 Answers5

34

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)
muru
  • 207,228
15

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
storm
  • 5,013
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
kenn
  • 5,232
1

Use busybox. For example with Debian.

apt install busybox
mkdir ~/www
busybox httpd -p 80 -h ~/www
0

Install local-web-server, it installs the ws command which you can run to serve any directory as a static site.

This clip demonstrates static hosting plus a couple of log output formats - dev and stats.

Static static log output

Lloyd
  • 101