We hope you find this tutorial helpful. In addition to guides like this one, we provide simple cloud infrastructure for developers. Learn more →

How To Install and Setup Yii PHP Framework on Ubuntu 12.04

PostedAugust 2, 2013 96.7k views PHP Frameworks PHP Ubuntu

Status: Deprecated

This article covers a version of Ubuntu that is no longer supported. If you are currently operate a server running Ubuntu 12.04, we highly recommend upgrading or migrating to a supported version of Ubuntu:

Reason: Ubuntu 12.04 reached end of life (EOL) on April 28, 2017 and no longer receives security patches or updates. This guide is no longer maintained.

See Instead:
This guide might still be useful as a reference, but may not work on other Ubuntu releases. If available, we strongly recommend using a guide written for the version of Ubuntu you are using. You can use the search functionality at the top of the page to find a more recent version.

About Yii

Yii is a highly performant PHP Object Oriented framework that helps you build web applications quickly. The way it is designed lets you focus on what makes your application unique while it takes care of the more "boring" code definition tasks. It follows the MVC pattern for a clean separation of logic from presentation and it features quite a lot of security enforcing functionality.

Yii gives a great boost to your application by letting you generate automatically a skeleton for it, Models for your database and even CRUD (create, read, update, delete) related code. All you have to do then is customize everything to your liking without having to write the basics any application would require. So let’s see how to install it and get started developing with Yii (Yes, it is!)

This tutorial assumes you are already running an Ubuntu VPS (but other Linux distributions will also work) with LAMP installed on it. If you you can follow the steps in these tutorials to get you ready:


To install Yii, you need to get the latest version and extract it somewhere in your web server’s web root (probably /var/www if you are running Apache). So let’s copy the link to the zip file of the latest release from http://www.yiiframework.com/download/ and run the following command to download it to our VPS:

wget http://yii.googlecode.com/files/yii-1.1.13.e9e4a0.zip

Make sure you replace this link with the one of the latest release at the time of your installation. Next, run the following command to unzip the framework:

unzip yii-1.1.13.e9e4a0.zip

Make sure you replace the file name with the one you just downloaded. If you don’t have Unzip installed on your system, run the following command before trying to unzip the file:

sudo apt-get install unzip

After you successfully unzipped the framework, go ahead and rename the folder to something more appropriate for you. Let’s say we name it

mv yii-1.1.13.e9e4a0 yii_framework

Make sure you replace the folder name with the name of the extracted folder for your case.


Yii requires your VPS to have PHP 5.1 or above installed, so let’s use the Yii requirement checker to see where we stand. Point your browser to the following url to display the checker:


Make sure you replace as necessary here to point to the respective file in the Yii framework. Now you should see the Requirement Checker page on which, if all goes well, the conclusion should be something like: "Your server configuration satisfies the minimum requirements by Yii." In this case, you can move on.

Creating your First Application

Yii has a very powerful functionality to automatically generate stuff for you. The skeleton of your application is one such thing, so let’s see how to do that. Navigate to where you would like the application to be generated. This can be also outside of the folder where you extracted the Yii framework. So let’s go back to the server web root and do it there:

cd /var/www

Decide on the name of an application folder (let’s say mysite) and run the following command:

php yii_framework/framework/yiic webapp mysite

Make sure the path corresponds to your environment. Select Yes when you are prompted and now you have your new application under /var/www/mysite.

To test it out, point your browser to that folder:


You should now see the homepage of your skeleton application. You have a couple of pages already created as well as a contact form and login/logout functionality. You can login with the following credentials:

  • username: admin
  • password: admin

This is functionality you don’t need to create yourself.


Since we are using LAMP, we don’t have much to configure. You’ll probably want to remove the index.php from the the URL structure though. To do this, create an .htaccess file in the root folder of the application (mysite) and paste in the following code:

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

Save the file and exit.

Note: To use the .htaccess functionality in Apache, you'll need mod_rewrite enabled on your VPS. To check for this, run the following command:

apache2ctl -M

If you see rewrite_module in the list, you are good to go. If not, enable it with the following command:

a2enmod rewrite

Then, restart Apache for the changes to take effect:

sudo service apache2 restart

Additionally, ensure that your .htaccess file is enabled by setting AllowOverride to All in the virtual hosts file:

          Options Indexes FollowSymLinks MultiViews
          AllowOverride All
          Order allow,deny
          allow from all

Now you need to also tell the application you no longer want the index.php in the URL. Open the main.php file found in the protected/config folder and uncomment the following block:


Next, add the following to the urlManager array:

'showScriptName' => false

The block should now look something like this:

  'showScriptName' => false,

Save the file and exit. Now if you navigate to your new application, you can go through the pages seeing cleaner URLs and no index.php in the structure.

The skeleton application is by default connected to a SQLite database that comes with the application, but also contains a commented out template for connecting to a MySQL database. So what we need to do is to comment out the first one and uncomment the latter one as well as specify the information related to our own database (that we will create in the next tutorial). To do this, open again the main.php file located in the protected/config folder of your application and comment out the following block:

  'connectionString' => 'sqlite:'.dirname(__FILE__).'/../data/testdrive.db',

And remove the commenting around the following block:

  'connectionString' => 'mysql:host=localhost;dbname=testdrive',
  'emulatePrepare' => true,
  'username' => 'root',
  'password' => '',
  'charset' => 'utf8',

Here, we can now add the connection information to the database we will create. We will give it a name (db_tutorial) and specify the username and password to access it. You can also now create the empty database with this name and fill everything up before we continue this tutorial in part 2.

Article Submitted by: Danny


Creative Commons License