Thứ Ba, 22 tháng 9, 2015

Installing Yii2 framework on Ubuntu 14.04

Preferred way of installing Yii2 framework is through Composer. So we will have to install Composer, but in order to install composer, first we will need to install some packages that Composer and Yii2 need in order to work on our system. Don't worry I will show you everything you need to do. Most of our work will be done in terminal. I will give you all commands, you just have to copy and paste them in your terminal.

So let's get started. Open up your terminal, and lets install first two packages you will need in order to install composer.

First let's install curl. Here is the command that you need to paste in your terminal and then press enter to execute it ( you will have to enter your password after ):

sudo apt-get install curl

Now let's install php5-cli package, so you can execute php scripts from your terminal:

sudo apt-get install php5-cli

You will be asked to confirm installation, type "yes" and press enter to execute.

Now we will install the php5-mysql package, because we will need it to be able to work with yii2 migrations. Execute this command:

sudo apt-get install php5-mysql

Now let's install and activate php5-mcrypt package. First we will install php5-mcrypt package with this command:

sudo apt-get install php5-mcrypt

And to activate it paste this command and press enter:

sudo php5enmod mcrypt

Let's install php5-gd package too, we will need it when we run functional tests, and on some system setups to be able to see Captcha widget.
 
sudo apt-get install php5-gd

These five packages will stay on your system. This means that you will not need to repeat this process every time you want to install Yii2.
Now we are ready to install Composer, and we will install it globally. First, let's download the composer. Copy-paste this command and execute it:

curl -sS https://getcomposer.org/installer | php

Now let's install it globally:

sudo mv composer.phar /usr/local/bin/composer

Now every time you need to use composer you just write "composer 'some command' " in your terminal.

Let's use Composer to install Composer asset plugin. "The Composer Asset Plugin allows you to manage project assets (css, js, etc.) in your composer.json without installing NPM or Bower." Copy -> paste the following command to your terminal, and execute it.

composer global require "fxp/composer-asset-plugin:1.0.0"

During installation of this package, you may be asked to enter your GitHub username and password. If you do not have it, just create account there, it is free.

Finally, we are ready to install yii2 basic and advanced templates. Move to your localhost web root folder. If you are using lampp you can move with this command :

cd /opt/lampp/htdocs/


To install basic template execute this command:

composer create-project --prefer-dist yiisoft/yii2-app-basic

As you can see we are using composer to download yii2 basic application. After the download is complete you can rename the downloaded folder to something you like, I will be generic and rename it to "basic".
Now if you start your lampp, you can start your basic Yii2 application by simply visiting this URL in your browser:

localhost/basic/web/

Now let's install advanced template. The process of installing advanced template is slightly more complex, but do not worry. Move to your htdocs folder again
and execute this command to install Yii2 advanced template there:

composer create-project --prefer-dist yiisoft/yii2-app-advanced advanced

This time I have renamed the yii2-app-advanced to "advanced" inside my composer create-project command.
After download is complete we will have to initialize our application, create database and run yii migration to populate that database with boilerplate tables.
To initialize your application, first move inside the "advanced" folder. In your terminal execute this command:

cd advanced

Now run the php init file with this command:

php init

You will be asked to chose environment. Chose Development ( type "0" and press enter ) and then you will have to confirm your choice by typing "yes" and press enter again.
Now create database that you will use for this application. You can use any tool you like, for example "phpMyAdmin". Give it any name you want. Now we have to tell Yii to use this database. In your IDE or text editor open up the "main-local.php" configuration file. Path to this file is:

common/config/main-local.php

Inside this file you can set up your database credentials. Write your database name, username and password and save the file.
Now you can execute the yii migration that will populate your database with necessary tables. Inside advanced folder execute this command:

./yii migrate

Confirm migration by typing "yes" and press enter.
Now you are ready to start your application and see what you got. Advanced template is consisting from front-end and back-end sides. To start each of them, you can use these URLs:

localhost/advanced/frontend/web

and

localhost/advanced/backend/web

NOTE: if you want to use .htaccess to remove index.php entry script from your URLs, you may need to enable mod_rewrite and AllowOverride All in your Apache configuration.


You can read Linux guide here, see sections 1 and 2.

Thanks for reading this tutorial. I hope it was useful to you.


EmoticonEmoticon