How to deploy a PHP web application with confidence?
posted on 24 Jan 2020A long time ago, the PHP web applications were distributed throughout via FTP from local computer to remote server. This way may be fast and the result of our work is immediately.
BUT…There are big BUT.
- The process of “Deploying” hasn’t feedback and test or build aren’t executed before deploy the application so we can’t be sure that web application has no error to production mode.
- No backup (only manually backup, if you remember do it) and no easy rollback to previously version are available.
The tool that I’m here to suggest, is called Deployer (it was written in php).
What is Deployer
Deployer manage and automate a new release distribution of the our web application.
As write before, it was written in php and it is possibile to install throught composer:
composer install deployer/deployer
and then, type instruction below to initialize the config file (main entry) of deployer:
./vendor/bin/deployer init
Usually (but not always) the config file is located in the root dir of the project and it called deploy.php
Folder structure
- root-server-dir
- .dep
- releases
- 1
- 2
- …
- shared
- current
- .dep is a hidden folder, where deployer store the releases tracking and it creates inside it a .lock file which it allow one deploy at a time.
- releases is the folder where deployer store up to five releases in case we have to rollback to previous release because an error occured in the last release
- current is a symbolic link to the last release
- shared is the folder which manage the files or folders which have override (usually these file or folder aren’t versioned by GIT)
The config file can contains many standard recipes, custom ones or override existing others
The tasks are the core of the deployer. They are a php function that make a specific work and they are execute in the defined order.
example:
task('task-name', function () {
run('echo "Hello world!"');
});
There is a way to define a context variable trhought a set function as below:
set('variableName', 'value');
And then we can use them:
If the variable already exists in the standard recipe that on we are basing to, and we re-defined it, this vartiable will be override with the new value
example. Below we re-defined the repository where our application is versioned and it will be cloned from deployer:
set('repository', 'git@bitbucket.org:qweb_srl/foo-repo.git');
others important variables are:
- shared_files
- shared_folders
respectively they represents:
- The files that haven’t to override
- The folders that haven’t to override
These files or folder won’t override from other version of them and they will store inside shared folder.
Hosts
Another importa function in deployer is host(). It defines where the application will be deployed. We can define one or more hosts, if the web app is shared from more customers or environments (production, staging etc.)
host('staging')
->hostname('demo.foo.it')
->user('demofoo')
->port('1234')
->set('deploy_path', '~/')
->set('branch', 'staging');
host('prod')
->hostname('prod')
->user('foo')
->port('1234')
->set('deploy_path', '~/')
->set('branch', 'master');
Recipes
There are many availables recipes that contain the default tasks for common CMS, tools or frameworks like Symfony, Laravel, Magento, Wordpress, Zend, Prestashop etc. As described above these default task can be overriden from us in the main config file deploy.php.
BEFORE THIS DO THAT
We can also, specify a task which it must be execute before other one with before() deployer function like below:
before('task-name', 'other-task-name');
AFTER THIS DO THAT
Similarly to above we can define the tasks that are performed after another task:
after('task-name', 'other-task-name');