zen of coding

Setting up Nginx and CakePHP 2.0

Nginx is a pretty awesome web server (fast, and easy to configure… at least I prefer the syntax over some other popular web servers).

I figured to share the installation process of both CakePHP 2.0 and Nginx on Ubuntu 11.04 (Natty).

Let’s fire up the terminal…

(I presume you have some basic knowledge of *nix so I won’t go into details about the commands, etc.)

sudo apt-get install git

Next, let’s get a fresh version of cake 2:

cd /cake
git clone https://github.com/cakephp/cakephp.git
git checkout 2.0
git pull

Git should tell us that we are “up-to-date”.

Alright, now we have to setup our environment.
The best way I found of going about it (after trying quite a few methods out there) is by the running the excellent set of scripts, which you can find here.
Pull the scripts from the git repo, in the similar way as shown above, into some reasonable local destination. Once you have the files locally, simply run:

sudo ./install.sh

After answering a few questions… our LEMP environment is ready!

Let’s see if Nginx is working as expected…
In the browser head over to localhost, at least at the time of this writing, you get a phpinfo() page served up by default.

So we are satisfied that Nginx is serving up PHP and now it’s time to setup a CakePHP 2.0 app.
When we’ve pulled CakePHP in the very beginning, it came with a skeleton app, which we will use for growing our new one.

Let’s copy it someplace easily accessible (assuming we are in the root of cake… you should see “app”, “vendors”, “lib” directories):

sudo cp -r app /web/

In order not to move cake anywhere, we’ll create a symbolic link to our lib.
(Presuming we are now in the “/web” directory):

sudo ln -s /cake/cakephp/lib lib

So at this point we have php, web server, cake core, skeleton app, mysql all ready to go.
The only remaining part is to tell the web server about our cake app. Similarly to Apache we can setup virtual hosts and rewrite rules in Nginx. I don’t know many details about setting up Nginx and all the rewrite rule tricks available, but as mentioned before, from the examples the syntax looks quite simple and one should be able to decipher the directives relatively easily.

… Being lazy and not wanting to go through the docs, I googled around and thanks to this post it was quite fast to setup a virtual host for the app.

After the installation Nginx will have a setting file in:
/etc/nginx/sites-available/default
(This particular set of installation steps is applicable to Ubuntu, but hopefully you’ll know how to achieve the same procedure in your own OS).

If we review the file quickly it seems like a solid starting point, but we need to have some rewrite rules for cake to make pretty-urls work.
Well, once again thanks to aforementioned post, all we have to do is add the snippet below to our default config (hey, at least it’s working for me):

# rewrite rules for cakephp
  location / {
    root   /web/app/webroot;
    index  index.php index.html;
    try_files $uri $uri/ index.php;

    # If the file exists as a static file serve it
    # directly without running all
    # the other rewite tests on it
    if (-f $request_filename) {
      break;
    }
    if (!-f $request_filename) {
      rewrite ^/(.+)$ /index.php?url=$1 last;
      break;
    }
 }

The root setting is pointing to our app’s webroot, of course… which in trun becomes the root of the virtual host (let’s just use localhost for now, otherwise you’d need to take a few additional steps, but that’s beyond the scope of this post).
Hopefully the code of the setting is not too hard to figure out.

So at this point we have a virtual host pointing to our app and all the settings in place, let’s restart the web server:

sudo /etc/init.d/nginx restart

… and if all goes well, once you visit localhost in your browser, you should see the CakePHP 2.0 welcome page.

  • Somebody

    The first if block is pointless.
    The second if block is pointless if you use try_files.

    location / {
    try_files $uri $uri/ index.php?url=$1;
    }

    Works for me.

  • teknoid

    @Somebody

    Thanks, I’ll try it out.

  • Jose Lorenzo

    This is my setup for CakePHP 2.0:

    location / {
    try_files $uri $uri/ /index.php?$uri&$args;
    expires max;
    access_log off;
    }

    • petteyg359

      Just use $request_uri. That includes both uri and args.

  • Walthee Lalk

    Nice post. Have you tried out Cherokee? I personally prefer it over nginx.

  • Cheers for this, I have been wanting to play about with nginx and this would be the perfect thing to try.

    Really enjoyed your comparison of it and apache as well. Keep the good posts coming!

  • teknoid

    @Jose Lorenzo

    Thanks, I don’t know if I am ready to turn off logging yet… still need my debugging messages… I’ll checkout your setting when I get a minute.

    @Walthee Lalk

    I’ve heard some very good things about Cherokee, but haven’t had a chance to try it.

  • teknoid

    @Toby

    Thanks. Will try ;)
    The comparison was rudimentary, but it is what it is… I am sure there are ways to tune Apache to perform better, however I am not really interested in tuning web servers. I want to install it and let it rip.
    I would love to get some numbers from a real-world application, but that might be a little bit down the road.

  • Jose Lorenzo

    @teknoid

    Actually I just wanted to show that the ?url=$uri stuff is not needed, and that you are forgetting the rest of the query string in your config :)

  • teknoid

    @Jose Lorenzo

    I just tried this, and the only issue I see is that it won’t let me serve info.php, which is a simple file with phpinfo() in it from web root… complains about “missing controller”… I foresee a fun weekend ahead :)

  • Jose Lorenzo

    @teknoid

    Weird, because it checks first for existing files in webroot, then falls back to the CakePHP front controller

  • Michael

    Everything works fine up until I add that snippet to the default file and attempt to restart nginx.

    $ sudo /etc/init.d/nginx restart
    * Stopping Web Server nginx [ OK ]
    * Starting Web Server nginx [fail]
    nginx: [emerg] “location” directive is not allowed here in /etc/nginx/sites-enabled default:62

    Don’t know what I’m doing wrong.. I’ve already made sure root is pointing in the correct spot.

  • teknoid

    @Michael

    Seems like location config is wrong.
    I would paste your file somewhere and check with “nginx authorities”.

  • Pingback: CakePHP : signets remarquables du 30/08/2011 au 05/09/2011 | Cherry on the...()

  • tg2345

    When you setup the link to /cake/cakephp/lib
    Where is the lib folder, there is app, vendors and plugins, and then cake. If you go inside cake there is a libs folder, but I’m some what confused on that part.

  • teknoid

    @tg2345

    I’m not sure, but it sounds like you didn’t check out 2.0 branch.

    • tg2345

      I got it all figured out thanks, I checked out cake 1.3
      The only problem being that cookies that get encrypted don’t work with suhosin, but there have already been fixes for that released.

%d bloggers like this: