
Install and configure Yii2 on shared hosting
Installation and configuration of Yii2 are described in the official guide, and many articles have been published, but I did not find the guide that would help me install and configure this framework from beginning to end. During the installation, I came across 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 this will reduce the time for dancing and simplify life for someone.
Since I have little experience configuring the server and working in the Linux / Unix console, the first problem appeared already when installing Composer. Several versions of PHP are installed on the server, and by default console commands are executed by PHP version 5.2.17. And Composer requires PHP version 5.3.2 and higher. The solution was this:
Server settings do not make it possible to make Composer executable and run directly, so we launch it through the PHP interpreter, while not forgetting to specify the PHP version:
The first step is completed - Composer is installed.
First, follow the instructions : install the composer asset plugin, which manages the dependencies of the bower and npm packages.
You only need to do this 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, while Advanced implements the separation of the public and administrative parts (frontend and backend) and creates a ready-made model (and database table) user.
First, consider the installation of Basic, as it is simpler, installation is performed using the command:
Where
A small lyrical digression about the directory structure on the server and in the framework. The virtual hosting in question is configured so that the site files are located in a directory
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 a directory
Now the contents of the catalog
Yii2 Basic is installed, you can open the site in a browser.
Open the file
Next, configure the links, by default, the routing has the form
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. Next, in the file,
enablePrettyUrl - generate beautiful links, showScriptName - do not write the index.php script name in the link. Now you can go to our site and see that it already uses beautiful type links
Ok, we’ve sorted it out with Yii2 Basic, now let's move on to Advanced.
The start of installation is similar to Basic:
Yii2 is installed, Yii2 files and directories appeared in its mysite directory, including frontend for the public part of the site and backend for the admin panel. However, there is no index.php file in frontend / web, so then we execute the init command to initialize the template and create a symbolic link for frontend:
Now the frontend is available at the main address of the site. Next, configure the backend:
The backend is now available at
We set up beautiful addresses in the front end:
We perform similar operations for the backend. Along the way, we notice that there is no file
The parameters for accessing the database in Advanced are specified in
The migration and user tables are created in the database.
Hurrah! Yii2 is installed, configured, and ready to go.
Introduction - Composer
The Definitive Guide to Yii 2.0 (official Yii2 guide in English)
Complete Yii 2.0 guide (Russian translation)
Installation Yii2 Advanced
How To Install The Advanced Template In Yii2
1. Install Composer
Since I have little experience configuring the server and working in the Linux / Unix console, the first problem appeared already when installing Composer. Several versions of PHP are installed on the server, and by default console commands are executed by PHP version 5.2.17. And Composer requires PHP version 5.3.2 and higher. The solution was this:
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 launch 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, follow the instructions : install the composer asset plugin, which manages the dependencies of the bower and npm packages.
php5.6 composer.phar global require "fxp/composer-asset-plugin:~1.1.1"
You only need to do this 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, while Advanced implements the separation of the public and administrative parts (frontend and backend) and creates a ready-made model (and database table) user.
2.1. Install Yii2 Basic
First, consider the installation of Basic, as it is simpler, installation is performed using the command:
php5.6 composer.phar create-project --prefer-dist yiisoft/yii2-app-basic mysite
Where
mysite
is the directory in which you want to install Yii2, it must be empty (if a subdirectory was automatically created when creating the directory for the site on the server public_html
, it must be deleted). Composer can 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 virtual hosting in question is configured so that the site files are located in a directory
mysite/public_html
. Files in a directory public_html
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 a directory
basic/web
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 catalog
basic/web
are our site, and other framework files are not accessible from the network. Yii2 Basic is installed, you can open the site in a browser.
2.1.1. Configuring Yii2 Basic
Open the file
config/db.php
and change the settings to the correct ones for your database. After 'charset' => 'utf8',
you can specify the table prefix like this: 'tablePrefix' => 'myprefix_'
(if several sites use the same database). Next, configure the links, by default, the routing has the form
/index.php?r=site%2Fabout
, we will translate it into 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. Next, in the file,
config/web.php
add (or uncomment) the following lines $config = ['components']
after'db' => require(__DIR__ . '/db.php'),
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
],
],
enablePrettyUrl - generate beautiful links, showScriptName - do not write the index.php script name in the link. Now you can go to our site and see that it already uses beautiful type links
/site/about
. Ok, we’ve sorted it out with Yii2 Basic, now let's move on to Advanced.
2.2. Install Yii2 Advanced
The start of installation is similar to Basic:
php5.6 composer.phar create-project --prefer-dist yiisoft/yii2-app-advanced mysite
Yii2 is installed, Yii2 files and directories appeared in its mysite directory, including frontend for the public part of the site and backend for the admin panel. However, there is no index.php file in frontend / web, so then we execute the init command to initialize the template and create a symbolic link for 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
The backend is now available at
/admin
. We set up beautiful addresses in the front end:
- Create in
/frontend/web
the same.htaccess
as in the Basic template - In the config
/frontend/config/main.php
register the same
We perform similar operations for the backend. Along the way, we notice that there is no file
db.php
in the directory config
. The parameters for accessing the database in Advanced are specified in
common/config/main-local.php
, there we prescribe the same as db.php
in Basic. Then apply the migration:php5.6 yii migrate
The migration and user tables are created in the database.
Hurrah! Yii2 is installed, configured, and ready to go.
Sources used
Introduction - Composer
The Definitive Guide to Yii 2.0 (official Yii2 guide in English)
Complete Yii 2.0 guide (Russian translation)
Installation Yii2 Advanced
How To Install The Advanced Template In Yii2