Home | 簡體中文 | 繁體中文 | 雜文 | 打賞(Donations) | ITEYE 博客 | OSChina 博客 | Facebook | Linkedin | 知乎專欄 | Search | Email

48.3. Module

server.modules              = (
#                               "mod_rewrite",
#                               "mod_redirect",
#                               "mod_alias",
                                "mod_access",
#                               "mod_trigger_b4_dl",
#                               "mod_auth",
#                               "mod_status",
#                               "mod_setenv",
#                               "mod_fastcgi",
#                               "mod_proxy",
#                               "mod_simple_vhost",
#                               "mod_evhost",
#                               "mod_userdir",
#                               "mod_cgi",
#                               "mod_compress",
#                               "mod_ssi",
#                               "mod_usertrack",
#                               "mod_expire",
#                               "mod_secdownload",
#                               "mod_rrdtool",
                                "mod_accesslog" )

	

48.3.1. simple_vhost

$ sudo lighty-enable-mod simple-vhost
		

simple-vhost.default-host = "www.example.com"

create your virtual host directory

$ mkdir -p /var/www/www.example.com/html
		

create a test file

$ echo helloworld!!!> /var/www/www.example.com/html/index.html
		

48.3.2. ssl

啟用 ssl 模組

$ sudo lighttpd-enable-mod ssl
[sudo] password for neo:
Available modules: auth cgi fastcgi proxy rrdtool simple-vhost ssi ssl status userdir
Already enabled modules: cgi fastcgi simple-vhost
Enabling ssl: ok
Run /etc/init.d/lighttpd force-reload to enable changes
		

創建 ssl 證書

$ sudo openssl req -new -x509 -keyout server.pem -out server.pem -days 365 -nodes
$ sudo chmod 400 server.pem
		

48.3.3. redirect

		
url.redirect                = ( "^/music/(.+)" => "http://www.example.org/$1" )
		
		

301重定向

RewriteCond %{HTTP_HOST} ^example\.org$ [NC]
RewriteRule ^(.*)$ http://www.example.org/$1 [R=301,L]
		

lighttpd 實現上面 apache功能

$HTTP["host"] =~ "^example\.org" {
    url.redirect = (
        "^/(.*)$" => "http://www.example.org/$1"
    )
}

$HTTP["host"] =~ "^example\.com$" {
  url.redirect = ( "^/(.*)" => "http://www.example.com/$1" )
}
		

48.3.4. rewrite

example 1

url.rewrite-once = ( "^/wiki/(.*)$" => "/wiki/awki.cgi/$1" )
$HTTP["url"] =~ "^/wiki" {
  $HTTP["url"] !~ "^/wiki/awki.cgi/" {
    url.access-deny = ("")
  }
}
		

example 2

$HTTP["host"] =~ "^.*\.(example.org)$" {
  url.rewrite-once = ( "^/(.*)" => "/index.php/$1" )
}
		

example 3

		
$HTTP["host"] =~ "^.*\.(example.org)$" {
  url.rewrite = (
        "^/(images|stylesheet).*" => "/$0",
        "^/(.*)" => "/index.php/$1"
  )
}
		
		

48.3.4.1. Lighttpd Rewrite QSA

			
# Apache
RewriteRule ^/index\.html$ /index.php [QSA]
RewriteRule ^/team_(.*)\.html$ /team.php?id=$1 [QSA]

#lighttpd
"^/index\.html(.*)"                     => "/index.php$1",
"^/team_(\w+)\.html\?(.*)"              => "/team.php?id=$1&$2",
			
			

ref: http://redmine.lighttpd.net/wiki/lighttpd/MigratingFromApache

			
url.rewrite = (
        "^/index\.html(.*)"                     => "/index.php$1",
        "^/index\.html"                         => "/index.php",
        "^/team_(.*)\.html"                     => "/team.php?id=$1",
        "^/team_(\w+)\.html\?(.*)"              => "/team.php?id=$1&$2"
)
			
			

48.3.5. alias

		
$HTTP["host"] =~ "^.*\.(example.org)$" {
    alias.url = (
        "/images" => "/home/neo/workspace/Development/photography/application/photography/images",
        "/stylesheet" => "/home/neo/workspace/Development/photography/application/photography/stylesheet"
    )
}
		
		

48.3.6. auth

enable auth

$ sudo lighttpd-enable-mod auth
		

/etc/lighttpd/conf-enabled/05-auth.conf

		
$ sudo vim  conf-enabled/05-auth.conf

auth.backend = "plain"
auth.backend.plain.userfile = "/etc/lighttpd/.secret"

