Lighttpd 1.4 on Unix systems

This section contains notes and hints specific to Lighttpd 1.4 installs of PHP on Unix systems.

Please use the » Lighttpd trac to learn how to install Lighttpd properly before continuing.

FastCGI is the preferred SAPI to connect PHP and Lighttpd. FastCGI is automagically enabled in php-cgi.

Letting Lighttpd spawn php processes

To configure Lighttpd to connect to PHP and spawn FastCGI processes, edit lighttpd.conf. Sockets are preferred to connect to FastCGI processes on the local system.

Example #1 Partial 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
  ))
)

The bin-path directive allows lighttpd to spawn FastCGI processes dynamically. PHP will spawn children according to the PHP_FCGI_CHILDREN environment variable. The bin-environment directive sets the environment for the spawned processes. PHP will kill a child process after the number of requests specified by PHP_FCGI_MAX_REQUESTS is reached. The directives min-procs and max-procs should generally be avoided with PHP. PHP manages its own children and opcode caches like APC will only share among children managed by PHP. If min-procs is set to something greater than 1, the total number of php responders will be multiplied PHP_FCGI_CHILDREN (2 min-procs * 16 children gives 32 responders).

Spawning with spawn-fcgi

Lighttpd provides a program called spawn-fcgi to make the process of spawning FastCGI processes easier.

Spawning php-cgi

It is possible to spawn processes without spawn-fcgi, though a bit of heavy-lifting is required. Setting the PHP_FCGI_CHILDREN environment var controls how many children PHP will spawn to handle incoming requests. Setting PHP_FCGI_MAX_REQUESTS will determine how long (in requests) each child will live. Here's a simple bash script to help spawn php responders.

Example #2 Spawning FastCGI Responders

#!/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"

Connecting to remote FCGI instances

FastCGI instances can be spawned on multiple remote machines in order to scale applications.

Example #3 Connecting to remote php-fastcgi instances

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