4

在 Linux 將目錄快速轉為靜態網站

 2 years ago
source link: https://blog.darkthread.net/blog/simplest-http-server-linux/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

在 Linux 將目錄快速轉為靜態網站

calendar.svg 2022-02-27 03:13 PM comment.svg 3 eye.svg 2,410

弄了幾個靜態 HTML、JavaScript 想放在客廳的 Linux 家用伺服器上給其他家庭成員存取。因屬臨時性質,不想花時間搞設定、架網站或寫程式,不然 ASP.NET Core Minimal API 用一行 app.UseFileServer() 就能搞定。

因此,我花了點時間,研究「如何用最少的力氣在 Linux 跑一個網站?」

直接說結論,切到網站根目錄,python3 -m http.server,網站就架好了。預設網站會聽 8000 Port,但 Port、綁定 IP、網站路徑都可以指定,細節可參考官方文件。(註:http.server 僅包含基本安全檢查,不適用於正式生產環境)

但這有個問題,程式在前景執行,ssh 或 telnet 登出就會結束。查到 Ctrl-Z + bg 轉為背景執行,再搭配 disown -h 的做法,讓登出時 Shell 不轉發 SIGNUP 訊號終止程式,但實測這招對 http.server 無效,退出 ssh 時程式仍會被中止。最後試出的可行做法是用 Shell 指令 nohup nohup python3 -m http.server & 將 http.server 丟到背景執行並在離線或登出後繼續工作,輸出結果則會被導向 nohup.out 檔案。

如此便能將 Linux 目錄快速轉成靜態網站囉。

and has 3 comments

Comments

Post a comment

Comment
Name Captcha 99 - 35 =

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK