Configuring Lighttpd for CakePHP
I tried a few days ago to install a CakePHP project of mine on a lighttpd web-server. As expected, it’s clean URLs didn’t work, so I set out to find a solution.
One possible solution is the one oulined 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 begining 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.







I’ll try this later on my Joyent server.
Jacob Friis Saxberg
3 Feb 09 at 19:52
Hi Jacob, good luck with setting the server and please come back to tell how it went.
Guy
3 Feb 09 at 20:17
It’s running but makes the browser hang?!
Jacob Friis Saxberg
4 Feb 09 at 03:02
It’s weird. What browser do you use? Is there any errors in the lighttpd error log?
Guy
4 Feb 09 at 08:48
Here’s the solution to my problem:
http://redmine.lighttpd.net/boards/2/topics/756
Your rewrite works. Thanks.
Jacob Friis Saxberg
5 Feb 09 at 02:12
Thanks so much for this solution. It solved a huge problem that I was having with rewrites and lighttpd.
Jared
29 Mar 09 at 04:48
Can you please specify where to put this code in?
Akash Xavier
26 Apr 09 at 15:22
The configuration code goes into /etc/lighttpd/lighttpd.conf (or where ever your lighttpd.conf is).
Guy
26 Apr 09 at 15:28
Thanks for the quick reply Guy
Akash Xavier
26 Apr 09 at 15:43
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?
Akash Xavier
26 Apr 09 at 15:49
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?
Akash Xavier
26 Apr 09 at 15:51
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.
Akash Xavier
26 Apr 09 at 16:07
[...] found a way to get this working from Guy Rutenberg’s site. First open up /etc/lighttpd/lighttpd.conf in you fav editor (if you are using an SSH client, just [...]
CakePHP on lighttpd « Akash Xavier
24 May 09 at 19:28
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
Pat
16 Jun 09 at 13:02
What version of CakePHP do you use, did you change the default paths?
Guy
17 Jun 09 at 13:04
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
Rolf
21 Jun 09 at 13:12
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″,
)
ricardo
5 Feb 10 at 15:32
[...] Die Quelle dieser Lösung (englisch) [...]
CakePHP und lighttpd » CakePHP » PHP und MySQL Werkzeugkasten
15 Mar 10 at 14:27