- Composer
- How we use Deployer recipes
- Create a deploy.php file
- Overriding recipe defaults
- Remove config you don't use
- Deployer recipes
- Configuration
- Hosts
- Deployment tasks
Install via Composer in your project:
composer require studio24/deployer-recipes:^2.0 --dev
Recipes are a set of tasks and settings to help us deploy a website. We create custom recipes for Studio 24 usage, some of which are based on standard Deployer recipes.
Your deploy.php
file will load one or more recipes to help with deployment tasks.
If you need to see what a recipe does, you can view the source code.
If an example deploy file doesn't exist for your platform you can add one to the deployer-recipes repo.
The deploy.php
file is used to store configuration for deployment. To get started copy an example file to your project to help start you off.
You only need to copy one of these.
For deploying Craft CMS websites.
cp vendor/studio24/deployer-recipes/examples/craft-cms.php ./deploy.php
For deploying WordPress websites.
cp vendor/studio24/deployer-recipes/examples/wordpress.php ./deploy.php
For deploying Laravel web apps.
cp vendor/studio24/deployer-recipes/examples/laravel.php ./deploy.php
For deploying Symfony web apps.
cp vendor/studio24/deployer-recipes/examples/symfony.php ./deploy.php
If none of the above work, try this!
cp vendor/studio24/deployer-recipes/examples/default.php ./deploy.php
Many recipes contain predefined settings for shared_files
, shared_dirs
, writable_dirs
, remote_user
, http_user
, and webroot
.
You can view these settings in the source code for the recipe.
Please note we have some common settings in recipe/common.php (e.g. http_user, remote_user).
To add a setting to your deploy.php
file, you can use the add() function. This will add to the predefined settings.
To completely replace the settings in your deploy.php
file, you can use the set() function. This will replace the predefined settings.
Please make sure you remove any config that is not used for your project.
For example, the example deploy scripts contain an PHP-FPM reload command and we set the http_user
for each host if we are using PHP-FPM. If you don't use this remove this.
Your deploy.php
file will load one or more recipes to help with deployment tasks.
You can add other recipes for specific tasks.
Reload PHP-FPM after a deployment.
require 'contrib/php-fpm.php';
// PHP-FPM reload
after('deploy', 'php-fpm:reload');
Please note the deploy
user needs sudo permissions to run the PHP-FPM reload command.
This needs to be setup via Ansible for the hosting platform.
deploy ALL=(ALL) NOPASSWD:/usr/bin/systemctl reload php*-fpm
Add the Slack recipe to send notifications to a Slack channel on deployment.
Configuration variables are set up using the set()
function. You can add values to an array with the add()
function.
You'll need to edit these settings:
application
- your application namerepository
- GitHub repo URL, please ensure this uses the SSH URL, e.g. git@github.com:studio24/project-name.gitdisk_space_filesystem
- the filesystem volume to check disk space when deploying
Settings that you shouldn't need to change:
remote_user
- remote user to login via SSH to deploy fileswebroot
- document root for the website, usually web or public
The deployment process checks disk space and warns on low capacity.
To enable the warning check you need to set disk_space_filesystem
:
# AWS
set('disk_space_filesystem', '/data');
# Mythic Beasts
set('disk_space_filesystem', '/');
The check warns when over 80% disk space is used. You can change this with disk_space_threshold
:
set('disk_space_threshold', 90);
The shared_files
setting defines shared files that need to persist between deployments. These should be excluded from git.
Examples include a local .env
config file.
The shared_dirs
setting defines shared folders that need to persist between deployments. These should be excluded from git,
or optionally you can include the folder in git but exclude the contents.
Examples include storage folder for logs, or image upload folders.
The writable_dirs
setting defines folders that the webserver needs to write to.
Examples include storage folder for logs, or image upload folders.
If you want to make sure a folder exists add it to writable_dirs
. For example, you may set a shared
folder to
be shared and writable and specify some sub-folders you want to exist. E.g.
set('shared_dirs', [
'storage',
]);
set ('writeable_dirs', [
'storage',
'storage/backups',
'storage/logs',
]);
If you need to be able to copy files from the remote server to your local Mac, then you can set
this up via sync
.
The format is:
set('sync', [
'name' => [
'remote/folder/path/' => 'local/folder/path'
],
]);
See sync documentation.
You can use a .env
file to store sensitive information. This is loaded in the deploy.php file via:
set('dotenv', '{{current_path}}/.env');
You can then use environment variables in your deploy.php file.
For example, if you have the environment variable API_KEY
in your .env
file:
API_KEY=abc123
You can reference this in your deploy.php file like so:
getenv('API_KEY');
The host settings to enable Deployer to publish files over SSH. You will need to edit these.
The format of the host settings is:
host('[environment name]')
->set('hostname', '[ip address]')
->set('http_user', '[php_fpm_user]')
->set('deploy_path', '/path/to/project_root')
->set('log_files', [
'/var/log/apache2/DOMAIN.co.uk.access.log',
'/var/log/apache2/DOMAIN.co.uk.error.log',
])
->set('url', '[website url]');
An example:
host('production')
->set('hostname', '1.2.3.4')
->set('http_user', 'production')
->set('deploy_path', '/var/www/vhosts/studio24.net/production')
->set('log_files', [
'storage/logs/*.log',
'/var/log/apache2/studio24.net.access.log',
'/var/log/apache2/studio24.net.error.log',
])
->set('url', 'https://www.studio24.net');
The key information you need to set is:
host('[environment name]')
- Environment namehostname
- Hostname or IP address to deploy tohttp_user
- The PHP-FPM user (not required if you are not using PHP-FPM)deploy_path
- Path to deploy files tolog_files
- Array of log files (optional)url
- The URL of the website
You can set log_files to one file, multiple files, or use * to pattern match valid log files (e.g. storage/logs/*.log
).
See hosts documentation.
You can add any custom deployment tasks here. Hopefully the common tasks required to deploy your project will be taken care of by the recipe you are using.
We recommend leaving the default tasks as defined in the Deployer recipe and using the before() and after() functions to add other tasks to run at certain points in the deployment process.
E.g.
task('my-custom-task', function () {
// do something
});
after('deploy:success', 'my-custom-task');
You can see the different deployment steps by passing the --plan
option to your deploy command.
Also see Deplopyer common tasks.
If you call the before() or after() function multiple times for the same task, then it will override previous calls. To run multiple tasks just create a custom task which you can pass an array of tasks to run. E.g.
task('post-deploy-tasks', [
'slack:notify:success',
'my-custom-task'
]);
after('deploy:success', 'post-deploy-tasks');
If you need to run Composer in a sub-path, e.g. a WordPress plugin, add the following configuration:
// Custom (non-root) composer installs
set('composer_paths', [
'web/wp-content/plugins/s24-wp-image-optimiser'
]);
And add this task to run:
// Custom (non-root) composer installs
after('deploy:prepare', 'vendors-subpath');
Please note it is recommended to only use Composer in the project root folder to avoid clashes between package versions.