auth.require = ( "/tmp/" =>
        (
        "method" => "basic",
        "realm" => "Password protected area",
        "require" => "user=neo"
        )
)
		
		

create a passwd file

$ sudo vim .secret
neo:chen

$ sudo chmod 400 .secret
$ sudo chown www-data /etc/lighttpd/.secret
		

$ sudo /etc/init.d/lighttpd reload

48.3.7. compress

創建cache目錄

mkdir -p /var/cache/lighttpd/compress
		

配置lighttpd.conf檔案

找到server.modules列表,去掉"mod_compress"註釋,再打開compress module的註釋

#### compress module
compress.cache-dir         = "/var/lighttpd/cache/compress/"
compress.filetype          = ("text/plain", "text/html")
		

Compressing Dynamic Content

php.ini

zlib.output_compression = On
zlib.output_handler = On
		

最後使用telnet測試

telnet www.bg7nyt.cn 80
GET /index.html HTTP/1.0
Host: 10.10.100.183
Accept-Encoding: gzip,deflate
		

看到亂碼輸出,而非HTML,表示配置成功.

例 48.2. lighttpd compress

$HTTP["host"] =~ "www\.example\.com$" {

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

	$HTTP["url"] =~ "(\.png|\.css|\.js|\.jpg|\.gif)$" {
		expire.url = (""=>"access 30 seconds")
	}
}
			

48.3.8. expire

<access|modification> <number> <years|months|days|hours|minutes|seconds>
expire.url = ( "/images/" => "access 1 hours" )
		

Example to include all sub-directories:

$HTTP["url"] =~ "^/images/" {
	expire.url = ( "" => "access 1 hours" )
}
		

例 48.3. lighttpd expire

$HTTP["host"] =~ "www\.example\.com$" {
	$HTTP["url"] =~ "(\.png|\.css|\.js|\.jpg|\.gif)$" {
		expire.url = (""=>"access 30 seconds")
	}
}
			

48.3.9. status

$ sudo lighty-enable-mod status
$ sudo /etc/init.d/lighttpd force-reload
		

48.3.10. setenv

$HTTP["url"] =~ "^/(.*)" {
	setenv.add-response-header = ( "Cache-Control" => "no-store, no-cache, must-revalidate, post-check=0, pre-check=0, max-age=-1" )
}

$HTTP["url"] =~ ".swf" {
       setenv.add-response-header  = ("Pragma" =>"no-cache","Expires" => "-1")
}

$HTTP["url"] =~ ".swf" {
       setenv.add-response-header  = ("Cache-Control" =>"max-age=0")
}

$HTTP["url"] =~ ".html" {
       setenv.add-response-header  = ("Cache-Control" =>"s-maxage=3600")
}

$HTTP["url"] =~ ".css" {
	setenv.add-response-header = (
      "Content-Encoding" => "gzip"
    )
}
		

48.3.10.1. Automatic Decompression

  $HTTP["url"] =~ "(README|ChangeLog|\.txt)\.gz$" {
    setenv.add-response-header  = ( "Content-Encoding" => "gzip")
    mimetype.assign = ("" => "text/plain" )
  }
			

48.3.11. fastcgi

48.3.11.1. enable fastcgi

enable fastcgi

$ sudo lighty-enable-mod fastcgi
		
48.3.11.1.1. spawn-fcgi
			
#### fastcgi module
## read fastcgi.txt for more info
## for PHP don't forget to set cgi.fix_pathinfo = 1 in the php.ini
fastcgi.server             = ( ".php" =>
                               ( "localhost" =>
                                 (
                                   "socket" => "/tmp/php-fastcgi.socket",
                                   "bin-path" => "/usr/local/bin/php-cgi",
                                   "max-procs" => 16,
                                   "bin-environment" => (
                                      "PHP_FCGI_CHILDREN" => "128",
                                      "PHP_FCGI_MAX_REQUESTS" => "1000"
                                   ),
                                   "broken-scriptfilename" => "enable"
                                 )
                               )
                            )

fastcgi.server    = ( ".php" =>
        ((
                "bin-path" => "/usr/bin/php-cgi",
                "socket" => "/tmp/php.socket",
                "max-procs" => 2,
                "idle-timeout" => 200,
                "bin-environment" => (
                        "PHP_FCGI_CHILDREN" => "10",
                        "PHP_FCGI_MAX_REQUESTS" => "10000"
                ),
                "bin-copy-environment" => (
                        "PATH", "SHELL", "USER"
                ),
                "broken-scriptfilename" => "enable"
        ))
)


			
			
