9

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.

  • What kinds of file do you have php python or plain html? – Dan Sep 5 '14 at 19:33
  • @dan08 At the moment, just plain html and css. I might want to add NodeJS in the future, but then I will have a different set-up. – etsnyman Sep 5 '14 at 19:46
  • So you can simply open those in your web browser, no server required. – Dan Sep 5 '14 at 19:48
  • Can you clarify your question? It is really far easier to serve documents from /var/www/html then it is from your home directory. Either way you install Apache along with mysql, php, or whatever else you might need. To use /va/www/html simply copy the files. It is more work to configure Apache to serve files from your home directory as you have to either enable home directories or edit apache configuration files. In both locations you still have to have the directories/files available to www-data. I do not understand what you find so difficult. – Panther Sep 5 '14 at 19:56
  • @dan08 There are crippling limitations to serving from a file:// address rather than a http:// address. Some links and small Javascript snippets simply do not work. – etsnyman Sep 5 '14 at 19:58
10

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
11

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)
  • Genius! Thank you, @muru! For some reason, my port 8000 is in use (I can't figure out why) but I just ran python3 -m http.server 4000 and then navigated to localhost:4000 in Firefox, and BAM! - my website is ready to be tested! Thank you! – etsnyman Sep 5 '14 at 20:18
2

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
  • +1 for the list of static servers. I must say, the indentation of that code looks very odd. – muru Sep 5 '14 at 20:40
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

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.