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.
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/
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
yii_framework:
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:
http://example.com/yii_framework/requirements/index. php
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.
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:
http://example.com/mysite/index.php
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:
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:
/* 'urlManager'=>array( 'urlFormat'=>'path', 'rules'=>array( '/'=>'/view', '//'=>'/', '/'=>'/', ), ), */
Next, add the following to the urlManager array:
'showScriptName' => false
The block should now look something like this:
'urlManager'=>array( 'urlFormat'=>'path', 'showScriptName' => false, 'rules'=>array( '/'=>'/view', '//'=>'/', '/'=>'/', ), ),
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:
'db'=>array( 'connectionString' => 'sqlite:'.dirname(__FILE__).'/../data/testdrive.db', ),
And remove the commenting around the following block:
/* 'db'=>array( '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.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
I would recommend to use nginx+php-fpm instead of apache with modphp. Easy to configure and no need in htaccess file. Provide far better productivity (tests at work showed approximately 4 times better results).
Good One ! Keep it up man … :)
great tutorial,
can you write a guide to use YII Framework on LEMP,
Thank you
I think the latest Yii download is now at: https://github.com/yiisoft/yii/releases/download/1.1.14/yii-1.1.14.f0fee9.zip
thank you admin with your help i just finished this… its realy works very nice …
im not able to create my first application but ya has finished installation of yii,… but just haveing problem to create my frist application thanxx kamal nasser…
@viv.parashar93: Which part isn’t working for you?
i cant work with your suggetstions. haveing problem… if can help then mail me back on Viv.parashar93@hotmail.com
Just one word - Thanks!
Thanks a lot…All steps are working…It is really save my time…