Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Unix知識 >> Unix資訊

用 lighttpd 打造輕量站點

XAMPP 與許多軟體組合都內建 Apache HTTP Server,而 Apache 確實是出色的 Web 伺服器——統計上全球多數站點都在用它,加上大量模組可以擴充功能、與各種程式語言緊密整合。

但 Apache 並不是唯一選擇,某些情境下也不是最合適的。一個複雜的 Apache 實例會吃掉可觀的記憶體,直接壓低吞吐量;即使是很小的實例,也可能得不償失。這時 lighttpd(讀作「lighty」)就值得一試:體積小、效率高,兼具安全性、速度、相容性與彈性,而且設定檔比 Apache 簡單得多。

從原始碼建置

lighttpd 依賴一些外部函式庫,至少需要 PCRE(Perl 相容正則表達式)與 zlib 的開發版本(含標頭檔)。備齊之後編譯就不難:

$ # Lighttpd requires libpcre3-dev and zlib1g-dev
$ wget https://www.lighttpd.net/download/lighttpd-1.4.22.tar.gz
$ tar xzf lighttpd-1.4.22.tar.gz
$ cd lighttpd-1.4.22
$ ./configure && make && sudo make install

最精簡的設定

起碼只要指定文件根目錄、服務埠、幾個 MIME 類型,以及守護行程的預設使用者與群組:

server.document-root = "/var/www/lighttpd/host1"
server.groupname = "www"
server.port = 3000
server.username = "www"
mimetype.assign = (
  ".html" => "text/html",
  ".txt"  => "text/plain",
  ".jpg"  => "image/jpeg",
  ".png"  => "image/png" )
static-file.exclude-extensions = ( ".fcgi", ".php", ".rb", "~", ".inc" )
index-file.names = ( "index.html" )

假設存成 /opt/etc/lighttpd.conf,啟動方式為:

lighttpd -D -f /opt/etc/lighttpd.conf

虛擬主機只要三行

$HTTP["host"] == "www2.example.org" {
  server.document-root = "/var/www/lighttpd/host2" }

當主機名稱符合條件時,就改用另一個文件根目錄。

lighttpd 特別擅長處理大量並行請求,也很容易與 Rails、PHP 這類框架搭配。對需要同時服務很多連線、又不想讓記憶體被吃光的場景來說,它是個非常划算的選擇。

Copyright © Linux教程網 All Rights Reserved