LightTPD and CakePHP setup in subdirectories

This article is also available in the following languages:
By baverman
I faced the challenge to install CakePHP under LightTPD. All works smoothly as long as I deploy projects in document root of domain. But when I want to setup CakePHP in subdir all goes wrong.
The source of the problem is env('PHP_SELF'). In the default LightTPD/PHP instalation it is not working. You should tweak some settings.

  1. Add cgi.fix_pathinfo = 1 in your php.ini
  2. Add "broken-scriptfilename" => "enable" into lighttpd.conf at fastcgi.server section.
  3. Add '/subdir' into rewrite rules to emulate apache's RewriteBase
  4. In webroot/index.php define('WEBROOT_DIR', basename(dirname(dirname(__FILE__))));
Here is piece of my lighttpd.conf:
fastcgi.server = ( ".php" =>
  ((
    "socket" => "/usr/local/lighttpd/fcgi/wortex/socket",
    "check-local" => "disable",
    "broken-scriptfilename" => "enable" 
  ))
)

$HTTP["host"] == "example.com" {
  server.document-root = "/var/www/example.com/"
  url.rewrite-once = (
    "/project1/(css|files|img|js|stats)/(.*)" => "/project1/webroot/$1/$2",
    "^/project1/([^.]+)$" => "/project1/index.php?url=$1",

    "/project2/(css|files|img|js|stats)/(.*)" => "/project2/webroot/$1/$2",
    "^/project2/([^.]+)$" => "/project2/index.php?url=$1"
  )
}
'project1' and 'project2' are dirs made by bake.php. Url in browser looks like http://example.com/project1/controller/action

Comments

  • Posted 05/20/10 10:29:14 AM
    Hello, i have made a virtualhost that points to a directory inside my webroot (cake directory) and i made a project called "cakecms" (that is in /var/www/cake/cakecms), this is how it works for me:

    $HTTP["host"] =~ "^(subdomain\.)?example.com$" {
    server.document-root = "/var/www/cake/"
    url.rewrite = (
    "/cakecms/(.*)" => "cakecms/webroot/$1/$2",
    "/cakecms" => "cakecms/webroot/$1/$2",
    "^/(css|files|img|js|stats)/(.*)$" => "app/webroot/$1/$2",
    "^/(.*)$" => "app/webroot/index.php?url=$1"
    )
    }
    I have to put this line:
    "/cakecms" => "cakecms/webroot/$1/$2",
    because if i call subdomain.example.com/cakecms without the trailing slash, it donesn't work... well, for each project i have to make two rules:
    "/project_name/(.*)" => "project_name/webroot/$1/$2",
    "/project_name" => "project_name/webroot/$1/$2",
    One line with trailing slash, and another without it, i know... maybe im writing more code, but... it works !!!! :)

    i hope it works for you people!
  • Posted 07/10/08 07:23:11 PM
    lightys' rewrite mod usually sends 404 with extensions, so i changed the rewrite rule:

    url.rewrite-once = (
    "/(css|files|img|js|stats)/(.*)" => "/$1/$2",
    "^/(.+)/?$" => "/index.php?url=$1"
    )
  • Posted 11/01/07 12:40:04 PM
    I add
    "^/(test.php)(.*)$" => "/test.php$2",
    to url.rewrite-once for testing in 1.2

    url.rewrite-once= (
    "/(css|files|img|js|stats)/(.*)" => "/$1/$2",
    "^/(test.php)(.*)$" => "/test.php$2",
    "^(.*)$" => "/index.php?url=$1"
    )
  • Posted 10/18/07 06:40:31 AM
    Here is the complete solution to getting Cake 1.1x running on Lighttpd 1.5x

    http://thefaultandfracture.blogspot.com/2007/10/enabling-cakephp-11-on-lighttpd-15.html
  • Posted 04/20/07 01:51:41 PM
    Here's the rewrite rule to get cakephp working in lighttpd without the subdirectory structure outlined here.

    url.rewrite-once = (
    "/(.*)\.(.*)" => "$0",
    "/(css|files|img|js)/" => "$0",
    "^/([^.]+)$" => "/index.php?url=$1"
    )

    More info check out this thread on google groups
    http://groups.google.com/group/cake-php/browse_thread/thread/2e1c408e723551e9/59d93ccb66552419?lnk=gst&q=lighttpd&rnum=7#59d93ccb66552419
    Don't forget to turn on mod_rewrite :)
  • Posted 12/24/06 12:45:06 PM
    I am working on a project with other developers who run their code on their local Apache servers. Thus changing webroot/index.php file is not a good solution for me. I want to keep our code as portable as possible while relying on configuration to kink out the differences.
    • Posted 12/25/06 05:01:59 AM
      You may create lighttpd version of index.php (lightttpd-index.php for example) and change rewrite rules.

Comments are closed for articles over a year old