Project

General

Profile

Actions

TutorialLighttpdAndPHP » History » Revision 72

« Previous | Revision 72/91 (diff) | Next »
Anonymous, 2008-09-14 11:25


PageOutline = Setting up PHP with Lighttpd =
Introduction

Lighttpd supports PHP through both CGI and FastCGI. As the name suggests, FastCGI is preferable.

Performance

In all our tests it has shown better performance than Apache 1.3.x + mod_php4. See http://www.lighttpd.net/benchmark/ for the results.

Does it support opcode cachers?
like APC, TurckMM, XCache and friends

YES! Even if their docs say they do not work in CGI. Under lighttpd, PHP is usually run as FastCGI which supports those opcode cachers like mod_php4 in Apache.

(more to add)

You might be interested in reading the [http://sourceforge.net/forum/forum.php?thread_id=1205624&forum_id=416741 Support for FASTCGI mode?] thread from eAccelerator Open Discussion Forum.

= Installation =

=== Windows ===

==== PHP in CGI Mode ====

First you need to install [http://en.wlmp-project.net/downloads.php Lighttpd for Windows]. Then you need to edit C:\lighttpd\etc\lighttpd.conf, uncomment the "mod_cgi" line and add this line: {{{
cgi.assign = ( ".php" => "c:/php/php-cgi.exe" )
}}}
Be careful, there cannot be any space in the path to PHP-CGI in the above snippet. Save lighttpd.conf and restart lighttpd. Open your browser and go to [http://localhost/ http://localhost/], you should have a welcome page. You can put your PHP files in C:\lighttpd\htdocs - To test PHP, just create a file named index.php, save it in the Lighttpd root directory (C:\lighttpd\htdocs) and paste this content into this file: {{{
phpinfo();
?>
}}}
Accessing [http://localhost/ http://localhost/], you should get a page that lists all PHP variables and information.
Please note that FastCGI doesn't work on Windows at this time. Good luck!

==== PHP in Fast CGI Mode ====

Download the complete WLAMP package or the latest LightTPD build for Win32 from from http://wlmp.dtech.hu/down_lighty.php?lang=en
If you choose the later you must get the latest PHP package from http://www.php.net/downloads.php
Unpack the distribution so you have a directory structure similar to this:
c:\LightTPD
c:\PHP

Goto c:\LightTPD\lighttpd-inc.conf if you use the default setup or edit your lighttpd configuration file and uncomment the line "mod_fastcgi" and add the following line: {{{
fastcgi.server = ( ".php" =>( "localhost" =>("host" => "127.0.0.1","port" => 521 )))
}}}
Host and port points to the ip address and port where the fastcgi daemon will be listening. I have not tried this, but i guess it should work if you have two machines and you put the fastcgi daemon in other machine. In the above lines Lighttpd and PHP FastCGI are on the same machine.

Next start the php-cgi daemon from the php directory:
c:\php\php-cgi.exe -b 127.0.0.1:521
And then (re)start the LightTPD web server.

You can put your PHP files in C:\lighttpd\htdocs - To test PHP, just create a file named index.php, save it in the Lighttpd root directory (C:\lighttpd\htdocs) and paste this content into this file: {{{
phpinfo();
?>
}}}
Accessing [http://localhost/ http://localhost/], you should get a page that lists all PHP variables and information.

If all is OK you could write a batch file to start/stop the server and the PHP daemon at the same time. Please note that you should have the RunHiddenConsole.exe in your path, also this files should be placed in c:\LightTPD

Start-LightTPD.bat {{{
@ECHO OFF
ECHO Starting PHP FastCGI...
RunHiddenConsole.exe ..\PHP\php-cgi.exe -b 127.0.0.1:521
ECHO Starting LightTPD...
ECHO.
lighttpd.exe -v
ECHO.
lighttpd.exe -f conf\lighttpd-srv.conf -m lib
EXIT
}}}

Stop-LightTPD.bat {{{
@ECHO OFF
ECHO Stopping LightTPD...
bin\process.exe -k lighttpd.exe >nul
ECHO Stopping PHP FastCGI...
bin\process.exe -k php-cgi.exe >nul
ECHO.
EXIT
}}}

=== Unix Systems ===

'''Startingpoint:''' Lighttpd is already [wiki:TutorialInstallation installed and working]

First of all you need a PHP which is providing FastCGI support. Depending on your
distribution you might already have it: === !ArchLinux ===
Read http://wiki.archlinux.org/index.php/Fastcgi_and_lighttpd

=== FreeBSD === {{{
#!ShellExample
$ cd /usr/ports/lang/php5
  1. make install clean
    }}}
    Be sure to check the "use as FastCGI" option in the configuration screen. If you don't see the configuration screen, you're either using an old version of ports tree, or need to execute "make config". If you're using PHP 4 or an early version of PHP 5, you may have to use www/php4-cgi or a similar port instead of the new master port.

=== PC-BSD ===
Download the [http://www.pbidir.com/search.php?str=lighttpd self-executable setup wizard], double-click the file, and follow the instructions of the setup wizard. You will have a "www" directory under your home directory to put your web pages. You'll have the possibility to launch lighttpd at system startup automatically, and to support one or several users. Once you're done, just open http://localhost/ in your favorite browser and you should see the welcome screen.

=== Gentoo ===
Make sure the USE flag 'cgi' is enabled. {{{
#!ShellExample
$ emerge -av dev-lang/php
}}}
If it is not, add the USE flag your /etc/make.conf or just to the dev-lang/php ebuild. {{{
#!ShellExample
  1. echo dev-lang/php cgi >> /etc/portage/package.use
    }}}
    XCache is also in the official tree (as of 2007-02-03), so emerge dev-php5/xcache should simply work out-of-the-box as well.
=== Debian / Ubuntu ===
Debian/Ubuntu provides a fastcgi enabled version. {{{
#!ShellExample
  1. apt-get install php4-cgi
    }}}
    If you're using php5-cgi, all you need is change your "bin-path" at your [http://trac.lighttpd.net/trac/wiki/TutorialLighttpdAndPHP#Configuration configuration] (Debian uses /usr/bin/php4-cgi as default). === pkgsrc (NetBSD, DragonFlyBSD and others) ===
    Add {{{
    PKG_OPTIONS.php = fastcgi
    }}}
    to your mk.conf. PHP can found in www/php4 and lang/php5. The fastcgi binary is located in /usr/pkg/libexec/cgi-bin/php === Others ===
    Download a source tar-ball from http://www.php.net/ and configure it with at least this settings: {{{
    #!ShellExample
    $ ./configure \
    --enable-fastcgi \
    --enable-discard-path \
    --enable-force-cgi-redirect
    }}}
    If you want to have the same PHP as you are using in a mod_php configuration somewhere else call {{{
    #!php

    }}} * copy the configure options from the output of the script * remove the ``--with-apxs`` and ``--with-apxs2`` options * add the three options from above.
    Build PHP now by callings ``make`` and ``make install`` and see if you can find a php binary which is responding: {{{
    #!ShellExample
    $ php -v
    PHP 5.0.3 (cgi-fcgi) (built: Dec 21 2004 12:59:18)
    Copyright (c) 1997-2004 The PHP Group
    Zend Engine v2.0.3, Copyright (c) 1998-2004 Zend Technologies
    with eAccelerator v0.9.3, Copyright (c) 2004-2005 eAccelerator, by eAccelerator
    }}}
    or something like this. The ``(cgi-fcgi)`` is the important part. The binary might also be called ``php-cgi``.
    so do something like php-cgi -v to see the (cgi-fcgi) banner. {{{
    #!ShellExample
    $ php-cgi -v
    PHP 4.4.2 (cgi-fcgi) (built: Jul 21 2006 15:45:58)
    Copyright (c) 1997-2006 The PHP Group
    Zend Engine v1.3.0, Copyright (c) 1998-2004 Zend Technologies
    }}}

