| |
(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
test
这两个文件在apache中工作正常,但在lighttpd 1.4.11上却不能正常工作,在移植CNFUG.org的服务到lighttpd的时候,就遇到这个问题,同时发现 mod_ssi 也不支持嵌套 #include。后来根据lighttpd 1.4.11的代码,写了一个小patch for mod_ssi,解决了以上的问题,附代码:
(注:嵌套 #include 的代码由 marc@r4l.com'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
#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, "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 |
|