Symfony 1.4 on IIS 6

Getting Symfony to run on anything that isn’t Apache is basically a pain. Before even attempting it you should first make sure it’s completely impossible to spin up a Linux box (Turnkey LAMP anyone?) and run your project on that, or if you’re totally stuck with Windows, consider installing Apache (directly, or by way of WAMP or something similar).

This post is for the unfortunate few who are forced into running Symfony 1.4 on Windows Server under IIS 6 via PHP using FastCGI.

There are a couple of things you’ll need:

If you already have a copy of PHP that’s fine as long as it supports Symfony 1.4 (5.2.4 and above) but keep in mind early versions of 5.3 suffered from unusual date bugs that can be really annoying. Otherwise, install PHP using the installer, or copy the contents of the zip to somewhere like C:\PHP.

Installing FastCGI is relatively painless – everything you need to know is in this guide on the IIS.net website. Just keep in mind that if your path to the php-cgi executable has spaces in it, you will need to quote it, or write it as something like C:\Progra~1\PHP\php-cgi.exe instead.

Installing Symfony can be tiresome if you have no experience with SVN, but trust me it is totally worth it. Pick yourself up a free SVN space at Assembla. If you’re running Symfony, you’re probably a “professional”, and if you’re doing development without an SCM you’re a fucking idiot. Checkout a working copy of your Symfony project trunk to a “dev” folder on your hard drive – NOT to the IIS web root directory. Symfony is designed to be setup as a virtual directory, so all your backend scripts aren’t exposed.

If you want your Symfony project to be the web root of your server, then in the IIS Manager panel, just change the root directory for your site in IIS to your project’s working copy’s web directory. So http://localhost/ should point to something like C:\dev\my_sf_project\web

If you want your Symfony project to be a sub-directory of your server, then you’ll need to add a virtual directory that similarly points to the web folder of the working copy. Just select your server in the IIS Manager panel, and  choose Action->New->Virtual Directory… and select the web folder of your checked-out Symfony project. So http://localhost/my_sf_project/ should point to something like C:\dev\my_sf_project\web.

You’ll also need to create a second virtual directory to the C:\dev\my_sf_project\lib\vendor\data\web\sf directory as per the Symfony 1.4 webserver configuration instructions – this step gives you all the built-in images and crap like that.

Now you need to set up ISAPI rewrite. This is the hard part. Installing it is easy, but tweaking the config that actually rewrites URLs is not easy. Many web admins balk at implementing mod rewrite scripts, and virtually none feel comfortable modifying them once they’re even close to working. You’ll need to edit the config file httpd.conf in C:\Program Files\Helicon\ISAPI_Rewrite3, and depending on your Symfony setup you’ll need different configurations.

Rewrite config for Symfony project as webserver root

Options +FollowSymLinks +ExecCGI
# uncomment the following line, if you are having trouble
# getting no_script_name to work
#RewriteBase /

# we skip all files with .something
RewriteCond %{REQUEST_URI} \..+$
RewriteCond %{REQUEST_URI} !\.html$
RewriteRule /.* - [L]

# we check if the .html version is here (caching)
RewriteRule ^/$ /index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f

# no, so we redirect to our front web controller
RewriteRule ^(.*)$ index.php/$1 [QSA,L]

Rewrite config for Symfony project as virtual directory

Options +FollowSymLinks +ExecCGI
# uncomment the following line, if you are having trouble
# getting no_script_name to work
#RewriteBase /

# we skip all files with .something
RewriteCond %{REQUEST_URI} /my_sf_project/.*\..+$
RewriteCond %{REQUEST_URI} !/my_sf_project/\.html$
RewriteRule /my_sf_project/.* - [L]

# we check if the .html version is here (caching)
RewriteRule ^/my_sf_project/$ /my_sf_project/index.html [QSA]
RewriteRule ^(/my_sf_project/[^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f

# no, so we redirect to our front web controller
RewriteRule ^/my_sf_project/(.*)$ /my_sf_project/index.php/$1 [QSA,L]

Obviously you’ll need to replace my_sf_project with whatever the name of your project’s virtual IIS directory is. Once you’ve done this, everything should work. The ISAPI Rewrite program doesn’t require you to restart IIS when you make changes, they’re reflected immediately.

Other reading:

  • Symfony 1.4 on IIS7 – apparently quite easy!
  • The Symfony 1.0 guide on IIS 6 – still quite relevant, but their rewrite rules are stunted – they don’t deal with the case of http://server/module/action – i.e. no index.php or app name in the URL.

2 thoughts on “Symfony 1.4 on IIS 6”

    1. Yeah, the differences are minor, but they do require some futzing about. I created this article on the basis that it may prevent some other folk from having to do that futzing.

Leave a Reply

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