Lighttpd 搭建 Web 服務器


背景:
      公司項目用到了lighttpd,由於自己沒有接觸過,所以做下記錄。

簡介:
      Lighttpd 是一個德國人領導的開源Web服務器軟件,其根本的目的是提供一個專門針對高性能網站,安全、快速、兼容性好並且靈活的web server環境。具有非常低的內存開銷,cpu占用率低,效能好,以及豐富的模塊等特點。支持FastCGI, CGI, Auth, 輸出壓縮(output compress), URL重寫, Alias等重要功能,而Apache之所以流行,很大程度也是因為功能豐富,在lighttpd上很多功能都有相應的實現了。

安裝:
環境:Ubuntu 11
下載安裝:

apt-get install lighttpd

配置文件在:/etc/lighttpd/lighttpd.conf

View Code
#指定要裝載的模塊
server.modules = (
        "mod_access",
        "mod_alias",
        "mod_compress",
        "mod_redirect",
#       "mod_rewrite",
)
#fastcgi的配置
#fastcgi.server = ( ".php" => ((
#                    "host" => "127.0.0.1",
#                    "port" => "9000",
#                 )))

fastcgi.server = ( ".php" => ((
                    "socket" => "/tmp/php.socket",
                 )))
#設置網頁服務器的根目錄。配置文件必需指定該參數
server.document-root        = "/var/www"
#設置上傳目錄。缺省為 /var/tmp 
server.upload-dirs          = ( "/var/cache/lighttpd/uploads" )
# 設置錯誤日志文件的路徑
server.errorlog             = "/var/log/lighttpd/error.log"
# 設置Lighttpd進程的路徑
server.pid-file             = "/var/run/lighttpd.pid"
#指定可以運行服務器的用戶名和組名,要求lighttp以root權限啟動。
server.username             = "www-data"
server.groupname            = "www-data"
#默認讀取的文件,index.lighttpd.html 是安裝默認的文件,按照順序來查找執行。
index-file.names            = ( "index.php", "index.html",
                                "index.htm", "default.htm",
                               " index.lighttpd.html","a.html")

url.access-deny             = ( "~", ".inc" )
# 禁止訪問通過擴展的文件的某些類型的源
static-file.exclude-extensions = ( ".php", ".pl", ".fcgi" )
server.port = 80
server.bind = "192.168.200.201"

## Use ipv6 if available
#include_shell "/usr/share/lighttpd/use-ipv6.pl"
#為目錄列表設置編碼
dir-listing.encoding        = "utf-8"
#啟用或則禁用目錄列表
server.dir-listing          = "disable"

compress.cache-dir          = "/var/cache/lighttpd/compress/"
compress.filetype           = ( "application/x-javascript", "text/css", "text/html", "text/plain" )

#虛擬主機1
$HTTP["host"] == "bbs.db2.com" {
server.name = "bbs.db2.com"
server.document-root = "/var/www/db2/"
accesslog.filename = "/var/www/db2/access.log"
accesslog.format = "%{X-Forwarded-For}i %v %u %t \"%r\" %s %b \"%{User-Agent}i\" \"%{Referer}i\""
}
#虛擬主機2
$HTTP["host"] == "bbs.abc.com" {
server.name = "bbs.abc.com"
server.document-root = "/var/www/abc/"
accesslog.filename = "/var/www/abc/access.log"
}
#虛擬主機3
$HTTP["host"] == "bbs.xyz.com" {
server.name = "bbs.xyz.com"
server.document-root = "/var/www/xyz/"
accesslog.filename = "/var/www/xyz/access.log"
}


include_shell "/usr/share/lighttpd/create-mime.assign.pl"
include_shell "/usr/share/lighttpd/include-conf-enabled.pl"

 

安裝完畢之后,做瀏覽器里面輸入:服務器ip地址,會出現“Index of /”的列表(server.dir-listing = "enable"),點 index.lighttpd.html 文件,直接加載了該html文件的內容。這個文件剛好是在參數 server.document-root 指定的目錄中。要是server.dir-listing = "disable"的話,直接輸入ip則會出現404報錯,需要輸入ip+index.lighttpd.html才行。
自己可以創建一個html文件,如a.html放到server.document-root指定的目錄中,再修改 index-file.names 參數,讓a.html包含進去,再重啟lighttpd,會讓a.html展示出來。
到此,lighttpd基本功能已經成功安裝。

使用:

1:多虛擬主機的配置:
1個IP地址配置多個域名,即1個IP多各域名。
首先,需要修改host名稱,如:

vi /etc/hosts
192.168.220.201 bbs.abc.com --添加
192.168.220.201 bbs.xyz.com --添加

