Installing CakePHP on IIS 7

Recently I spoke with someone on Twitter who was having issues running CakePHP on IIS. With all the talk about ASP.NET MVC on IIS, folks forget that the MVC pattern works in other languages as well. CakePHP provides MVC  development on PHP. That said, I wanted to dive in and see what the issues were involved in getting this project up and running on IIS 7. I managed to get it installed pretty quickly, but it does take a little tweaking to get you up and running. I’ve chronicled my adventures with CakePHP below in case anyone else is having issues. That said, I must first say that I am not an expert working with CakePHP. This was my first experience with the project, so this information is provided “as-is” and should be taken with a grain of salt. With this demo, I’ll be walking through the “Cake Blog Tutorial” offered on cakephp.org, and modifying it as needed to work with IIS 7. That said, let’s get started.

Prerequisites

Yes, there ARE a lot of Prerequisites, but these are pretty typical for any MVC app on any platform.

Assumptions / Conventions

For the purposes of this post, I will use the convention/assumption that you have unzipped CakePHP to c:inetpubCakePHP . You should have the following paths now:

  • c:inetpubCakePHP
    • app
    • cake
    • vendors
    • .htaccess
    • index.php
    • version.txt

I will also use the assumption that this is being installed on the “Default Web Site”. This is unlikely what you are doing, so you’ll want to replace the “Default Web Site” instances in the steps below with your site or application path.

Lastly, I will assume that you are using and have already installed MySQL. You may use another database if you please, but this blog will reference MySQL.

Installing the Blog Sample

Pointing IIS to the cake document root

First, you’ll need to configure your website to point to the correct location. Using the assumptions above, the correct location would be c:inetpubCakePHPappwebroot .

Creating a Blog Database

Second, configure your database connection. To do this, you’ll need to create a blog database, and then point your configuration to that new catalog.

Start by creating a new MySQL Catalog using your favorite tool. I used MySQL Administrator. Simply right click in the catalogs and click “create new schema.”

Create a schema named “CakeBlog”. Once the schema is created, click on the “Tools” menu and select “MySQL Query Browser” and execute the following script:

/* First, create our posts table: */
CREATE TABLE posts (
     id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
     title VARCHAR(50),
     body TEXT,
     created DATETIME DEFAULT NOT NULL,
     modified DATETIME DEFAULT NOT NULL8.);

/* Then insert some posts for testing: */
INSERT INTO posts (title,body,created)
      VALUES ('The title',
                  'This is the post body.', NOW());
INSERT INTO posts (title,body,created)
      VALUES ('A title once again',
                  'And the post body follows.', NOW());
INSERT INTO posts (title,body,created)
      VALUES ('Title strikes back',
                  'This is really exciting! Not.', NOW());

* This SQL code copied verbatim from tutorial found here:

You’ve now created your database and a blog posts table with some default posts. Time to configure CakePHP to read from the database:

Cake Database Configuration

We’ll need to let CakePHP know where the database is. Copy database.php.default in c:inetpubCakePHPappconfig to database.php

Open c:inetpubcakephpappconfigdatabase.php and change the $default variable to point to your database:

var $default = array( 'driver'    => 'mysql',
                      'connect'   => 'mysql_connect',
                      'host'      => 'localhost',
                      'login'     => 'CakeBlog',
                      'password'  => 'c4ke-1z-k00l',
                      'database'  => 'CakeBlog',
                      'prefix'    => '' );

* This PHP code copied nearly verbatim from tutorial found here:
http://book.cakephp.org/view/326/The-Cake-Blog-Tutorial

You should now be able to open your browser to your application and see the default cake configuration page.

Setting up Rewriting Rules

CakePHP uses mod_rewrite, but also provides the ability to use Cake’s built-in ‘pretty URLs’. We’ll be importing the mod_rewrite rules from the .htaccess files from the default cakephp installation into the IIS URL Rewrite module. We’ll then have to modify those rules.

Start this process by opening the IIS Management Console. Open your application path. In this instance, we are using “Default Web Site”.

  1. Click on the “Default Web Site
  2. Open the “URL Rewrite” module
  3. Click on “Import Rules…” in the Actions pane
  4. Click the “” button next to the “Configuration file” textbox.
  5. Select the c:inetpubcakephp.htaccess file and click “OK
  6. Click the “Import” button
  7. Click the “Apply” button in the “Actions” pane
  8. Repeat steps 4, 5, 6 and 7 for c:inetpubcakephpapp.htaccess and c:inetpubcakephpappwebroot.htaccess files.

The rules are imported, but now you’ll need to edit the rules.

  1. Click the “Back to Rules” button in the “Actions” pane
  2. Edit the two rules with the action starting with “webroot/
  3. Remove the “webroot” portion of the “Rewrite URL”. Your paths should now look as follows:

Creating your MVC Application

The remainder of your application setup should follow the steps found in the original “Cake Blog Tutorial”. There is nothing different between IIS and Apache at this point, so copying the steps would be a bit redundant. Start with the step named “Create a Post Model”. Much like ASP.NET MVC, Cake provides an MVC pattern for developing PHP applications.

