Installing symfony on IIS
=========================

Yes, there's something else than Apache for web servers. That's why we'll see in this tutorial how to install symfony on IIS.

What do we need?
----------------

- A windows server (really)
- An IIS server (obviously)
- PHP5 installed and configured in IIS as an isapi module (see PHP5 installation instructions)
- isapi/rewrite/ [www.isapirewrite.com](http://www.isapirewrite.com/), an URL manipulation engine we'll use to replace Apache's mod_rewrite. Why this product? It seems to be mainly used on IIS, and the free lite version works for symfony. If you're used to something else, read on, there should be only one difference in the last chapter of this tutorial.

Install symfony
---------------

First, update the pear package, since you need the version 1.4.0 to handle channels:

    $ pear upgrade PEAR

Then, add the symfony channel:

    $ pear channel-discover pear.symfony-project.com

Install symfony beta version >= 0.5.73:

    $ pear install symfony/symfony-beta

>**Note**: if you don't have the `phing` package, you will need to install it as well:
>
>     $ pear install http://phing.info/pear/phing-current.tgz
>

Find more about [symfony installation](http://www.symfony-project.com/book/trunk/03-Running-Symfony).

Initialize the project
----------------------

To create the directory of your project (let's use `c:\myproject`) and the basic tree structure for an application called `myapp`, open a command console and type :

    $ cd c:\myproject
    $ symfony init-project myproject
    $ symfony init-app myapp


Configure IIS
-------------

We'll consider two configurations options from now on:

- In the first case, the webserver is only used for our symfony project, and the URL is something like `http://myproject/`. Assuming the directory in which you created the directory `myapp` is `c:\myproject\`, configure the root directory of your server to be `c:\myproject\web` (in IIS administration console).
- The other option is to install our symfony project in a directory (may be virtual) of you server, the URL is in this case something like `http://myserver/myproject/`. In IIS administration console, create a new virtual directory on the root of your website for the directory `c:\myproject\web`.

Add a virtual directory in the main directory of your server. Name it `sf` and configure it to `data\symfony\web\sf` of your pear directory. If you installed php5 in `c:\php5` with default configuration, the full path is `C:\php5\PEAR\pear\data\symfony\web\sf`.

Configuration of URL rewriting
------------------------------

We'll assume that isapi/rewrite is installed and running on your server. We have not yet bought it, so we only have one httpd.ini file to configure. The configuration depends on the options defined previously: 

- for URLs like `http://myproject/` we need:

        [ISAPI_Rewrite]
        
        # Defend your computer from some worm attacks
        RewriteRule .*(?:global.asa|default\.ida|root\.exe|\.\.).* . [F,I,O]
        
        # we skip all files with .something except .html
        RewriteCond URL .*\..+$
        RewriteCond URL (?!.*\.html$).*
        RewriteRule (.*) $1 [L]
        
        # we keep the .php files unchanged
        RewriteRule (.*\.php)(.*) $1$2 [L]
        
        # finally we redirect to our front web controller
        RewriteRule (.*) /index.php [L]

- for URLs like `http://myserver/myproject/` we need:

        [ISAPI_Rewrite]
        
        # Defend your computer from some worm attacks
        RewriteRule .*(?:global.asa|default\.ida|root\.exe|\.\.).* . [F,I,O]
        
        # we skip all files with .something except .html
        RewriteCond URL /myproject/.*\..+$
        RewriteCond URL (?!/myproject/.*\.html$).*
        RewriteRule /myproject/(.*) /myproject/$1 [L]
        
        # we keep the .php files unchanged
        RewriteRule /myproject/(.*\.php)(.*) /myproject/$1$2 [L]
        
        # finally we redirect to our front web controller
        RewriteRule /myproject/(.*) /myproject/index.php [L]

You may restart IIS.

Configuring symfony
-------------------

The last step is editing the file `settings.yml` located in the config directory of our symfony project (`c:\myproject\apps\myapp\config\` in our example). You won't be surprised to find a little difference between our two options, so:

- for URLs like `http://myproject/`, we need these lines:

        all:
         .settings:
           path_info_key:          HTTP_X_REWRITE_URL

- for URLs like `http://myserver/myproject`, we need these lines:

        all:
         .settings:
           path_info_key:          HTTP_X_REWRITE_URL

>**Important note**: if you don't use isapi/rewrite/, the `HTTP_X_REWRITE_URL` may be wrong. You'll have to make a specific test to know how to configure symfony. Open the file `myapp_dev.php` (or `whatever you named your application_dev.php`) in the web directory of your project, and add these following lines on line 2 :
>
>     print phpinfo();
>     die();
>
>Now open the following URL : `http://myproject/myapp_dev.php/test/rewrite` (or `http://myserver/myproject/myapp_dev.php/test/rewrite` depending your configuration), and look at the PHP Variables. If `_SERVER["PATH_INFO"]` equals /test/rewrite, remove the `path_info_key` line from `settings.yml`, else you'll have to find the name of the variable containing this value (`HTTP_X_REWRITE_URL` for `isapi/rewrite/`). Remove the previous two lines, symfony is ready.

Configuring a symfony application in its own directory
------------------------------------------------------

A symfony project may contain several applications. In the default configuration, all the applications share the same css and uploads directory. Let's change this and create a specific directory for our application myapp. Considering our previous options, we'll manage to have these URLs : `http://myproject/myapp` and `http://myserver/myapp`. This time, there are no differences in the configuration between these options.

Create a subdirectory `myapp` in the web directory of your symfony project (to have in our case `c:\myproject\web\myapp`).

Copy the files `index.php` and `myapp_dev.php` in this directory, create there two directories named `css` and `uploads`.

In IIS administration console, create a new virtual directory on the root of your server named `myapp`. Configure this virtual directory to `c:\myproject\web\myapp`.

Add these lines at the end of your `httpd.ini` file:

      #[Configuration for direct myapp access]
      # we skip all files with .something except .html
      RewriteCond URL /myapp/.*\..+$
      RewriteCond URL (?!/myapp/.*\.html$).*
      RewriteRule /myapp/(.*) /myapp/$1 [L]
  
      # we keep the .php files unchanged
      RewriteRule /myapp/(.*\.php)(.*) /myapp/$1$2 [L]
  
      # finally we redirect to our front web controller
      RewriteRule /myapp/(.*) /myapp/index.php [L]

Edit the file `settings.yml` of `myapp` application (`c:\myproject\apps\myapp\config\settings.yml`) and make sure you have the following lines:

      all:
       .settings:
         path_info_key:          HTTP_X_REWRITE_URL

(see previous note concerning `HTTP_X_REWRITE_URL`)

Finally, edit both front controllers (`index.php` and `myapp_dev.php` in `c:\myproject\web\myapp`) and change:

    [php]
    define('SF_ROOT_DIR',    realpath(dirname(__FILE__).'/..'));

to:

    [php]
    define('SF_ROOT_DIR',    realpath(dirname(__FILE__).'/../..'));

That's it, you're done.
