Lighttpd PHP fastcgi configuration

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)

ADVERTISEMENTS

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

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source & DevOps topics via:
CategoryList of Unix and Linux commands
File Managementcat
Network Utilitiesdig host ip nmap
Package Managerapk apt
Processes Managementbg chroot disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w

ADVERTISEMENTS
21 comments… add one
  • Pierre Jul 3, 2007 @ 10:41

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

    the last “,” is actually causing issues.

  • 🐧 nixCraft Jul 3, 2007 @ 12:04

    Pierre,

    Thanks for the heads up. Post has been updated…

  • RuBiCK Jul 16, 2007 @ 19:47

    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????

  • RuBiCK Jul 16, 2007 @ 22:07

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

  • mmao Nov 24, 2007 @ 3:07

    You also need to create the log dir and the socket file dir, otherwise, the server can not be started

  • mayank Dec 27, 2007 @ 9:04

    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

  • semarsuper Feb 24, 2008 @ 17:19

    mayank: what OS are you running ?
    you need to install lighttpd-fastcgi,

    redhat/fedora/centos
    # yum install lighttpd-fastcgi

  • mumuri May 5, 2008 @ 22:21

    for ubuntu it’s
    apt-get install php5-cgi

  • admin May 14, 2008 @ 11:05

    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.

  • qingqing Jul 17, 2008 @ 20:41

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

    how should i set the configure file for it. thx

  • China pictures Oct 22, 2008 @ 7:06

    Hi,

    I want to have multiple php error_log files without using different php.ini files.

    Any ideas ?

    Thanks

  • Ulrich Jan 10, 2009 @ 15:29

    I’ve read, that it is recommended to set PHP_FCGI_MAX_REQUESTS to something like 500.

  • brainextender May 14, 2009 @ 13:33

    I wonder how to enable fast-cgi for pages with html extension?!

  • needHelp Jul 8, 2010 @ 18:42

    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!

  • Diego Martins Sep 28, 2010 @ 0:16

    Dud, It’s Perfect!Everthing goes fine here! Tanks a lot!

  • Juan Mar 31, 2011 @ 20:30

    It worked on Debian 6, lighttpd 1.4.28, PHP 5.3.6

    Thanks!

  • Stephan Hughson May 13, 2011 @ 10:34

    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

  • Juan Jun 25, 2012 @ 18:12

    You save my day. Thanks.

  • Serj Aug 7, 2012 @ 4:36

    Would have been perfect if instructions for running php in .html or .htm files were included…

  • Jay Dec 7, 2012 @ 13:44

    Big thanks, the solution You have provided works perfectly.

  • Dakota thoughs Oct 5, 2013 @ 20:31

    YOU SAVE MY DAY!
    IT WORKS!

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.