Unix 平台的 Lighttpd 1.4

本节包括在 Unix 平台的 Lighttpd 1.4 下安装 PHP 的说明和提示。

推荐阅读 » Lighttpd trac 了解一下正确安装 Lighttpd 然后继续。

推荐使用 FastCGI 作为 SAPI 模块来连接 PHP 和 Lighttpd。 在 php-cgi 中自动启用 FastCGI。

使 Lighttpd 产生 php 进程

要配置 Lighttpd 连接到 PHP 并产生 FastCGI 进程, 编辑 lighttpd.conf。 推荐使用套接字在本机连接 FastCGI 进程。

示例 #1 部分 lighttpd.conf

server.modules += ( "mod_fastcgi" )

fastcgi.server = ( ".php" =>
  ((
    "socket" => "/tmp/php.socket",
    "bin-path" => "/usr/local/bin/php-cgi",
    "bin-environment" => (
      "PHP_FCGI_CHILDREN" => "16",
      "PHP_FCGI_MAX_REQUESTS" => "10000"
    ),
    "min-procs" => 1,
    "max-procs" => 1,
    "idle-timeout" => 20
  ))
)

bin-path 指令允许 lighttpd 动态产生 FastCGI 进程。PHP 会根据 PHP_FCGI_CHILDREN 环境变量产生子进程。bin-environment 指令设定了所产生的进行的环境。PHP 会在达到 PHP_FCGI_MAX_REQUESTS 所指定的请求数目之后杀死一个子进程。在 PHP 中通常应避免 min-procsmax-procs 指令。PHP 自己管理其子进程,并且例如 APC 之类的 opcode 缓存仅在 PHP 管理下的子进程之间共享。如果 min-procs 被设定成某个大于 1 的值,则 PHP 应答器的总数目为该值乘以 PHP_FCGI_CHILDREN(如 min-procs 为 2,PHP_FCGI_CHILDREN 为 16 则会产生 32 个应答器)。

通过 spawn-fcgi 产生进程

Lighttpd 提供一个名为 spawn-fcgi 的程序来简化产生 FastCGI 进程的手续。

产生 php-cgi

有可能不通过 spawn-fcgi 来产生进程,但需要做些工作。设定 PHP_FCGI_CHILDREN 环境变量控制了 PHP 产生多少个子进程来处理请求。设定 PHP_FCGI_MAX_REQUESTS 将决定每个子进程存活多久(以请求数目决定)。 以下为一个简单的 bash 脚本来帮助产生 php 应答器。

示例 #2 产生 FastCGI 应答器

#!/bin/sh

# Location of the php-cgi binary
PHP=/usr/local/bin/php-cgi

# PID File location
PHP_PID=/tmp/php.pid

# Binding to an address
#FCGI_BIND_ADDRESS=10.0.1.1:10000
# Binding to a domain socket
FCGI_BIND_ADDRESS=/tmp/php.sock

PHP_FCGI_CHILDREN=16
PHP_FCGI_MAX_REQUESTS=10000

env -i PHP_FCGI_CHILDREN=$PHP_FCGI_CHILDREN \
       PHP_FCGI_MAX_REQUESTS=$PHP_FCGI_MAX_REQUESTS \
       $PHP -b $FCGI_BIND_ADDRESS &

echo $! > "$PHP_PID"

连接远程 FCGI 实例

FastCGI 实例可被产生于多个远程机器以分散应用程序。

示例 #3 连接远程 php-fastcgi 实例

fastcgi.server = ( ".php" =>
   (( "host" => "10.0.0.2", "port" => 1030 ),
    ( "host" => "10.0.0.3", "port" => 1030 ))
)
add a note add a note

User Contributed Notes 3 notes

up
-6
askroot at gmail dot com
15 years ago
<-- Start -->
#!/bin/bash

/usr/bin/spawn-fcgi \
    -s /var/run/lighttpd/php-fastcgi-kr.php.net.socket \
    -f "/usr/bin/php-cgi -c /etc/php-kr.php.net.ini" \
    -u lighttpd \
    -g lighttpd \
    -C 4 \
    -P /var/run/spawn-fcgi-kr.php.net.pid \
    -F 12
<--  End -->

lighttpd 1.5 - Config
<-- Start -->
$HTTP["host"] =~ "^(www.)?kr.php.net" {
    server.document-root = "/home/kr.php.net"
    accesslog.filename = "/var/log/lighttpd/kr.php.net-access_log"
    index-file.names = ( "index.php", "index.html", "index.htm" )
    url.access-deny = ( "~", ".inc", ".htaccess" )
    server.error-handler-404 = "/error.php"
    alias.url = (
        "/FAQ.php" => "/home/kr.php.net/manual/kr/faq.php",
        "/stats" => "/home/kr.php.net-stats"
    )
    setenv.add-environment = (
        "MIRROR_LANGUAGE" => "kr",
        "MIRROR_STATS" => "1"
    )
    $HTTP["url"] =~ "\.php$" {
        proxy-core.balancer = "round-robin"
        proxy-core.allow-x-sendfile = "enable"
        proxy-core.protocol = "fastcgi"
        proxy-core.backends = ( "unix:/var/run/lighttpd/php-fastcgi-kr.php.net.socket" )
        proxy-core.max-pool-size = 16
    }
}
<-- End -->
up
-7
pittss at gmail dot com
13 years ago
You can custom php.ini just  add environment PHPRC in your virtualhost.

    fastcgi.server    = ( ".php" => ((
        "bin-path" => "/usr/bin/php-cgi",
        "socket" => "/tmp/php.socket",
        "bin-environment" => (
            "PHP_FCGI_CHILDREN" => "3",
            "PHP_FCGI_MAX_REQUESTS" => "1000",
            "PHPRC" => "/usr/www/vhost1/php.ini"
        ),
        "bin-copy-environment" => (
            "PATH", "SHELL", "USER"
        ),
        "broken-scriptfilename" => "enable"
    )))
up
-9
Ant P.
14 years ago
If PHP_FCGI_MAX_REQUESTS isn't given, the default value is 500.
To Top