改完之后,需要重啟networking 才能生效。
接着,在/etc/lighttpd/lighttpd.conf 配置文件中添加虛擬主機:

$HTTP["host"] == "bbs.db2.com" {
server.name = "bbs.db2.com"
server.document-root = "/var/www/db2/"
accesslog.filename = "/var/www/db2/access.log"
accesslog.format = "%{X-Forwarded-For}i %v %u %t \"%r\" %s %b \"%{User-Agent}i\" \"%{Referer}i\""
}

$HTTP["host"] == "bbs.abc.com" {
server.name = "bbs.abc.com"
server.document-root = "/var/www/abc/"
accesslog.filename = "/var/www/abc/access.log"
}

$HTTP["host"] == "bbs.xyz.com" {
server.name = "bbs.xyz.com"
server.document-root = "/var/www/xyz/"
accesslog.filename = "/var/www/xyz/access.log"
}

再在/var/www/的目錄下創建 db2,abc,xyz目錄,把之前寫的a.html放入到這些目錄中。
最后,在瀏覽器里面輸入bbs.db2.com,bbs.xyz.com,bbs.abc.com 看看什么效果。

當然上面的虛擬主機可以單獨寫一個文件,再在include 到 lighttpd.conf 中。

2,lighttpd 支持 PHP,即 lighttpd + php-fpm

 安裝:

apt-get install php5-fpm php5

 確保 /etc/php5/fpm/php.ini 中的cgi.fix_pathinfo=1

cd /etc/lighttpd/conf-available/
cp 15-fastcgi-php.conf 15-fastcgi-php-spawnfcgi.conf
vi 15-fastcgi-php.conf
fastcgi.server += ( ".php" =>
        ((
                "bin-path" => "/usr/bin/php-cgi",
                "socket" => "/tmp/php.socket",
                "max-procs" => 1,
                "bin-environment" => (
                        "PHP_FCGI_CHILDREN" => "4",
                        "PHP_FCGI_MAX_REQUESTS" => "10000"
                ),
                "bin-copy-environment" => (
                        "PATH", "SHELL", "USER"
                ),
                "broken-scriptfilename" => "enable"
        ))
)

開啟lighttpd 的 fastcgi 配置,執行:

lighttpd-enable-mod fastcgi
lighttpd-enable-mod fastcgi-php

出現2個軟鏈接:
ls -l /etc/lighttpd/conf-enabled
lrwxrwxrwx 1 root root 33 Nov 14 04:52 10-fastcgi.conf -> ../conf-available/10-fastcgi.conf
lrwxrwxrwx 1 root root 37 Nov 14 04:52 15-fastcgi-php.conf -> ../conf-available/15-fastcgi-php.conf

修改/etc/lighttpd/lighttpd.conf 配置:添加

fastcgi.server = ( ".php" => ((
                    "host" => "127.0.0.1",
                    "port" => "9000",
                 )))

重啟lighttpd

/etc/init.d/lighttpd force-reload

最后寫一個php頁面:index.php ,該文件放到/var/www/xyz/目錄下面。
在index-file.names配置文件中的index-file.names選項里添加該php文件。注:index-file.names 選項里當有多個文件時,會按照文件的順序來去查找頁面。

<?php
phpinfo();
?>

現在可以在瀏覽器里面輸入查看頁面了。
上面的是通過TCP進行通信鏈接的,端口是9000
netstat -ap | grep php
tcp        0      0 localhost:9000          *:*                     LISTEN      1288/php-fpm.conf
或則:
netstat -ntl | grep 9000

那么通過套接字如何進行呢?

1,修改/etc/php5/fpm/pool.d/www.conf文件,把
listen = 127.0.0.1:9000
改成
listen = /tmp/php.socket
2,修改/etc/lighttpd/lighttpd.conf文件,把
fastcgi.server = ( ".php" => ((
                    "host" => "127.0.0.1",
                    "port" => "9000",

                 )))
改成
fastcgi.server = ( ".php" => ((
                    "socket" => "/tmp/php.socket",
                 )))

最后重啟php和lighttpd:
/etc/init.d/php5-fpm restart
/etc/init.d/lighttpd restart

趕緊在瀏覽器里面輸入地址看看是否OK。

 

這里只是簡單的介紹了lighttpd的一些功能,還有很多功能沒有寫出來,如:跳轉等。后期當自己遇到的時候再學再完善筆記。

參考:
http://redmine.lighttpd.net/projects/lighttpd/wiki/Docs_ConfigurationOptions
http://www.howtoforge.com/installing-lighttpd-with-php5-php-fpm-and-mysql-support-on-ubuntu-12.10
http://i.linuxtoy.org/docs/guide/ch23s03.html



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM