从任何目录运行Web服务器

我正在写一个小网站,但我不想弄清楚如何安装和配置完整的LAMP堆栈来测试我的〜/ home目录下的网站。 这将是完全破坏性和不必要的。

我想要的只是有一个目录,例如〜/ home / Documents / Website,并从该文件夹中运行一个小型Web服务器作为网站的“home”文件夹。

我知道Jekyll( http://jekyllrb.com )可以做类似的事情,但它似乎只适用于构建和配置的基于Ruby / Jekyll的站点。

是不是有一些小的Web服务器程序,我可以轻松安装,然后只是运行非常简单?

例如,如果我只需要从命令行运行诸如simple-server serve ~/home/Documents/Website ,然后导航到例如localhost:4000或其他任何测试网站,那将是完美的。

(如果在Ubuntu中已经可以使用,我只是不知道如何,请告诉我。)

编辑:看起来读者更容易忽视我的问题和疑虑,低估我的问题,然后简单地告诉我做与我想要实现的相反的事情。 这不具有建设性。

如果你安装了php,你可以使用php内置服务器来运行html / css和/或php文件:

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

作为输出你会得到:

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

我所知道的最简单的方法是:

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

命令的输出将告诉您正在侦听哪个端口(我认为默认为8000)。 运行python3 -m http.server --help以查看可用的选项。

欲获得更多信息:

  1. 关于http.server Python文档
  2. 简单的HTTP服务器 (这也提到了python2语法)

你想要什么称为静态Web服务器 。 有很多方法可以实现这一目标。

它列出了静态Web服务器

一种简单的方法:将以下脚本保存为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"); 

index.html放在同一目录中并运行

  node static_server.js