Installation and configuration of Yii2 are described in the official manual, as well as many articles have been published, but I did not find the manual that would help me install and configure this framework from beginning to end. During installation, I ran into some questions, the answers to which were in different places on the Internet. After long dances with tambourines, I set up Yii2 as I wanted. I will describe my tuning experience in this article, in the hope that for some it will shorten the time of dancing and make life easier.
1. Install Composer
Since I have little experience with setting up a server and working in the Linux / Unix console, the first problem appeared already when installing Composer. There are several versions of PHP installed on the server, and by default the console commands execute PHP version 5.2.17. And Composer requires PHP version 5.3.2 and higher. The solution was:
php -r "readfile('https://getcomposer.org/installer');" | php5.6
Server settings do not make it possible to make Composer executable and run directly, so we run it through the PHP interpreter, while not forgetting to specify the PHP version:
php5.6 composer.phar install
The first step is completed - Composer is installed.
2. Installing Yii2
First of all,
according to the instructions : install the composer asset plugin, which manages dependencies of the bower and npm packages.
php5.6 composer.phar global require "fxp/composer-asset-plugin:~1.1.1"
This needs to be done only once, in the future you can install Yii2 several times using the commands described below.
Now choose which version of the Yii2 template to install: Basic or Advanced. Basic is a simple application, and in Advanced the division into public and administrative parts (frontend and backend) is implemented and a ready-made model (and database table) user is created.
2.1. Install Yii2 Basic
First, consider the installation of Basic, because it is simpler, it is installed using the command:
php5.6 composer.phar create-project --prefer-dist yiisoft/yii2-app-basic mysite
Where
mysite
is the directory in which you need to install Yii2, it must be empty (if the
public_html
subdirectory was automatically created when creating the directory for the site on the server, you must delete it). Composer may request a login password from Github (due to the limit on the number of requests from Github).
')
A small lyrical digression about the directory structure on the server and in the framework. The considered virtual hosting is configured so that the files of the sites are located in the directory
mysite/public_html
. Files in the
public_html
directory are accessible from the web.
The structure of the Yii2 Basic template includes several files and directories that are recommended to be made inaccessible from the web in order to “protect code and data from unwanted access”, as well as the
basic/web
directory which is intended to be the root directory of the web server. For the required structure, create a "symbolic" link:
cd mysite ln -s web public_html
Now the contents of the
basic/web
directory are our site, and other framework files are inaccessible from the network.
Yii2 Basic is installed, you can open the site in the browser.
2.1.1. Setup Yii2 Basic
Open the
config/db.php
and change the settings to the correct ones for your database. After
'charset' => 'utf8',
you can specify a table prefix like this:
'tablePrefix' => 'myprefix_'
(if several sites use the same database).
Next, set up links, by default, the routing is
/index.php?r=site%2Fabout
, translate it to this form
/site/about
. To do this, create a file
web/.htaccess
with the contents as recommended in the
office. guide :
Options +FollowSymLinks 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
Now the web server knows that requests must be sent to index.php, and there is no need to specify index.php in the URL. Further in the
config/web.php
we add (or uncomment) to
$config = ['components']
after
'db' => require(__DIR__ . '/db.php'),
following lines
'urlManager' => [ 'enablePrettyUrl' => true, 'showScriptName' => false, 'rules' => [ ], ],
enablePrettyUrl - generate beautiful links, showScriptName - do not write the name of the script index.php in the link. Now you can go to our website and see that it already uses beautiful links like
/site/about
.
Ok, with Yii2 Basic sorted out, now go to Advanced.
2.2. Installation Yii2 Advanced
Starting installation is similar to Basic:
php5.6 composer.phar create-project --prefer-dist yiisoft/yii2-app-advanced mysite
Yii2 is installed, files and directories of Yii2 appeared in its mysite directory, including the frontend for the public part of the site and the backend for the admin. However, in frontend / web there is no index.php file, therefore, we next execute the init command to initialize the template and create a symbolic link for the frontend:
cd mysite php5.6 init ln -s frontend/web public_html
Now the frontend is available at the main address of the site. Next, configure the backend:
cd frontend/web ln -s ../../backend/web admin
Now the backend is available at
/admin
.
We configure beautiful addresses in the frontend:
- Create in
/frontend/web
the same .htaccess
as in the Basic template - In the config /
/frontend/config/main.php
config/ /frontend/config/main.php
prescribe the same
We perform similar operations for the backend. Along the way, we note that there is no
db.php
file in the
config
directory.
The parameters for accessing the database in Advanced are specified in
common/config/main-local.php
, where we also write there as in
db.php
in Basic. Then we apply migration:
php5.6 yii migrate
The migration and user tables are created in the database.
Hooray! Yii2 is installed, configured and ready to go.
Sources used
Introduction - ComposerThe Definitive Guide to Yii 2.0 (Yii2 official guide in English)The complete guide to Yii 2.0 (Russian translation)Installation Yii2 AdvancedHow To Install The Advanced Template In Yii2