Lighttpd


一个好用的轻巧的webservice

	

编译安装


-- 1.下载 --

下载相关软件



$ sudo apt-get install libpcre3*


cd /usr/local/src/

wget http://www.lighttpd.net/download/lighttpd-1.4.15.tar.gz

tar zxvf lighttpd-1.4.15.tar.gz

cd lighttpd-1.4.15


-- 2.安装 --

编译安装


./configure --prefix=/usr/local/lighttpd-1.4.15 \

--with-bzip2 \

--with-memcache

make

make install


-- 3.创建目录与配置文件 --

创建目录与配置文件

ln -s /usr/local/lighttpd-1.4.15/ /usr/local/lighttpd

mkdir -p /www/pages

mkdir /www/logs

mkdir /usr/local/lighttpd/htdocs

mkdir /usr/local/lighttpd/logs

mkdir /usr/local/lighttpd/etc

cp ./doc/lighttpd.conf /usr/local/lighttpd/etc/

cd /usr/local/lighttpd/



-- 4.修改配置文件 --

配置lighttpd.conf

vi etc/lighttpd.conf

找到 server.modules

删除 mod_fastcgi 前的注释

跟据你的需求修改下面定义

server.document-root = "/usr/local/lighttpd/htdocs/"

server.errorlog = "/usr/local/lighttpd/logs/lighttpd.error.log"

accesslog.filename = "/usr/local/lighttpd/logs/access.log"

注释 $HTTP["url"]

#$HTTP["url"] =~ "\.pdf$" {

# server.range-requests = "disable"

#}


-- 5.运行 --

运行lighttpd

/usr/local/lighttpd/sbin/lighttpd -f /usr/local/lighttpd/etc/lighttpd.conf


测试

curl http://ip/ 因为/www/pages/下没有HTML页面所以返回:

404 - Not Found



-- 6.制作启动脚本 --

每次启动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是不是方便多了。

-- 7.z.en写的简单的管理脚本 --

#!/bin/bash
# author: z.En Wong<w@zenw.org>


PREFIX=/usr/local/lighttpd/
PROG=$PREFIX/sbin/lighttpd
OPTIONS=" -f /usr/local/lighttpd/lighttpd.conf"
restart(){
        stop
        start


}

start(){

        $PROG$OPTIONS
}
stop(){
        PID=`pidof lighttpd`
        kill $PID

}

case "$1" in
        stop)
                stop
                ;;
        start)
                start
                ;;
        restart)
                restart
                ;;
        *)
                echo "{start|stop|restart}"
esac



		

安装PHP(FastCGI)


-- 1. 下载PHP --

      cd /usr/local/src/
      wget http://cn2.php.net/get/php-5.2.3.tar.bz2/from/cn.php.net/mirror
      tar jxvf php-5.2.3.tar.bz2
      cd php-5.2.3

-- 2. configure --

      ./configure --prefix=/usr/local/php-5.2.3 \
      --with-config-file-path=/usr/local/php-5.2.3/etc \
      --enable-fastcgi \
      --enable-force-cgi-redirect \
      --with-curl \
      --with-gd \
      --with-ldap \
      --with-snmp \
      --enable-zip \
      --enable-exif \
      --with-pdo-mysql \
      --with-pdo-pgsql \

      make
      make test
      make install
      				

      其它有用的模块

      --enable-pcntl

-- 3. 符号连接 --

      ln -s /usr/local/php-5.2.3 /usr/local/php
      ln -s /usr/local/php/bin/php /usr/local/bin/php

-- 4. php.ini --

      cp php.ini-dist /usr/local/php/etc/php.ini

-- 5. env --

      PHP_FCGI_CHILDREN=384

