Installing Yii in 10 Easy Steps

I've been working with the Yii Framework for a few months now, and have been massively impressed so far. The amount of foresight that went into the design and architecture of this framework is mind boggling, and I keep getting reminded of this over and over again when I'm trying to get a very specific task done, and I learn that a well-documented, simple method or class already exists expressly for this purpose. I'm really surprised that Yii hasn't been getting a lot of chatter in the development community (at least from the people I've been listening to).

I've thrown together a few Yii applications so far, and I thought I would document a few installation/configuration processes for my benefit, and for anyone else who stumbles across this. All of this is available in disparate locations, including the official Yii reference, but I wanted to list things out as simply as possible, detailing how I got it to work for my setup.

(Note: I started using Yii the same time I started developing in Mac OS X, so I had a two-fold learning curve. If I'm unclear or incorrect about some of the steps I detail, please feel free to point out the mistakes in the comments section.)

Here's how to perform a very simple Yii installation that connects to a database. In this example I'm calling the application 'test' — you can name yours whatever you want.

Step 1. Download and unzip the Yii framework: http://www.yiiframework.com/

Step 2. Make a new directory named 'test' in webroot (or a subdirectory). For me, this location is ~/Sites/yii/test

Step 3. Move the recently-downloaded yii directory into the new 'test' directory. For convenience, I like to leave the name of the Yii framework directory as-is, and create a symbolic link to it: ln -s yii-1.1.8.r3324/ yii

Step 4. Use Yii's yiic tool to create the web application code. Navigate to to the project root directory (the 'test' directory in this example) and run:

yii/framework/yiic webapp webapp.

This will create a directory with the name "webapp."

Step 5. Edit the virtual hosts configuration to point to this installation. Here is how I did this, along with what I added:

cd /opt/local/apache2/conf/extra
sudo vi httpd-vhosts.conf

I added following:

<VirtualHost *:80>
  <Directory "/Users/ben/Sites/yii/test/webapp">
    Options Indexes FollowSymLinks
    AllowOverride None
    Order deny,allow
    Allow from all
    Satisfy all

    IndexIgnore */*
    RewriteEngine on
    # if a directory or a file exists, use it directly
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    # otherwise forward it to index.php
    RewriteRule . index.php
  </Directory>

  ServerAdmin info@benjaminlhaas.com
  DocumentRoot "/Users/ben/Sites/yii/test/webapp"
  ServerName test.local
  ServerAlias test.local
  ErrorLog "/private/var/log/apache2/yii-test-error_log"
  CustomLog "/private/var/log/apache2/yii-test-access_log" common
</VirtualHost>

With this in place, I can address my web application by test.local. Additionally, I've setup my access and error logs, so I can debug by outputting messages via error_log().

Step 6. Edit the hosts file as such:

cd /private/etc
sudo vi hosts

I added the following line:
127.0.0.1 test.local

Step 7. Restart Apache:
sudo /opt/local/apache2/bin/apachectl restart

Step 8. At this point, the code is all set and hopefully everything works. Pull up a browser, navigate to test.local and verify "My Web Application" loads.

Step 9. Make a MySQL database and user. I did this by executing the following commands:

CREATE DATABASE test;
CREATE USER 'test_dbuser'@'localhost' IDENTIFIED BY 'Password1';
GRANT INSERT, SELECT, UPDATE, INDEX, DELETE, CREATE, DROP, ALTER, SHOW VIEW, CREATE VIEW ON test.* TO 'test_dbuser'@'localhost';

Step 10. Configure the Yii application. Open /webapp/protected/config/main.php in a text editor and make the following changes:

  • Enable gii: Un-comment the 'gii' section in the 'modules' array.
  • Enable clean urls: Un-comment the 'urlManager' section in the 'components' array.
  • Set up the database connection: Update the 'db' section with the MySQL credentials created in step 9.
  • Set up the admin email contact: Update the 'adminEmail' parameter in the 'params' section.

And that's it. Once you've done this a bunch of times you can get it done in just few minutes. Of course, this gives you the fundamentals of a framework, but beyond that it doesn't give you a whole heck of a lot. Most of the time you'll want to install a user framework as well, which I'll discuss in a later post.

Category:
Tags:

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options