If you are using a version control system such as Git, for your project's code and pushing releases into remote repository, you can use Git to deploy code to your production server via the git pull
shell command instead of uploading files manually. Also, you can write your own shell script to pull new repository commits, update vendors, apply migrations, and do more things.
However, there are many tools available for automating the deployment process. In this recipe, we consider the tool named Deployer.
Create a new yii2-app-basic
application by using the Composer package manager, as described in the official guide at http://www.yiiframework.com/doc-2.0/guidestart-installation.html.
If you have a shared remote repository, you can use it for deployment source.
asset-plugin
too:global require 'fxp/composer-asset-plugin:~1.1.1'
ssh-keygen
.~/.ssh/id_rsa.pub
file content into deployment the SSH keys page of your repository settings on GitHub, Bitbucket, or other repositories storage.git clone [email protected]:user/repo.git
deployer.phar
globally on your local host:sudo wget http://deployer.org/deployer.phar sudo mv deployer.phar /usr/local/bin/dep sudo chmod +x /usr/local/bin/dep
deploy.php
file with the deployment configuration:<?php require 'recipe/yii2-app-basic.php'; set('shared_files', [ 'config/db.php', 'config/params.php', 'web/index.php', 'yii', ]); server('prod', 'site.com', 22) // SSH access to remote server ->user('user') // ->password(password) // uncomment for authentication by password // ->identityFile() // uncomment for authentication by SSH key ->stage('production') ->env('deploy_path', '/var/www/project'); set('repository', '[email protected]:user/repo.git');
dep deploy:prepare prod
/var/www/project
directory. It has two subdirectories after the initialization:project ├── releases └── shared
shared
directory like this:project ├── releases └── shared ├── config │ ├── db.php │ └── params.php ├── web │ └── index.php └── yii
The Deployer tool will include these files in every release subdirectory via symbolic links.
Specify your private configuration in share/config/db.php
:
<?php return [ 'class' => 'yiidbConnection', dsn' => 'mysql:host=localhost;dbname=catalog', 'username' => 'root', 'password' => 'root', 'charset' => 'utf8', ];
Also, specify it in share/config/params.php
:
<?php return [ 'adminEmail' => '[email protected]', ];
Set the content of share/web/index.php
:
<?php defined('YII_DEBUG') or define('YII_DEBUG', false); defined('YII_ENV') or define('YII_ENV', 'prod'); $dir = dirname($_SERVER['SCRIPT_FILENAME']); require($dir . '/../vendor/autoload.php'); require($dir . '/../vendor/yiisoft/yii2/Yii.php'); $config = require($dir . '/../config/web.php'); (new yiiwebApplication($config))->run();
Also, set the content of the share/yii
file:
#!/usr/bin/env php <?php defined('YII_DEBUG') or define('YII_DEBUG', false); defined('YII_ENV') or define('YII_ENV', 'prod'); $dir = dirname($_SERVER['SCRIPT_FILENAME']); require($dir . '/vendor/autoload.php'); require($dir . '/vendor/yiisoft/yii2/Yii.php'); $config = require($dir. '/config/console.php'); $application = new yiiconsoleApplication($config); $exitCode = $application->run(); exit($exitCode);
Note: We deliberately use the dirname($_SERVER['SCRIPT_FILENAME'])
code instead of the original __DIR__
constant because __DIR__
will return incorrect value when the file is included via symbolic link.
Note: If you use the yii2-app-advanced
template you can redeclare only the config/main-local.php
and config/params-local.php
files of every (backend, frontend, console, and common) because web/index.php
and yii
files will be created automatically by the init
command.
deploy.php
file and run the deploy command:dep deploy prod
current
directory to the current release:project ├── current -> releases/20160412140556 ├── releases │ └── 20160412140556 │ ├── ... │ ├── runtime -> /../../shared/runtime │ ├── web │ ├── vendor │ ├── ... │ └── yii -> /../../shared/yii └── shared ├── config │ ├── db.php │ └── params.php ├── runtime ├── web │ └── index.php └── yii
DocumentRoot
of your server in project/current/web
directory.dep rollback prod
The current
directory will lead to your previous release files.
Most of the deployment tools do the same tasks:
The Deployer tool has predefined recipes for popular frameworks. You can extend any existing recipe or write a new one for your specific case.
18.221.222.47