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
  • 70,465
etsnyman
  • 1,125
  • What kinds of file do you have php python or plain html? – Dan Sep 05 '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 05 '14 at 19:46
  • So you can simply open those in your web browser, no server required. – Dan Sep 05 '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 05 '14 at 19:56
  • 1
    @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 05 '14 at 19:58
  • @bodhi.zazen Jekyll, for instance, serves a complete web page without Apache, mysql, or php. Surely there is a similar package that does not require Jekyll to have built the site? – etsnyman Sep 05 '14 at 19:58
  • If you want to use http:// rather then file:// you need a web server. Apache is most common, there are others, nginx, use any one you wish. I am not familiar with Jekyll, but if you have experience with it, use it. – Panther Sep 05 '14 at 20:10
  • 1
    All information about Apache and Jekyll, and why neither will work for me, is in my original question. Please read it thoroughly before you add a comment or answer. – etsnyman Sep 05 '14 at 20:16
  • Thanks to @muru who read my original question, a simple web server is already bundled with Ubuntu in the form of Python's http.server. – etsnyman Sep 05 '14 at 20:20
  • I don't know who downvoted my question, but if it was one of the commentators above, then that person did not read my question properly. @muru had no problems reading and answering it with a simple, easy solution. – etsnyman Sep 05 '14 at 20:21

5 Answers5

32

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
  • 197,895
  • 55
  • 485
  • 740
  • 1
    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 05 '14 at 20:18
  • how would you turn it into a shell script ? – unixcreeper Mar 09 '22 at 23:49
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
  • 4,973
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,162
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
  • 1