Run a web server from any directory

14,382

Solution 1

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 2

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 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

Solution 4

Use busybox. For example with Debian.

apt install busybox
mkdir ~/www
busybox httpd -p 80 -h ~/www
Share:
14,382
etsnyman
Author by

etsnyman

Updated on September 18, 2022

Comments

  • etsnyman
    etsnyman over 1 year

    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.

    • Dan
      Dan over 9 years
      What kinds of file do you have php python or plain html?
    • etsnyman
      etsnyman over 9 years
      @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.
    • Dan
      Dan over 9 years
      So you can simply open those in your web browser, no server required.
    • Panther
      Panther over 9 years
      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.
    • etsnyman
      etsnyman over 9 years
      @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
      etsnyman over 9 years
      @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?
    • Panther
      Panther over 9 years
      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.
    • etsnyman
      etsnyman over 9 years
      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
      etsnyman over 9 years
      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
      etsnyman over 9 years
      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
    etsnyman over 9 years
    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!
  • muru
    muru over 9 years
    +1 for the list of static servers. I must say, the indentation of that code looks very odd.
  • Алексей Неудачин
    Алексей Неудачин over 2 years
  • unixcreeper
    unixcreeper about 2 years
    how would you turn it into a shell script ?