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

It is giving me error

It is giving me error

"yii/framework/yiic webapp webapp"

Can you be more specific?

What is the error you are receiving? From what directory are you attempting to run this command?

resolved this by with ./yiic

resolved this by with ./yiic instead of just yiic

Error it gives me

Error 403
You are not allowed to access this page.

How do I access the forms gii creates?

access control?

Do you have some kind of access control in place, such as the Rights module? If so, you will need to explicitly grant permission for a user to see this page.

Redirect menus?

Hi!

Thanks for this tutorial. I'm new with Yii and I'm learning. I have a question, Can I use this method for redirecting different pages according to user level? I mean...if a user is logged as Admin, for instance, can I redirect him/her to a page with different menus, content and "rights" than a normal user? I want an Admin to edit, delete and create users and employees but a normal user would only view and print information.

Thanks in advance!!

PS: I apologize if there's any grammar mistake. English is my second language.

User & Rights module

Hi there,

Yes indeed, you implement a few add-modules to get what you need. Check out this post for some more specific directions: http://www.benjaminlhaas.com/blog/installing-yii-users-and-rights-5-steps

Let me know how it goes for you!

yii main folder recive nd

yii main folder recive nd three folder(framework, demos, requirements) than wat i do..

Note: Before using Yii, PHP

Note:
Before using Yii, PHP should be in running mode from command prompt. Add path like "C:\wamp\bin\php\php5.3.4;" to env variable in the system on windows

Step 1: Download Yii 1.1.14
Step 2: The folders will be
- demos
- framework
- requirements

Step 3: Remove or comment line "Deny from All" in .htaccess within framework folder if you want to create your project within Framework folder. Otherwise, no need to work in htacess.

Step 4: Go to framework from cmd

Step 5: Create Skeleton of the project say testdrive

C:\wamp\www\yii\framework>yiic webapp ../testdrive
Create a Web application under 'C:\wamp\www\yii\testdrive'? (yes|no) [no]:yes
mkdir C:/wamp/www/yii/testdrive2
mkdir C:/wamp/www/yii/testdrive2/assets
mkdir C:/wamp/www/yii/testdrive2/css
generate css/bg.gif
........

Step 6: Go to Yii/testdrive - will contain the below folder
- assets
- css
- images
- protected
- themes

C:\xampp\yii\framework>yiic

C:\xampp\yii\framework>yiic webapp ../testdrive
"" was unexpected at this time.

Switched to nginx

Hi,

Thanks for sharing this article. I've found it helpful for setting up Yii with Apache multiple times but now I switched over to Nginx, so...

...it looks like Yii runs great and actually a bit faster on Nginx, so I thought I would post the article I used to setup Yii on my SSD VPS as I believe many will find it useful.

Thanks and take care

- Chris

Error 403...

I received: 403 error You are not allowed to access this page.

A friend send me this : https://www.rosehosting.com/blog/how-to-install-yii-framework-on-a-debia...

But i have a Centos server and i wasn't sure if i should use the tutorial above.

Can you please help?

Thank you.

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

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.