Installing Yii2 (Basic & Advanced Templates)

Yii2 provides two application templates: Basic and Advanced. The Basic Template is suitable for small to medium projects, while the Advanced Template is designed for large-scale applications with multiple environments.

In this tutorial, we will cover:

  1. Prerequisites for Yii2 Installation
  2. Installing Yii2 Basic Template
  3. Installing Yii2 Advanced Template
  4. Understanding the Directory Structure
  5. Running the Yii2 Application

1. Prerequisites for Yii2 Installation

Before installing Yii2, ensure your system meets the following requirements:

  • PHP 7.4 or later (Yii2 supports PHP 8)
  • Composer (Dependency Manager for PHP)
  • Web Server (Apache, Nginx, or built-in PHP server)
  • Database (MySQL, MariaDB, PostgreSQL, or SQLite)

To check if PHP and Composer are installed, run:

php -v
composer -V

If Composer is not installed, download it from getcomposer.org.


2. Installing Yii2 Basic Template

The Basic Template is the recommended starting point for most projects.

Step 1: Install Yii2 via Composer

Run the following command to create a Yii2 Basic project:

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

This will install Yii2 Basic in a folder named dynamic-duniya-basic.

Step 2: Navigate to the Project Folder

cd dynamic-duniya-basic

Step 3: Start the Built-in PHP Server

php yii serve

Now, open your browser and visit:

http://localhost:8080

You should see the Yii2 welcome page.

Step 4: Configure the Database

In the Basic Template, configure the database in:

// config/db.php
return [
    'class' => 'yii\db\Connection',
    'dsn' => 'mysql:host=localhost;dbname=yii2basic',
    'username' => 'root',
    'password' => '',
    'charset' => 'utf8',
];

 


3. Installing Yii2 Advanced Template

The Advanced Template is designed for enterprise-level applications and provides separate environments for frontend, backend, and console applications.

Step 1: Install Yii2 Advanced Template

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

Step 2: Initialize the Project

Yii2 Advanced requires an initialization step:

cd dynamic-duniya-advanced
php init

You will be asked to choose an environment:

Which environment do you want the application to be initialized in?
  [0] Development
  [1] Production

Select 0 for development or 1 for production.

Step 3: Configure the Database

Edit the database configuration file:

// common/config/main-local.php
return [
    'components' => [
        'db' => [
            'class' => 'yii\db\Connection',
            'dsn' => 'mysql:host=localhost;dbname=yii2advanced',
            'username' => 'root',
            'password' => '',
            'charset' => 'utf8',
        ],
    ],
];

Now, run the migration command to set up the database schema:

php yii migrate

Step 4: Start the Application

Run the PHP server for both frontend and backend:

php yii serve --docroot="frontend/web"
php yii serve --docroot="backend/web"

Visit http://localhost:8080 for the frontend and http://localhost:8080/admin for the backend.


4. Understanding the Directory Structure

Basic Template Directory Structure

dynamic-duniya-basic/
│── assets/        # Compiled CSS & JS files
│── commands/      # Custom console commands
│── config/        # Configuration files
│── controllers/   # Application controllers
│── models/        # Business logic and database models
│── runtime/       # Temporary files (cache, logs)
│── views/         # Application views (HTML)
│── web/           # Web-accessible root directory

Advanced Template Directory Structure

dynamic-duniya-advanced/
│── backend/       # Backend application
│── frontend/      # Frontend application
│── common/        # Shared code between frontend & backend
│── console/       # Console commands & scripts
│── environments/  # Config for different environments
│── vendor/        # Installed dependencies

The Advanced Template is ideal when you need separate frontend and backend applications.


5. Running the Yii2 Application

Using Built-in PHP Server

For the Basic Template:

php yii serve

For the Advanced Template (Frontend)

php yii serve --docroot="frontend/web"

For the Advanced Template (Backend):

php yii serve --docroot="backend/web"

You Can Start Development But i Recommend you to Create Virtual host for Basic or Advanced template whatever you choose. 

Using Apache or Nginx

If using Apache or Nginx, point the document root to the web/ directory.


Learn How to Create Virtual Host for Nginx on Ubuntu (For Yii2 Basic & Advanced Templates)