Home | 簡體中文 | 繁體中文 | 雜文 | 知乎專欄 | Github | OSChina 博客 | 雲社區 | 雲棲社區 | Facebook | Linkedin | 視頻教程 | 打賞(Donations) | About
知乎專欄多維度架構 微信號 netkiller-ebook | QQ群:128659835 請註明“讀者”

第 48 章 Lighttpd

目錄

48.1. 安裝Lighttpd
48.1.1. quick install with aptitude
48.1.2. yum install
48.1.3. to compile and then install lighttpd
48.1.3.1. shell script
48.2. /etc/lighttpd/lighttpd.conf
48.2.1. max-worker / max-fds
48.2.2. accesslog.filename
48.2.3. ETags
48.2.4. server.tag
48.3. Module
48.3.1. simple_vhost
48.3.2. ssl
48.3.3. redirect
48.3.4. rewrite
48.3.4.1. Lighttpd Rewrite QSA
48.3.5. alias
48.3.6. auth
48.3.7. compress
48.3.8. expire
48.3.9. status
48.3.10. setenv
48.3.10.1. Automatic Decompression
48.3.11. fastcgi
48.3.11.1. enable fastcgi
48.3.11.1.1. spawn-fcgi
48.3.11.1.2. php-fpm
48.3.11.2. PHP
48.3.11.2.1. 編譯安裝PHP
48.3.11.2.2. apt-get install
48.3.11.3. Python
48.3.11.3.1. Django
48.3.11.3.2. Python Imaging Library
48.3.11.4. Perl
48.3.11.4.1. Installing lighttpd and FastCGI for Catalyst
48.3.11.5. Ruby
48.3.11.6. UNIX domain sockets
48.3.12. user-agent
48.3.13. spdy
48.4. 其他模組
48.4.1. mod_secdownload 防盜鏈
48.5. Example
48.5.1. s-maxage

48.1. 安裝Lighttpd

48.1.1. quick install with aptitude

if you OS is Ubuntu/Debian

apt-get install lighttpd
netkiller@shenzhen:~$ sudo apt-get install lighttpd
		

the config file in /etc/lighttpd

netkiller@shenzhen:~/document/Docbook/Linux$ find /etc/lighttpd/
/etc/lighttpd/
/etc/lighttpd/lighttpd.conf
/etc/lighttpd/conf-enabled
/etc/lighttpd/conf-available
/etc/lighttpd/conf-available/10-userdir.conf
/etc/lighttpd/conf-available/10-fastcgi.conf
/etc/lighttpd/conf-available/10-cgi.conf
/etc/lighttpd/conf-available/README
/etc/lighttpd/conf-available/10-ssl.conf
/etc/lighttpd/conf-available/10-proxy.conf
/etc/lighttpd/conf-available/10-auth.conf
/etc/lighttpd/conf-available/10-simple-vhost.conf
/etc/lighttpd/conf-available/10-ssi.conf
		

Enabling and disabling modules could be done by provided e.g.

/usr/sbin/lighty-enable-mod fastcgi
/usr/sbin/lighty-disable-mod fastcgi
		

when you enabled a mod please force-reload it

netkiller@shenzhen:/etc/lighttpd$ sudo lighty-enable-mod fastcgi
Available modules: auth cgi fastcgi proxy simple-vhost ssi ssl userdir
Already enabled modules: userdir
Enabling fastcgi: ok
Run /etc/init.d/lighttpd force-reload to enable changes
netkiller@shenzhen:/etc/lighttpd$ sudo /etc/init.d/lighttpd force-reload
 * Stopping web server lighttpd                                                                                                                                                                                                                                                                                       [ OK ]
 * Starting web server lighttpd
		

48.1.2. yum install

# yum install lighttpd lighttpd-fastcgi -y
# chkconfig lighttpd on
		

創建緩存目錄

# mkdir -p /var/cache/lighttpd/compress
# chown lighttpd:lighttpd -R /var/cache/lighttpd
		

禁用ipv6

# vim /etc/lighttpd/lighttpd.conf

#server.use-ipv6 = "enable"
		

48.1.3. to compile and then install lighttpd

  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

48.1.3.1. shell script

lighttpd script

例 48.1. /etc/init.d/lighttpd

					
#!/bin/bash
# lighttpd init file for web server
#
# chkconfig: - 100 100
# description: Security, speed, compliance, and flexibility--all of these describe LightTPD which is rapidly redefining efficiency of a webserver;
#				as it is designed and optimized for high performance environments.
# author: Neo Chen<openunix@163.com>
#
# processname: $PROG
# config:
# pidfile: /var/run/lighttpd

# source function library
. /etc/init.d/functions

PREFIX=/usr/local/lighttpd
PROG=$PREFIX/sbin/lighttpd
OPTIONS="-f /usr/local/lighttpd/etc/lighttpd.conf"
USER=daemon
RETVAL=0
prog="lighttpd"

start() {
        echo -n $"Starting $prog: "
        if [ $UID -ne 0 ]; then
                RETVAL=1
                failure
        else
                daemon --user=$USER $PROG $OPTIONS
                RETVAL=$?
                [ $RETVAL -eq 0 ] && touch /var/lock/subsys/lighttpd
        fi;
        echo
        return $RETVAL
}

stop() {
        echo -n $"Stopping $prog: "
        if [ $UID -ne 0 ]; then
                RETVAL=1
                failure
        else
                killproc $PROG
                RETVAL=$?
                [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/lighttpd
        fi;
        echo
        return $RETVAL
}

reload(){
        echo -n $"Reloading $prog: "
        killproc $PROG -HUP
        RETVAL=$?
        echo
        return $RETVAL
}

restart(){
	stop
	start
}

condrestart(){
    [ -e /var/lock/subsys/lighttpd ] && restart
    return 0
}

case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  restart)
	restart
        ;;
  reload)
	reload
        ;;
  condrestart)
	condrestart
	;;
  status)
        status lighttpd
	RETVAL=$?
        ;;
  *)
	echo $"Usage: $0 {start|stop|status|restart|condrestart|reload}"
	RETVAL=1
esac

exit $RETVAL