【轉帖】Lighttpd 的安裝配置

原文地址: http://hi.baidu.com/jakee/blog/item/9679f62438f70e338744f9d6.html

Lighttpd 的安裝配置2006-07-16 01:06lighttpd(http://lighttpd.net/)和apache一樣是開源的,與apache相比,雖然功能不及apache完善,穩定性也不如apache,但是,不管是服務靜態頁面,還是服務動態內容(CGI,PHP),它都比apache快,用於ad banner之類的WEB服務器是最恰當不過了。

本文從應用的角度,說明如何安裝、配置lighttpd。

(1) 安裝

可從http://lighttpd.net/download/下載最新的源碼(.tar.gz)或者rpm包。如果下載的是.tar.gz文件,則和GNU的其他軟件一樣,先./configure一下,然後 make && make install就搞定了。但是如果你想定製一些功能,就得好好看看解壓後README, INSTALL以及./configure --help的輸出結果了。這裏僅僅說一下如何從源碼安裝,其他安裝方式可參考 http://trac.lighttpd.net/trac/wiki/TutorialInstallation。

$ gzip -cd lighttpd-1.4.9.tar.gz | tar xf -
...
$ cd lighttpd-1.4.9
$ ./configure --help
`configure' configures lighttpd 1.4.9 to adapt to many kinds of systems.

Usage: ./configure [OPTION]... [VAR=VALUE]...

To assign environment variables (e.g., CC, CFLAGS...), specify them as
VAR=VALUE.  See below for descriptions of some of the useful variables.

Defaults for the options are specified in brackets.

Configuration:
...

Installation directories:
  --prefix=PREFIX         install architecture-independent files in PREFIX
                          [/usr/local]
  --exec-prefix=EPREFIX   install architecture-dependent files in EPREFIX
                          [PREFIX]

By default, `make install' will install all the files in
`/usr/local/bin', `/usr/local/lib' etc.  You can specify
an installation prefix other than `/usr/local' using `--prefix',
for instance `--prefix=$HOME'.

For better control, use the options below.

Fine tuning of the installation directories:
...

Program names:
...

System types:
...

Optional Features:
  --disable-FEATURE       do not include FEATURE (same as --enable-FEATURE=no)
  --enable-FEATURE[=ARG]  include FEATURE [ARG=yes]
  --enable-maintainer-mode  enable make rules and dependencies not useful
                          (and sometimes confusing) to the casual installer
  --disable-dependency-tracking  speeds up one-time build
  --enable-dependency-tracking   do not reject slow dependency extractors
  --enable-static[=PKGS]
                          build static libraries [default=no]
  --enable-shared[=PKGS]
                          build shared libraries [default=yes]
  --enable-fast-install[=PKGS]
                          optimize for fast installation [default=yes]
  --disable-libtool-lock  avoid locking (might break parallel builds)
  --enable-lfs            Turn on Large File System (default)
  --disable-ipv6          disable IPv6 support

Optional Packages:
  --with-PACKAGE[=ARG]    use PACKAGE [ARG=yes]
  --without-PACKAGE       do not use PACKAGE (same as --with-PACKAGE=no)
  --with-gnu-ld           assume the C compiler uses GNU ld [default=no]
  --with-pic              try to use only PIC/non-PIC objects [default=use
                          both]
  --with-tags[=TAGS]
                          include additional configurations [automatic]
  --with-mysql[=PATH]
                          Include MySQL support. PATH is the path to
                          'mysql_config'
  --with-ldap             enable LDAP support
  --with-attr             enable extended attribute support
  --with-valgrind         enable internal support for valgrind
  --with-openssl[=DIR]
                          Include openssl support (default no)
  --with-openssl-includes=DIR
                          OpenSSL includes
  --with-openssl-libs=DIR OpenSSL libraries
  --with-kerberos5        use Kerberos5 support with OpenSSL
  --with-pcre             Enable pcre support (default yes)
  --with-bzip2            Enable bzip2 support for mod_compress
  --with-fam              fam/gamin for reducing number of stat() calls
  --with-webdav-props     properties in mod_webdav
  --with-gdbm             gdbm storage for mod_trigger_b4_dl
  --with-memcache         memcached storage for mod_trigger_b4_dl
  --with-lua              lua engine for mod_cml

Some influential environment variables:
...
如上所述,可通過--prefix指定安裝路徑,默認安裝在/usr/local下。可以指定啓用哪些特徵(插件),禁用哪些特徵(插件)。假定我們要把lighttpd安裝到/usr/local/lighttpd-1.4.9下面。

$ ./configure --prefix=/usr/local/lighttpd-1.4.9
$ make
$ make install
$ cp  doc/lighttpd.conf /usr/local/lighttpd-1.4.9/  # 拷貝配置文件
$ cd /usr/local/lighttpd-1.4.9
$ vi lighttpd.conf # 修改配置文件
配置文件很直觀明瞭,一般只要把server.document-root、server.errorlog、accesslog.filename改成你的實際目錄和文件名字就可以了。

$ sbin/lighttpd -f lighttpd.conf # 啓動lighttpd服務
$ ps aux | grep lighttpd
www 15403  0.0  0.9  2860 1148 ? S 00:15 0:00 sbin/lighttpd -f
這就完成了從安裝到啓動的整個過程,很簡單吧。從最後一行的輸出可以看出,lighttpd是單進程服務的,這和apache不一樣(也許是它的穩定性不如apache的一個原因)。

(2) 整合php和fastcgi

以php-4.3.11爲例,編譯PHP的時候,不能指定 --with-apxs選項,編譯命令行大致如下:

$ ./configure ... --enable-force-cgi-redirect --enable-fastcgi
$ make
$ sapi/cgi/php -v
PHP 4.3.11 (cgi-fcgi) (built: Jan 30 2006 00:12:34)
Copyright (c) 1997-2004 The PHP Group
Zend Engine v1.3.0, Copyright (c) 1998-2004 Zend Technologies
make完了後,會在sapi/cli目錄生成命令行下的php程序,sapi/cgi下生成fastcgi下的php程序。如果執行sapi/cgi下的php顯示版本號,你會發現有 cgi-fcgi的說明,這就表明你成功了。

$ mkdir /usr/local/lighttpd-1.4.9/fcgi
$ cp sapi/cgi/php /usr/local/lighttpd-1.4.9/fcgi/
$ vi /usr/local/lighttpd-1.4.9/lighttpd.conf
我們建立一個子目錄fcgi用來保存所有的fast-cgi程序,然後把php拷貝到該目錄下。編輯lighttpd.conf,如下所示:

...
server.modules = (
 ...
 "mod_fastcgi",
 ...)
...
fastcgi.server = (".php" =>
 ( "127.0.0.1" =>
  (
   "socket" => "/tmp/fcgi_php.sock",
   "bin-path" => "/usr/local/lighttpd-1.4.9/fcgi/php"
  )
 )
 )
重新啓動lighttpd就可以了。Lighttpd和fastcgi通信有兩種方式:通過Unix socket通信,如以上PHP的啓動;通過TCP/IP socket通信。Lighttpd支持基於fastcgi的負載均衡,不過我沒嘗試過。

關於fastcgi的協議規範,請參考http://www.fastcgi.com/,以下是我自己寫的一個fastcgi的配置樣例:

fastcgi.server = ( "/fastcgi/adsim" =>
 ( "127.0.0.1" =>
  (
   "host" => "127.0.0.1",
   "port" => 4000,
   "bin-path" => "/usr/local/lighttpd-1.4.9/fcgi/adsim",
   "check-local" => "disable"
  )
)
check-local必須設置爲disable,否則因爲找不到/fastcgi/adsim會導致請求失敗。

(3) 製作lighttpd啓動腳本

每次啓動lighttpd時我們要指定配置文件的位置,停止lighttpd時要先找到進程號,然後用kill發送停止信號,有點太麻煩了。好在lighttpd自帶了一個腳本程序能輔助完成這些操作,只要稍微改改就能用了,那就是源碼目錄doc/rc.lighttpd和doc/rc.lighttpd.redhat,後者專用於RedHat Linux。主要的改動之處在於:

...
if [ -z "$LIGHTTPD_CONF_PATH" ]; then
        LIGHTTPD_CONF_PATH="/usr/local/lighttpd-1.4.9/lighttpd.conf"
fi
...
lighttpd="/usr/local/lighttpd-1.4.9/usr/sbin/lighttpd"
...
用這個腳本管理lighttpd是不是方便多了。

(4) Lighttpd和OpenSSL

Lighttpd默認不編譯ssl模塊,所以必須在編譯的時候明確指定 --with-openssl,然後再生成自簽署的服務器證書或者從CA那裏獲取。生成自簽署證書的方法如下:

$ openssl req -new -x509 -keyout server.pem \
 -out server.pem -days 365 -nodes
Lighttpd要求證書和私匙保存在同一個文件裏,如果是分開的,則需要合併:

$ cat host.key host.crt > host.pem
配置lighttpd.conf,大致樣子如下:

ssl.engine = "enable"
ssl.pemfile = "server.pem"
你可以針對某個虛擬主機做這樣的設置,但是由於SSL工作在TCP層,所以不能設置基於名稱的虛擬主機,只能設置基於端口的。 以下是一個配置樣例:

$SERVER["socket"] == "192.168.146.128:443" {
       ssl.engine = "enable"
       ssl.pemfile = "/usr/local/lighttpd/certs/server.pem"
       server.document-root = "/home/www/wfs/www"
}
(5) 配置目錄列表

修改 lighttpd.conf,大致如下所示:

server.module = {
 ...
 "mod_dirlisting",
 ...}

dir-listing.activate = "enable"
(6) 配置CGI

修改lighttpd.conf,首先需要啓動mod_cgi,然後在static-file.exclude-extensions中指定cgi文件的擴展名,最後通過cgi.assign配置指令進行關聯。

對於帶擴展名且需要特定解析程序執行的CGI,可以指定解析程序的路徑,比如:

cgi.assign = ( ".pl"  => "/usr/bin/perl",
 ".cgi" => "/usr/bin/perl" )
對於帶擴展名切不需要特定解析程序就能執行的CGI,可指定解析程序爲空,比如:

cgi.assign = (".cgi" => "")
對於不帶擴展名的CGI程序,只能通過固定路徑存取了,比如:

cgi.assgin = ( "/cgi-bin/mycgi" => "/usr/local/cgi/mycgi )
(7) 配置虛擬主機

配置基於端口的虛擬主機上文有所描述,基於名稱的虛擬主機也很簡單。修改lighttpd.conf,啓動模塊mod_simple_vhost,然後指定你的虛擬主機信息,比如:

$HTTP["host"] == "news.example.org" {
  server.document-root = "/var/www/servers/news2.example.org/pages/"
}
Lighttpd注重於速度,而Apache注重於穩定性和功能,怎麼選擇還得看具體的應用。


=========================================

lighttpd 1.4.11 mod_ssi 補丁
lighttpd 1.4.11 的 mod_ssi 默認情況下不支持以下ssi語法:

有兩件文件test.shtml和header.shtml,test.shtml中設置了文章標題變量doc_title,然後再include header.shtml,headers.shtml再根據doc_title的值,顯示網頁標題,具體內容如下:

test.shtml

<!--#set var="doc_title" value="This is a test" -->
<!--#include virtual="header.shtml" -->

header.shtml

<html>
<head>
<title><!--#echo var="doc_title" --></title>
</head>
<body>
test
</body>
這兩個文件在apache中工作正常,但在lighttpd 1.4.11上卻不能正常工作,在移植CNFUG.org的服務到lighttpd的時候,就遇到這個問題,同時發現 mod_ssi 也不支持嵌套 #include。後來根據lighttpd 1.4.11的代碼,寫了一個小patch for mod_ssi,解決了以上的問題,附代碼:

(注:嵌套 #include 的代碼由 [email protected]'s 提供,在此感謝marc!代碼也可以從這兒獲得:http://matthew.cnfug.org/patch/mod_ssi.c.patch)

--- mod_ssi.c Sun Apr 2 01:20:30 2006
+++ mod_ssi.c.modify Sun Apr 2 01:21:05 2006
@@ -36,6 +36,9 @@
#include <sys/filio.h>
#endif

+/* determine if process finished */
+int h_finished = 0;
+
/* init the plugin data */
INIT_FUNC(mod_ssi_init) {
plugin_data *p;
@@ -57,7 +60,7 @@ FREE_FUNC(mod_ssi_free) {
UNUSED(srv);

if (!p) return HANDLER_GO_ON;
-
+
if (p->config_storage) {
size_t i;
for (i = 0; i < srv->config_context->used; i++) {
@@ -286,6 +289,37 @@ static int build_ssi_cgi_vars(server *sr
return 0;
}

+URIHANDLER_FUNC(mod_ssi_physical_path) {
+ plugin_data *p = p_d;
+ size_t k;
+
+ if (con->physical.path->used == 0) return HANDLER_GO_ON;
+
+ con->loops_per_request++;
+
+ mod_ssi_patch_connection(srv, con, p);
+
+ for (k = 0; k < p->conf.ssi_extension->used; k++) {
+ data_string *ds = (data_string *)p->conf.ssi_extension->data[k];
+
+ if (ds->value->used == 0) continue;
+
+ if (buffer_is_equal_right_len(con->physical.path, ds->value, ds->value->used - 1)) {
+ /* handle ssi-request */
+
+ if (mod_ssi_handle_request(srv, con, p)) {
+ /* on error */
+ con->http_status = 500;
+ }
+
+ return HANDLER_FINISHED;
+ }
+ }
+
+ /* not found */
+ return HANDLER_GO_ON;
+}
+
static int process_ssi_stmt(server *srv, connection *con, plugin_data *p,
const char **l, size_t n) {
size_t i, ssicmd = 0;
@@ -467,7 +501,11 @@ static int process_ssi_stmt(server *srv,
if (NULL != (ds = (data_string *)array_get_element(p->ssi_cgi_env, var_val))) {
buffer_copy_string_buffer(b, ds->value);
} else {
- buffer_copy_string(b, "(none)");
+ if (NULL != (ds = (data_string *)array_get_element(p->ssi_vars, var_val))) {
+ buffer_copy_string_buffer(b, ds->value);
+ } else {
+ buffer_copy_string(b, "(none)");
+ }
}

break;
@@ -481,6 +519,7 @@ static int process_ssi_stmt(server *srv,
const char * file_path = NULL, *virt_path = NULL;
struct stat st;
char *sl;
+ buffer *tmp;

for (i = 2; i < n; i += 2) {
if (0 == strcmp(l[i], "file")) {
@@ -574,7 +613,26 @@ static int process_ssi_stmt(server *srv,
}
break;
case SSI_INCLUDE:
- chunkqueue_append_file(con->write_queue, p->stat_fn, 0, st.st_size);
+ /* do recursive SSI expansion */
+ /* prevents infinite loop */
+ if (con->loops_per_request > 25 || buffer_is_equal(con->physical.path, p->stat_fn)) {
+ buffer_copy_string(srv->tmp_buf, "<!-- your include directives create an infinite loop, aborting -->");
+ chunkqueue_append_buffer(con->write_queue, srv->tmp_buf);
+ break;
+ }
+
+ tmp = buffer_init();
+ buffer_copy_string_buffer(tmp, con->physical.path); /* save path of current document */
+ buffer_copy_string_buffer(con->physical.path, p->stat_fn); /* next sub-document to parse */
+ if (mod_ssi_physical_path(srv, con, p) != HANDLER_FINISHED) {
+ /* the document was not processed, so write it as is */
+ chunkqueue_append_file(con->write_queue, con->physical.path, 0, st.st_size);
+ } else {
+ h_finished = 1;
+ }
+ buffer_copy_string_buffer(con->physical.path, tmp); /* restore saved path */
+ buffer_free(tmp);
+
break;
}
} else {
@@ -897,7 +955,11 @@ static int mod_ssi_handle_request(server

/* get a stream to the file */

- array_reset(p->ssi_vars);
+ if (h_finished == 1)
+ {
+ array_reset(p->ssi_vars);
+ h_finished = 0;
+ }
array_reset(p->ssi_cgi_env);
buffer_copy_string(p->timefmt, "%a, %d %b %Y %H:%M:%S %Z");
p->sizefmt = 0;
@@ -1038,35 +1100,6 @@ static int mod_ssi_patch_connection(serv
}
#undef PATCH

-URIHANDLER_FUNC(mod_ssi_physical_path) {
- plugin_data *p = p_d;
- size_t k;
-
- if (con->physical.path->used == 0) return HANDLER_GO_ON;
-
- mod_ssi_patch_connection(srv, con, p);
-
- for (k = 0; k < p->conf.ssi_extension->used; k++) {
- data_string *ds = (data_string *)p->conf.ssi_extension->data[k];
-
- if (ds->value->used == 0) continue;
-
- if (buffer_is_equal_right_len(con->physical.path, ds->value, ds->value->used - 1)) {
- /* handle ssi-request */
-
- if (mod_ssi_handle_request(srv, con, p)) {
- /* on error */
- con->http_status = 500;
- }
-
- return HANDLER_FINISHED;
- }
- }
-
- /* not found */
- return HANDLER_GO_ON;
-}
-
/* this function is called at dlopen() time and inits the callbacks */

int mod_ssi_plugin_init(plugin *p) {
@@ -1082,3 +1115,4 @@ int mod_ssi_plugin_init(plugin *p) {

return 0;
}
+

用法:
$ cd lighttpd-1.4.11/src
$ patch < /home/matthew/patch/mod_ssi.c.patch
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章