Once you have completed the steps, you should have a default site that looks something like the following:

Summary

Installing CakePHP on IIS is actually not much different from installing on Apache. The main difference lies in the implementation of mod_rewrite on Apache vs URL Rewriter in IIS. Obviously the installation of PHP differs from Apache. IIS makes the installation of PHP simple with Web Application Installer. If you are using CakePHP on IIS, I would be interested to hear if your experience was different than mine.

Be Sociable, Share!

    15 Thoughts on “Installing CakePHP on IIS 7

    1. Pingback: » Installing CakePHP on IIS 7 Tobin Titus | Coder Online

    2. hello~

      I download Cakephp 1.2.6 ver..and after Setting up Rewriting Rules,
      I find that paths have 5 rules…but in your example only have four rules..What’s wrong ?

    3. Elijha on March 8, 2010 at 6:09 am said:

      This all seems straight forward however I’m proabably running into the fact that newer versions are a little different (as always). So mabye you can sugegst or update if at all possible as I’m very new to IIS and especially how it uses URLrewrite.
      CakePHP 1.2.4.8284.
      IIS 7.5 (win 7)
      I’ve done the basic cake install and config for database all OK, however I noticed that the CSS on the default page was not loading so I’ve been looking into the URL rewrite. Your tuturial was one of a very few clearly covering the steps needed (many others simply suggested things like ISAPIrewrite).

      However when I import the 3 .htaccess files in the order as you have written I get 5 Imported rules ( two starting with app/webroot/ and two with webroot/ and on with index.php?).

      If I do not edit the rules (deleting the ‘webroot’ or ‘app/webroot’) then on the initial cakePHP page the CSS file is loaded and it all looks normal, however follwing the tutorial as link and attempting through several others (such as http://www.ibm.com/developerworks/opensource/tutorials/os-php-cake1/index.html) gives me a 404 error when viewing somthing like http://localhost/users/register .

      Deleting the ‘webroot’ and ‘app/webroot’ from the rewrite rules does not help and stops CSS from loading on the default page also.

      Here is the created web.config as imported from a current default cakePHP install on IIS 7

      Any help would be apreciated.

    4. Elijha on March 8, 2010 at 6:11 am said:

      looks like the config was blocked due to brackets .. here it is with different brackets (should be easy enough to find-replace on it)

      (?xml version=”1.0″ encoding=”UTF-8″?)
      (configuration)
      (system.webServer)
      (rewrite)
      (rules)
      (rule name=”Imported Rule 1″ stopProcessing=”true”)
      (match url=”^$” ignoreCase=”false” /)
      (action type=”Rewrite” url=”app/webroot/” /)
      (/rule)
      (rule name=”Imported Rule 2″ stopProcessing=”true”)
      (match url=”(.*)” ignoreCase=”false” /)
      (action type=”Rewrite” url=”app/webroot/{R:1}” /)
      (/rule)
      (rule name=”Imported Rule 3″ stopProcessing=”true”)
      (match url=”^$” ignoreCase=”false” /)
      (action type=”Rewrite” url=”webroot/” /)
      (/rule)
      (rule name=”Imported Rule 4″ stopProcessing=”true”)
      (match url=”(.*)” ignoreCase=”false” /)
      (action type=”Rewrite” url=”webroot/{R:1}” /)
      (/rule)
      (rule name=”Imported Rule 5″ stopProcessing=”true”)
      (match url=”^(.*)$” ignoreCase=”false” /)
      (conditions logicalGrouping=”MatchAll”)
      (add input=”{REQUEST_FILENAME}” matchType=”IsDirectory” negate=”true” pattern=”" ignoreCase=”false” /)
      (add input=”{REQUEST_FILENAME}” matchType=”IsFile” negate=”true” pattern=”" ignoreCase=”false” /)
      (/conditions)
      (action type=”Rewrite” url=”index.php?url={R:1}” appendQueryString=”true” /)
      (/rule)
      (/rules)
      (/rewrite)
      (/system.webServer)
      (/configuration)

    5. Brian on April 26, 2010 at 6:54 am said:

      Quick note, if you have static compression enabled on your site the URL Rewrite module will not work.

      This is really easy to fix, but can obviously drive you nuts if overlooked.

      To disable static compression,
      In IIS select the site you have cake installed in (same place you are doing all the rewrite rules).
      open Compression, and make sure neither check box is checked, then click apply.

      -You might have to restart your website for the changes to occur.

    6. Thanks for this, but I can’t get it to work… I don’t have access to the IIS Management Console, so I’m changing them in an editor.

      I’m not clear on what I’m supposed to change… here’s what I have:

      In /app/webroot my .htaccess file is now:

      RewriteEngine On
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteRule ^(.*)$ /index.php?url=$1 [QSA,L]

      AddType application/x-javascript .js
      AddType text/css .css

      In /app the .htaccess file is:

      RewriteEngine on
      RewriteRule ^$ / [L]
      RewriteRule (.*) /$1 [L]

      and in / .htaccess is:

      RewriteEngine on
      RewriteRule ^$ /app/ [L]
      RewriteRule (.*) /app/$1 [L]

      Do those look right? Thanks!

    7. kimvm on May 25, 2010 at 6:16 am said:

      Following both your tutorial and also http://book.cakephp.org/complete/1528/Blog/cakephp/cakephp1x#URL-Rewrites-on-IIS7-Windows-hosts-1636

      I can’t get the css loaded

      In code (cakePhp):
      css(‘style’)); ?>

      Translated in HTML by cakePHP:

      But somehow the url_rewrite does not translate the ‘/css/style.css’ to app/webroot/css/

      I’ve tried a rewrite rule for this but without any luck:
      RewriteRule ^/(img|css|files|js)(.*)$ /{1}{2}

      Suggestions?

    8. kimvm on May 26, 2010 at 12:32 am said:

      Is still can’t get the css, js and img loaded.

      I’ve been using the web.config out of this article:
      http://book.cakephp.org/view/1533/A-Note-on-mod_rewrite

      Please any suggestions…

    9. Arrovs on July 15, 2010 at 11:52 am said:

      Great surwey.
      It all works perfectly.
      Only difference is that iis7 have five rules and dont need change anything only accept import.
      When it is done, reload page and voila ready to go.

    10. I discovered that as of CakePHP 1.3 you don’t have to modify the imported URL Rewrite rules. Just leave the as they were imported and they should be fine.

      If you are worried about it and you already modified the rules, disable the modified rules and reimport the .htaccess file. Now test your app, if everything works, delete the disabled rules and you’re ready to go. If it doesn’t work for you, at least your no worse off than you were before.

    11. OK .. after giving up (yep my comment above was way back in March) and installing / developing on a unix box I have gone back and got it running fine on IIS 7.5 under windows 7.
      Key points
      1) – Install as PRODUCTION
      see http://book.cakephp.org/view/914/Production ie under your IIS Manager controll panel go to the ‘Basic Settings’ of the site and set the ‘Physical path’ to somthing like C:inetpubwwwrootcakeappwebroot – not just your cake directory.

      2) use the URL Rewrite settings as shown http://book.cakephp.org/view/1636/URL-Rewrites-on-IIS7-Windows-hosts they do work.

    12. Thanks Elijha for comming back and post the working rewrite rules!

    13. I have a Win 2008 IIS 7.5 FastCGI PHP 5.2 web server with Url Rewrite 2.0. I was successful in combining several posts (found through google) and combined the code into one web.config file that: 1. Shows CSS/Imgs 2. Allows url rewrites – ex. domain.com/posts (works)… and domain.com/posts/view/1 (works)… and domain.com/index.php/posts/view/1 (works).

      I added this code to my web.config file and used ftp to transfer the file. I did not import the .htaccess files.

      Here is the web.config code:

      I hope this helps others.

    14. Here is the code with ( and ). Use search/replace.

      (?xml version=”1.0? encoding=”UTF-8??)
      (configuration)
      (system.webServer)
      (rewrite)
      (rules)
      (rule name=”Imported Rule 0? stopProcessing=”true”)
      (match url=”^(img|css|files|js)(.*)$”)(/match)
      (action type=”Rewrite” url=”/app/webroot/{R:1}{R:2}” appendQueryString=”false”)(/action)
      (/rule)
      (rule name=”Imported Rule 1? stopProcessing=”true”)
      (match url=”^(.*)$” ignoreCase=”false” /)
      (conditions logicalGrouping=”MatchAll”)
      (add input=”{REQUEST_FILENAME}” matchType=”IsDirectory” negate=”true” /)
      (add input=”{REQUEST_FILENAME}” matchType=”IsFile” negate=”true” /)
      (/conditions)
      (action type=”Rewrite” url=”index.php?url={R:1}” appendQueryString=”true” /)
      (/rule)
      (rule name=”Imported Rule 2? stopProcessing=”true”)
      (match url=”^$” ignoreCase=”false” /)
      (action type=”Rewrite” url=”/” /)
      (/rule)
      (rule name=”Imported Rule 3? stopProcessing=”true”)
      (match url=”(.*)” ignoreCase=”false” /)
      (action type=”Rewrite” url=”/{R:1}” /)
      (/rule)
      (rule name=”Imported Rule 4? stopProcessing=”true”)
      (match url=”^(.*)$” ignoreCase=”false” /)
      (conditions logicalGrouping=”MatchAll”)
      (add input=”{REQUEST_FILENAME}” matchType=”IsDirectory” negate=”true” /)
      (add input=”{REQUEST_FILENAME}” matchType=”IsFile” negate=”true” /)
      (/conditions)
      (action type=”Rewrite” url=”index.php?url={R:1}” appendQueryString=”true” /)
      (/rule)
      (/rules)
      (/rewrite)
      (/system.webServer)
      (/configuration)

    15. Amazing…..Nothing works as per the instructions above. The site uploads with or without the rules and the IMG, CSS, JS, etc. do not load with or without rules.

    Leave a Reply

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

    You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

    Post Navigation