Configuring Lighttpd for CakePHP

I tried a few days ago to install a CakePHP project of mine on a lighttpd web-server. As expected, its clean URLs didn’t work, so I set out to find a solution.

One possible solution is the one outlined in the CakePHP manual. The solution uses the mod_magnet module (which basically runs a Lua script to do the rewriting), and I found it an overkill. I was looking for a simple solution based only on mod_rewrite, something like the solution for WordPress.

$HTTP["host"] =~ "^(www\.)?example.com" {
        server.document-root = "/home/guy/example.com/app/webroot/"
        url.rewrite = (
                "(css|files|img|js)/(.*)" => "/$1/$2",
                "^([^\?]*)(\?(.+))?$" => "/index.php?url=$1&$3",
        )
}

I will go line by line over the solution. The first line defines a vhost for the site. This is a simple vhost that uses conditional configurations. The second line sets the server’s document root to my app’s webroot directory (you’ll need to change the path accordingly). The next section is the URL rewriting which is the most important part. The first line of the rewriting rules takes care of the static files you serve from the document root, e.g. CSS, JavaScript, image etc. You can add to the beginning of the rule (the css|files|img|js any other directory you would like to sever static files from). The next line takes care of Cake’s clean URLs. It’s important to capture any query string explicitly, if exists, and pass it in appropriate way so GET variables will be passed correctly.

I hope other CakePHP users who want to develop their apps on lighttpd will find this “lightweight” solution helpful. Don’t hesitate to ask question and post your comments about this.

Edit: This post was written before lighttpd-1.4.24, which offers a better solution. See Sam’s comment below.

25 thoughts on “Configuring Lighttpd for CakePHP

  1. Jared

    Thanks so much for this solution. It solved a huge problem that I was having with rewrites and lighttpd.

  2. Guy Post author

    The configuration code goes into /etc/lighttpd/lighttpd.conf (or where ever your lighttpd.conf is).

  3. Akash Xavier

    I’m trying to add it via Vi editor using my Putty client manually by typing it.

    Any order of the config? I’m putting it in the end.

    And is the tabbing necessary or will the line breaks do after every line?

  4. Akash Xavier

    ah! seems like I can use the include command in the conf file by doing
    include “somefile.conf”

    so can I just add the code snippet into another file and then include it?

  5. Akash Xavier

    Thanks again Guy. My site is working fine now.

    This is what I did:

    I created a file called cakephp_neat_urls.conf in the same dir as lighttpd.conf and then in lighttpd.conf I added an include statement in the end.

    include “cakephp_neat_urls.conf”

    and it worked. πŸ™‚

    I got this idea of using another file when I noticed the other 2 include statements that were already present. I thought using another file would better so that I wouldn’t have to keep messing with the main conf file every time.

  6. Pingback: CakePHP on lighttpd « Akash Xavier

  7. Pat

    I implemented this solution, but the problem is not fixed … the default css files and images are still broken, what might I be doing wrong? (the official solution didnt work either)

    Pat

  8. Rolf

    Hi,

    thanks for your tip.

    It works for me on a new created empty project with cake bake project.

    But when I type http://www.domain.tld/index.php, then cake searches for the index controller.
    I added “(index.php|test.php|favicon.ico)” => “/$1” before the both other rules and the controller error is away. I can’t find drawbacks of my change, but I’m new to webdevelopment and especially to lighttpd.

    Rolf

  9. ricardo

    i have a lot of file in webroot, php, json, html, etc, so i put this lines in config and it works:

    url.rewrite = (
    “(.*).(html|php|json)(.*)” => “/$1.$2$3”,
    “(css|files|img|js)/(.*)” => “/$1/$2”,
    “^([^\?]*)(\?(.+))?$” => “/index.php?url=$1&$3”,
    )

  10. Pingback: CakePHP und lighttpd Β» CakePHP Β» PHP und MySQL Werkzeugkasten

  11. Guy Post author

    I’ve never used mod_userdir, but I you can easily implement mod_userdir functionlity using mod_rewrite or mod_lua.

  12. Sam Mosua

    Hi, found your solution good but not great!
    It helped get me started though =)

    Default CakePHP uses a file existence check to see if it should rewrite; to replicate this behavior use the following rules instead:

    url.rewrite-if-not-file =(
                "^([^?]*)(?(.+))?$" => "/index.php?url=$1&$3"
            )
  13. Guy Post author

    Hi, your solution is indeed better. In my defense, I can say that it only works since lighttpd-1.4.24, which was released almost a year after I wrote this solution πŸ™‚

  14. Jason

    Have you noticed that the links/forums that are generated have the ?url=URL that keeps on building on itself and gets bigger?

    for example, I will have domain.com/admin/orders

    If I click on a link, takes me to domain.com/admin/orders/page:2?url=admin/orders/

    then click on another link, domain.com/admin/orders/page:3?url=admin/orders/page:2?url=admin/orders/

    the url variable just keeps growing.

  15. David.Edmister

    Can you be a little more specifics on which files need to address the mod rewrite rules with CakePhp 3 beta and lighttpd rewrite rules. I’m assuming an edit to wwwroot/.htaccess and the localhost_host.conf files but what goes in where. Thanks in advance

Leave a Reply

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

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.