Installing symfony on IIS
Some of us are stuck with windows servers. A poorer subset of these souls are stuck with IIS on windows. 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, 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, create your project, create your app.
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:
Project as domain
for URLs like http://myproject.com/ we need:
# Block external access to the httpd.ini and httpd.parse.errors files RewriteRule /httpd(?:\.ini|\.parse\.errors).* / [F,I,O] # 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]
Project as subfolder
for URLs like http://myserver/myproject/ we need:
# 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.
A note about logging
By default, IIS with ISAPIRewrite will not log the friendly names. To enable this, you must add the U (unmangle) flag to the last rule in your script. For instance, for the first example above, the last rule would be:
RewriteRule (.*) /index.php [L,U]
The last step is editing the file settings.yml located in the config directory of our symfony project (c:\myproject\myapp\config\ in our example). You won't be surprised to find a little difference between our two options, so:
Project as domain
for URLs like http://myproject.com/, we need these lines:
all: .settings: path_info_key: HTTP_X_REWRITE_URL
Project as subfolder
for URLs like http://myserver/myproject, we need these lines:
all: .settings: relative_url_root: /myproject path_info_key: HTTP_X_REWRITE_URL
The lite version of isapi/rewrite/ does not support per site configurations, and thus placing httpd.ini within the project web dir will do nothing. If you are using the free lite version, you can only place the rules defined above within the global configuration. This file is found in the installation directory of isapi/rewrite typically C:\Program Files\Helicon\ISAPI_Rewrite\httpd.ini. For sites with multiple virtual hosts, a global configuration file *may* cause problems on the other sites.
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 _SERVERPATH_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\myapp\config\settings.yml) and make sure you have the following lines:
all: .settings: relative_url_root: /myapp 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:
That's it, you're done.