Quantcast
Channel: Richard Knop » Zend Framework IIS rewrite rules
Viewing all articles
Browse latest Browse all 2

Making Zend Framework run under IIS

$
0
0

PHP is usually used together with Apache web server but sometimes, mainly during development, it is easier to work on a Windows PC with IIS 7. Fortunately, PHP and IIS 7 are friends and it’s easy to set up a Zend Framework project under IIS 7 webs server. I have decided to merge together few of my older posts which dealt with smaller parts of the whole process. This post should cover installation of IIS 7, PHP, configuration of PHP in IIS manager and finally setting up a Zend Framework project. Let’s begin.

First, you need to install PHP. Download it from here and install it with default settings. On Windows 7 default installation path should be:

C:\Program Files (x86)\PHP\v5.3

Second, install IIS 7. That’s too easy so I won’t give you step by step instructions.

Third, we need to set handler mappings for PHP in IIS manager. Click on Start, type IIS and open Internet Information Services (IIS) Manager:

Open Internet Information Services (IIS) Manager

Open Internet Information Services (IIS) Manager

In IIS Manager click on Handler Mappings:

Click on Handler Mappings in IIS manager

Click on Handler Mappings in IIS manager

Click on Add Module Mapping. In Request Path field type “*.php”, choose “FastCgiModule” as Module and type path to your php-cgi.exe file into Executable (Optional) field, in my case “C:\Program Files (x86)\PHP\v5.3\php-cgi.exe”. Type “PHP_via_FastCGI” into Name field. Click on OK. The handler mapping should look like this:

PHP Handler Mapping

PHP Handler Mapping

Next, we need to tell IIS manager to treat index.php files as default opening points for PHP websites. Open command line and execute this command:

%windir%\system32\inetsrv\appcmd.exe set config ^ -section:system.webServer/defaultDocument /+"files.[value='index.php']" ^ /commit:apphost
Add index.php as default file to IIS 7

Add index.php as default file to IIS 7

You should have PHP installed and configured correctly in IIS 7 now. As a checkpoint, create a new website with different port than the default one (e.g. 81), add index.php file with some simple echo statement to its root directory and open it in a web browser.

Create a new website and test your configuration with simple index.php

Create a new website and test your configuration with simple index.php

In the case of the image above, the URI of that website would be http://localhost:82/. Open it in your web browser. If everything works fine, we can move on.

Create your usual directory structure for Zend Framework in the root of your newly created website. The only thing left is rewriting URIs. Since IIS 7 does not support .htaccess files we are all used to from Apache environments, you will need to install the URL Rewrite module for IIS.

You can use my Zend blank project to create a quick basic structure for Zend Framework project with already configured index.php and bootstrap files. Just download the zipped file and unzip its contents inside the root directory of your newly created website. In case of this tutorial that would be C:\inetpub\test_website. The only thing you need to edit is the include path in index.php, so it will point to your Zend Framework library folder.

Create a file named web.config in the public directory of your Zend Framework project and put this XML code there:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="Imported Rule 1" stopProcessing="true">
          <match url="^.*$" />
          <conditions logicalGrouping="MatchAny">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" pattern="" ignoreCase="false" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" pattern="" ignoreCase="false" />
          </conditions>
          <action type="None" />
        </rule>
        <rule name="Imported Rule 2" stopProcessing="true">
          <match url="^.*$" />
          <action type="Rewrite" url="index.php" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>
Create web.config file in public directory and put rewrite rules in it

Create web.config file in public directory and put rewrite rules in it

Open your website in the browser and it it should work correctly. In case you used my Zend blank project you should see a page like this:

Hurray! You got Zend Framework working like charm under IIS 7 ;)

Hurray! You got Zend Framework working like charm under IIS 7 ;)

Final tip. You might have noticed that IIS 7 by default does not show you any PHP application errors. As a developer surely you need to see the errors so you can fix them. Just open your php.ini file, find display_errors directive and set it to On:

Set display_errors directive in php.ini to On

Set display_errors directive in php.ini to On


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images