Thursday, 16 October 2014

Yii 2.0 on Windows under IIS

It is possible to install and run Yii 2.0 on Windows. I did it today and learned some important lessons!

Firstly, you need PHP 5.4 installed because Yii 2.0 uses some features of 5.4. In my case, I had 5.3 and 5.4 installed, 5.4 was already setup in IIS but the path pointed to 5.3 (just to confuse me!).

You will need to use the console as well as IIS, so typing php -v in a console will confirm firstly whether PHP is visible in your path (it doesn't have to be, because you can call it with the full path) and secondly, what version it is.

If you haven't set up PHP in IIS, you will need to do that before you can run the site. Mine uses FastCGI and php_cgi.exe from my php installation. Instructions for that are here: Installing PHP in IIS

Yii recommends using Composer to install Yii 2.0 I already had that installed but if you don't, follow the instructions here: Installing Composer on Windows

If you don't use Composer very often, run \path\to\php composer.phar self-update to make sure it is up to date.

You can run the first command,
php composer.phar global require "fxp/composer-asset-plugin:1.0.0-beta3", 
to require the Yii plugin, in any directory (I ran it from the composer directory because it was easiest). Before copying and pasting the code above, please check with the Yii site that this is the latest composer package.

Now, you are ready to create your first Yii application.

You run the command and point it to where you want your new app to live:
php composer.phar create-project yiisoft/yii2-app-advanced \path\to\app_name 2.0.0
The part in bold is where you point to the new directory you want to create with your app in. When you run this, you will probably be asked for a Github username and password - you will need to create an account if you don't have one. It says it is something to do with rate-limiting but I don't really understand why this is necessary.

 Before you can run the app, you need to do a couple of things. Firstly, as per readme.md in the root of your application, run the init script in the application root directory. There is a Windows batch file which might not work correctly depending on whether your php.exe in the path is the correct version. If not, simply run \path\to\php.exe init and it will work. This script will create some relevant configuration files.

Secondly, you then need to create and configure your database. Create a blank database and preferably a user who only has access to that database and grant them all privileges. Edit \common\config\main-local.php and set the database connection details to match. Then, run yii in the same way as you ran init, either using the batch file or using \path\to\php.exe yii migrate, which  should find a single migration script which will be applied to the database.

Create IIS sites for your frontend (and optional back-end), which point to \approot\frontend\web and \approot\backend\web

 Set the permissions on the entire app folder and children to allow access to the relevant Application Pool user for each of the sites. By default, IIS 7 creates an application pool per site and each application pool has a user created for it. To set permissions, right-click the approot folder in Windows Explorer and choose Properties. Select the Security tab and click the Edit button. In the security dialog, click Add and in the names text box, type IIS AppPool\<app pool name> and press OK, if you have it correct, the name will appear as <app pool name> in the security list and you can give it read & execute access (usually the default).

If you then visit your site, you should see the nice vanilla Yii app, looking all Bootstrappy and ready to modify.

Enjoy.  

 
Post a Comment