If you still can't find it follow this hint: {{{
#!ShellExample
$ ls sapi/cgi/php*
}}}
Configuration
Add this line:

{{{
cgi.fix_pathinfo = 1
}}}

to your ``php.ini``.

And make sure mod_fastcgi is loaded in your ``lighttpd.conf``: {{{
server.modules = (
"mod_fastcgi",
)
}}}

Then add this basic section to the same file: {{{
fastcgi.server = ( ".php" => ((
"bin-path" => "/path/to/php-cgi",
"socket" => "/tmp/php.socket"
)))
}}}

A little bit more advance is this setting which tries the tune some more options. If you need PATH_INFO the broken-scriptfilename is
for you. {{{
fastcgi.server = ( ".php" => ((
"bin-path" => "/path/to/php-cgi",
"socket" => "/tmp/php.socket",
"max-procs" => 2,
"bin-environment" => (
"PHP_FCGI_CHILDREN" => "16",
"PHP_FCGI_MAX_REQUESTS" => "10000"
),
"bin-copy-environment" => (
"PATH", "SHELL", "USER"
),
"broken-scriptfilename" => "enable"
)))
}}}

It is important to set '''"max-procs"''' to 1 if you're using any php opcode cacher, unless you know what you're doing. Increase PHP_FCGI_CHILDREN if you want more childs serving the request.

Please read the configuration section for more background * [wiki:Docs:Configuration] * [wiki:Docs:ModFastCGI]
and use
http://trac.lighttpd.net/trac/browser/tags/lighttpd-1.4.11/doc/lighttpd.conf as starting point for the configuration.

Per directory PHP Config
If you're used to use Apache .htaccess files to set PHP options for each directory, see HowToPhpHtaccess :

''When using a cgi version of php (plain old cgi or fast-cgi) apache can't pass
any php settings from htaccess files it parses. This can be solved by giving
each user its own php.ini file, but I didn't like that solution.
This extension parses these configuration files (in most cases .htaccess) and
changes the settings. It will search all directories for a configuration file
from the docroot until the directory where the request scripts is found. A cache
is implemented to minimize the performance impact.''

[http://pecl.php.net/htscanner Download htscanner]

See Also * [wiki:HowToSetupFastCgiIndividualPermissions How to setup fastcgi and php with individual permissions] * [wiki:HowToInstallOnFreeBSD How to setup PHP and Lighty in FreeBSD] * [wiki:fastcgi-php-starter-for-freebsd Handy External Spawning FastCGI PHP Processes in FreeBSD] * [wiki:HowToFightDeepLinking Using a PHP to control remote linking] External links * [http://www.cyberciti.biz/tips/howto-setup-lighttpd-php-mysql-chrooted-jail.html Howto: Lighttpd FasCGI PHP, MySQL chroot jail installation under Debian Linux] * [http://www.bigbold.com/snippets/posts/show/321 lighttpd php-fastcgi config with its own php.ini]

Updated by Anonymous over 15 years ago · 72 revisions