Lighttpd PHP fastcgi configuration

Posted on in Categories Howto, lighttpd, Linux, Networking, php, RedHat/Fedora Linux, Suse Linux, Sys admin, Ubuntu Linux, UNIX last updated July 7, 2006
Lighttpd logo

FastCGI is a language independent, scalable, open extension to CGI that provides high performance without the limitations of server specific APIs. FastCGI provides better scalability and performance. Instead of creating a new process (the CGI program) for every request, FastCGI uses a single persistent process which handles many requests over its lifetime. (See wikipedia article for more information)

Make sure php support fastcgi

Type any one of the following command to verify that php support fastcgi
$ php -v

Output:

PHP 5.0.4 (cli) (built: Nov  8 2005 08:27:12)
Copyright (c) 1997-2004 The PHP Group
Zend Engine v2.0.4-dev, Copyright (c) 1998-2004 Zend Technologies

OR
$ php-cgi -v
Output:

PHP 5.0.4 (cgi-fcgi) (built: Nov  8 2005 08:25:54)
Copyright (c) 1997-2004 The PHP Group
Zend Engine v2.0.4-dev, Copyright (c) 1998-2004 Zend Technologies

You must get string cgi-fcgi. Next find out full path to php-cgi or php binary:
$ which php-cgi
Output:

/usr/bin/php-cgi

Open lighttpd configuration file:
# vi /etc/lighttpd/lighttpd.conf

First add the module mod_fastcgi (lighttpd provides an interface to a external programs that support the FastCGI interface via this module). Make sure your server.modules loades mod_fastcgi:

server.modules              = (
            "mod_access",
            "mod_accesslog",
            "mod_fastcgi",
            "mod_rewrite",
            "mod_auth"
)

Now add following lines to configuration:

fastcgi.server = ( ".php" => ((
                     "bin-path" => "/usr/bin/php-cgi",
                     "socket" => "/tmp/php.socket"
                 )))

Save the configuration and close all the files. Restart the lighttpd:
# /etc/init.d/lighttpd restart

Test your configuration by running php program or application.

See also

21 comment

  1. There is an typo in:
    fastcgi.server = ( “.php” => ((
    “bin-path” => “/usr/bin/php-cgi”,
    “socket” => “/tmp/php.socket”,
    )))

    the last “,” is actually causing issues.

  2. Hi,

    I get the next error when I try to start lighttpd

    Starting web server: lighttpd2007-07-16 21:43:23: (configfile.c.827) source: /etc/lighttpd/lighttpd.conf line: 164 pos: 31 parser failed somehow near here: socket
    failed!

    The line 146 is the socket configuration for php.

    Any ideas????

  3. It works!

    I write the solution:

    server.modules += ( “mod_fastcgi” )

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

  4. Starting lighttpd: 2007-12-27 14:26:03: (plugin.c.165) dlopen() failed for: /usr/lib/lighttpd/mod_fastcgi.so /usr/lib/lighttpd/mod_fastcgi.so: undefined symbol: ltostr
    2007-12-27 14:26:03: (server.c.621) loading plugins finally failed
    [FAILED]
    i am unable to start it

  5. Sir
    My server with lighttpd is working good and scripts are also running but they are not been abel to connect to there respective databases .Hense I am still on Apache . Please figure it out for me if you can.

    Thanks.

  6. I would like to set up a configuration just using CGI rather than fastcgi.

    how should i set the configure file for it. thx

  7. Hi, I wanted to install Lighttpd with PHP support on my Fedora 12 VM. However, I don’t have access to the internet on the VMware station neither do I have yum installed. Any suggestions on how I could install Lighttpd with PHP support?

    Thanks a lot!

  8. On Ubuntu (and probably Debian too as I bet it’s the same package), you don’t need to add that stuff manually to lighttpd.conf anymore.

    To get php running with lighttpd, you can just do:

    apt-get install lighttpd php5-cgi && lighttpd-enable-mod && service lighttpd restart

Leave a Comment