48.3.11.1.2. php-fpm

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

48.3.11.2. PHP

48.3.11.2.1. 編譯安裝PHP

  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

    顯示(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 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]
    				

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

參考php安裝

找到 fastcgi.server 去掉註釋

bin-path 改為PHP程序安裝目錄

fastcgi.server             = ( ".php" =>
                               ( "localhost" =>
                                 (
                                  "socket" => "/tmp/php-fastcgi.socket",
                                   "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

48.3.11.3. Python

sudo apt-get install python
sudo apt-get install python-setuptools
		
48.3.11.3.1. Django
wget http://www.djangoproject.com/download/0.96/tarball/
tar zxvf Django-0.96.tar.gz
cd Django-0.96
python setup.py install
			

生成項目

django-admin.py startproject newtest
			

web server

cd newtest/
./manage.py runserver
			

helloworld.py

from django.http import HttpResponse

def index(request):
    return HttpResponse("Hello, Django.")
			

urls.py

from django.conf.urls.defaults import *

urlpatterns = patterns('',
    # Example:
    # (r'^newtest/', include('newtest.foo.urls')),
    (r'^$', 'newtest.helloworld.index'),

    # Uncomment this for admin:
#     (r'^admin/', include('django.contrib.admin.urls')),
)
			

啟動Web Server

# ./manage.py runserver
Validating models...
0 errors found.

Django version 0.96, using settings 'newtest.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
			

curl http://127.0.0.1:8000/

48.3.11.3.2. Python Imaging Library

Debian/Ubuntu

sudo apt-get install libjpeg62-dev
sudo apt-get install python-imaging
			

採用源碼安裝

tar zxvf Imaging-1.1.6.tar.gz
cd Imaging-1.1.6/
			
sudo python setup.py install
[注意]decoder jpeg not available

首先確認jpeg庫是否安裝

find / -name jpeglib.h

然後修改標頭檔

Imaging-1.1.6/libImaging

修改Jpeg.h, #include "jpeglib.h" 改為

#include "/usr/include/jpeglib.h"

48.3.11.4. Perl

install fastcgi module

$ sudo apt-get install libfcgi-perl	libfcgi-procmanager-perl
		
48.3.11.4.1. Installing lighttpd and FastCGI for Catalyst

The examples also use a virtual host regexp that matches either www.myapp.com or myapp.com

$HTTP["host"] =~ "^(www.)?mysite.com"
			

Starting the FastCGI server

MyApp/script/myapp_fastcgi.pl -l /tmp/myapp.socket -n 5 -d
			

lighttpd.conf

server.document-root = "/var/www/MyApp/root"
			

$ sudo vim /etc/lighttpd/conf-available/10-fastcgi.conf

fastcgi.server = (
    "" => (
        "MyApp" => (
            "socket" => "/tmp/myapp.socket",
            "check-local" => "disable"
        )
    )
)
			

restart lighttpd

neo@master:~$ sudo /etc/init.d/lighttpd restart
 * Stopping web server lighttpd                   [ OK ]
 * Starting web server lighttpd                   [ OK ]
			

Testing

http://127.0.0.1/

More advanced configuration

例 48.4. fastcgi.conf

fastcgi.server = (
    "" => (
        "MyApp" => (
            "socket"       => "/tmp/myapp.socket",
            "check-local"  => "disable",
            "bin-path"     => "/var/www/MyApp/script/myapp_fastcgi.pl",
            "min-procs"    => 2,
            "max-procs"    => 5,
            "idle-timeout" => 20
        )
    )
)
				

48.3.11.5. Ruby

48.3.11.6. UNIX domain sockets

php-fpm.conf

listen = /var/run/fastcgi.socket
		

nginx 配置

    location ~ /index.php/ {
        root           /www/example.com/api.example.com/htdocs;
        fastcgi_pass   unix:/var/run/fastcgi.socket;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /www/example.com/api.example.com/htdocs$fastcgi_script_name;
        include        fastcgi_params;
    }		
		

48.3.12. user-agent

$HTTP["user-agent"] =~ "Googlebot|Sosospider+|eMule|Wget|^Java|^PHP|Ruby|Python" {
  url.rewrite = ( "^/(.*)" => "/crawler.html" )
}
		
$HTTP["user-agent"] =~ "Baiduspider+" {
    connection.delay-seconds = 10
}
		

48.3.13. spdy

server {
    listen 443 ssl spdy;

    ssl_certificate server.crt;
    ssl_certificate_key server.key;
    ...
}