博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
49.3. Module
阅读量:6843 次
发布时间:2019-06-26

本文共 13254 字,大约阅读时间需要 44 分钟。

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" )

49.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

49.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 userdirAlready enabled modules: cgi fastcgi simple-vhostEnabling ssl: okRun /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

49.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" )}

49.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"  )}

49.3.4.1. Lighttpd Rewrite QSA

# ApacheRewriteRule ^/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")

49.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"    )}

49.3.6. auth

enable auth

$ sudo lighttpd-enable-mod auth

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

$ sudo vim  conf-enabled/05-auth.confauth.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 .secretneo:chen$ sudo chmod 400 .secret$ sudo chown www-data /etc/lighttpd/.secret

$ sudo /etc/init.d/lighttpd reload

49.3.7. compress

创建cache目录

mkdir -p /var/cache/lighttpd/compress

配置lighttpd.conf文件

找到server.modules列表,去掉"mod_compress"注释,再打开compress module的注释

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

Compressing Dynamic Content

php.ini

zlib.output_compression = Onzlib.output_handler = On

最后使用telnet测试

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

看到乱码输出,而非HTML,表示配置成功.

例 49.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")	}}

49.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" )}

例 49.3. lighttpd expire

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

49.3.9. status

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

49.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"    )}

49.3.10.1. Automatic Decompression

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

49.3.11. fastcgi

49.3.11.1. enable fastcgi

enable fastcgi

$ sudo lighty-enable-mod fastcgi
49.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.inifastcgi.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"        )))
49.3.11.1.2. php-fpm

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

49.3.11.2. PHP

49.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/mirrortar jxvf php-5.2.3.tar.bz2cd 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 \makemake testmake install

    其它有用的模块

    --enable-pcntl
  3. 符号连接

    ln -s /usr/local/php-5.2.3 /usr/local/phpln -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 -vPHP 5.2.2 (cgi-fcgi) (built: May 25 2007 15:50:28)Copyright (c) 1997-2007 The PHP GroupZend 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 GroupZend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies

    使用 php -m 查看PHP Modules

    # bin/php -m[PHP Modules]cgi-fcgictypedatedomfiltergdhashiconvjsonldaplibxmlmssqlpcrePDOpdo_mysqlpdo_sqliteposixReflectionsessionSimpleXMLsnmpSPLSQLitestandardtokenizerxmlxmlreaderxmlwriterzip[Zend Modules]

49.3.11.2.2. 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",                                   "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

49.3.11.3. Python

sudo apt-get install pythonsudo apt-get install python-setuptools
49.3.11.3.1. Django
wget http://www.djangoproject.com/download/0.96/tarball/tar zxvf Django-0.96.tar.gzcd Django-0.96python setup.py install

生成项目

django-admin.py startproject newtest

web server

cd newtest/./manage.py runserver

helloworld.py

from django.http import HttpResponsedef 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 runserverValidating 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/

49.3.11.3.2. Python Imaging Library

Debian/Ubuntu

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

采用源码安装

tar zxvf Imaging-1.1.6.tar.gzcd 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"

49.3.11.4. Perl

install fastcgi module

$ sudo apt-get install libfcgi-perl	libfcgi-procmanager-perl
49.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

例 49.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        )    ))

49.3.11.5. Ruby

49.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;    }

49.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}

49.3.13. spdy

server {    listen 443 ssl spdy;    ssl_certificate server.crt;    ssl_certificate_key server.key;    ...}

原文出处:Netkiller 系列 手札

本文作者:陈景峯
转载请与作者联系,同时请务必标明文章原始出处和作者信息及本声明。

你可能感兴趣的文章
OSS工具篇
查看>>
OpenStack与Kubernetes融合架构下的优化实践
查看>>
Linux基础命令---cpio
查看>>
10 分钟让你明白 MySQL 是如何利用索引的
查看>>
免费报名 | 十年沉淀 阿里双11数据库技术峰会北京站邀您同行
查看>>
IS UNKNOWN
查看>>
jQuery学习(第二天)
查看>>
SQL Server 多表数据增量获取和发布 2.2
查看>>
CAShapeLayer 类解析
查看>>
Vue 组件库 HeyUI@1.17.0 发布,新增 Skeleton 组件
查看>>
PostgreSQL 10.1 手册_部分 III. 服务器管理_第 31 章 逻辑复制_31.2. 订阅
查看>>
java编程学习笔记——mybatis SQL注入问题
查看>>
计算机操作原理进程调度算法---先来先服务,短进程优先(C语言)
查看>>
docker 常用命令
查看>>
一题带你彻底理解 sleep() 和 wait()
查看>>
PostgreSQL 10.1 手册_部分 III. 服务器管理_第 24 章 日常数据库维护工作_24.2. 日常重建索引...
查看>>
阿里短信产品,发送短信的工具类
查看>>
HTML5_CSS3实现iOS Path菜单
查看>>
MySQL基础入门-第一课 新建数据库(linux版本)
查看>>
spring框架的定时任务cronExpression表达式详解
查看>>