-- 6. 使用 php -v FastCGI 安装情况 --

      php -v 或 php-cgi -v

      显示(cgi-fcgi)表示正确

      # cd /usr/local/php/
      # bin/php -v
      PHP 5.2.2 (cgi-fcgi) (built: May 25 2007 15:50:28)
      Copyright (c) 1997-2007 The PHP Group
      Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies
      				

      (cgi-fcgi)不能正常工作

      PHP 5.2.2 (cli) (built: May 25 2007 15:50:28)
      Copyright (c) 1997-2007 The PHP Group
      Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies
      				

      使用 php -m 或php-cgi -m 查看PHP Modules

      # bin/php -m
      [PHP Modules]
      cgi-fcgi
      ctype
      date
      dom
      filter
      gd
      hash
      iconv
      json
      ldap
      libxml
      mssql
      pcre
      PDO
      pdo_mysql
      pdo_sqlite
      posix
      Reflection
      session
      SimpleXML
      snmp
      SPL
      SQLite
      standard
      tokenizer
      xml
      xmlreader
      xmlwriter
      zip

      [Zend Modules]
      				

- apt-get install -

$ sudo apt-get install php5 php5-cli php5-cgi



找到 fastcgi.server 去掉注释

bin-path 改为PHP程序安装目录

fastcgi.server             = ( ".php" =>
                               ( "localhost" =>
                                 (
                                  "socket" => "/tmp/php-fastcgi.socket",#这个文件如果不存在,需要touch一下
                                   "bin-path" => "/usr/local/php/bin/php"
                                 )
                               )
                            )


- 下面例子更复杂一些 -

-- 1. /usr/local/lighttpd/etc/lighttpd.conf --

      include /usr/local/lighttpd/etc/php-fastcgi.conf

-- 2. /usr/local/lighttpd/etc/php-fastcgi.conf --

      fastcgi.server = ( ".php" =>
      	( "localhost" =>
              ( "socket" => "/tmp/php-fastcgi.socket",
                 "bin-path" => "/usr/local/php/bin/php",
                 "min-procs" => 1,
                 "max-procs" => 5,
                 "max-load-per-proc" => 4,
                 "idle-timeout" => 20
              )
      	)
      )
      				

-- 3. PHP FastCGI环境测试 --

      echo "<?php phpinfo(); ?>" > /www/pages/index.php

      curl http://127.0.0.1/index.php



		

simple-vhost


- 功能介绍 -

simple-vhost就是自动将你在浏览器地址栏里输入的URL匹配到一个document-root下与URL相同名字的目录中

这样可以实现不需要配置,就可以简单的创建虚拟主机


- 打开simple-vhost功能 -

将server.modules中的mod_simple_vhost去除注释


- 配置simple-vhost功能 -

The complete document root is constructed either by

如果完整的配置了simple-vhost目录

server-root + hostname + document-root

or if this path does not exist by

如果没有完整的配置simple-vhost目录

server-root + default-host + document-root

A small example should make this idea clear:

一个小例子

/var/www/
/var/www/logs/
/var/www/servers/
/var/www/servers/example.org/
/var/www/servers/example.org/lib/
/var/www/servers/example.org/pages/
/var/www/servers/mail.example.org/
/var/www/servers/mail.example.org/lib/
/var/www/servers/mail.example.org/pages/

将配置文件中以下项目解除注释

simple-vhost.server-root = "/var/www/servers/" 
simple-vhost.default-host = "example.org" 
simple-vhost.document-root = "pages" 




- 相关链接 -

http://redmine.lighttpd.net/wiki/1/Docs:ModSimpleVhost 官方标准设置说明 



		

rewrite


- 开启方法 -
-- 打开lighttpd.conf --

# vim /usr/local/lighttpd/lighttpd.conf

-- 解除配置文件里 server.modules 中的 mod_rewrite 的注释 --

也就是去掉#

...省略...

server.modules              = (
                               "mod_rewrite",

...省略...

-- 在配置文件里写上自己的重写规则 --

如:

url.rewrite=("^/(.*).html$"=>"/$1.php")

-- 重启lighttpd --

如果你使用了z.En的脚本(见lighttpd中),可以如下:

# /etc/init.d/lighttpd restart

如果没有使用,那么就需要

# kill `pidof lighttpd`
# /usr/local/lighttpd/sbin/lighttpd -f /usr/local/lighttpd/lighttpd.conf


- 更多链接 -

http://redmine.lighttpd.net/wiki/lighttpd/MigratingFromApache#mod_rewrite 官方说明