How to run a temporary http server without installing Nginx / Apache in Ubuntu 在Ubuntu中运行简易的Web服务器

yuxi(61)
Published in
#cn
Words
389
Reading
2 min
Listen
Play
9y

Sometimes we only want to run HTTP server temporarily, here is how:

有些时候我们可能并不想安装Nginx或者Apache之类的软件,而是需要一个临时的Web服务器。比如处于性能和安 全的考虑,羽西对于自己的开发服务器配置实行的就是极简主义,不需要的绝对不安装。但有些时候可能临时需要一个Web服务器来测试,下面给大家介绍一个简单的方法。

If you have enabled ufw, add ufw rule to allow the port being used by HTTP server. For example. to open port 3000 for HTTP server:

如果你配置并激活了ufw,那么首先需要添加新的规则:

sudo ufw allow from any to any port 3000

Now check all ufw rules:

好了,检查一下新加的规则

sudo ufw status numbered

Go to your website folder. Create html files, e.g. index.html if you don’t have them.

进入到对应的文件夹,确认相应的html文件以及配套的js,css等文件都在

Run the following command to start your web server by typing (Python 3 is the prerequisite):

运行一下的命令来运行python3自带的简易web服务器(前提是必须事先安装Python3):

python3 -m http.server 3000

Now you can access that page in your browser.

现在就可以在浏览器中访问你的页面了。

After running it, delete the rule (the number can be found from above command, e.g. sudo ufw status numbered):

在测试完毕后,终止python3 web服务器并删除对应的ufw规则:

sudo ufw delete THE_RULE_NUMBER

https://steemit.com 首发。非常感谢阅读,欢迎FOLLOW, Resteem和Upvote yuxi@yuxi 激励我创作更多更好的内容。

How to run a temporary http server without installing Nginx / Apach... | Ecency