Laravel Deployer Multiple Hosts

Most of us will wind up wanting a Laravel Deployer Multiple Hosts setup. You start out development on one test or staging server, but you often need another production server or more in the end. A setup with a development or staging server, a production web server and a database server for that production web server is ver common. So how do you set this up with Deployer.org’s deployer script?

Prerequisites

Laravel app should be installed on a provisioned server and you should have deployer set up on your own box. You can see documentation for both on their respective sites. For Laravel basic provisioning see Stedding.

Single Server

Here an example of a deploy.php script using a Laravel recipe to push to a single Laravel beta or development server

<?php 
namespace Deployer;

require 'recipe/laravel.php';
require 'vendor/deployer/recipes/cachetool.php';

// Configuration
set('repository', 'https://github.com/user/repo.git');
set('default_stage', 'production');
set('git_tty', true); // [Optional] Allocate tty for git on first deployment
set('ssh_type', 'native');
set('cachetool', '/var/run/php/php7.1-fpm.sock');
set('keep_releases', 10);

// Make sure uploads &amp; published aren't overwritten by deploying
set('shared_dirs', [
    'public/uploads',
    'public/published'
]);
set('writable_dirs', [
    'public/uploads',
    'public/published',
    'storage/framework/cache/data'
]);

// SMART CUSTOM DEPLOY COMMANDS
task('db:migrate', function () {
    run("cd {{release_path}} && php artisan migrate");
});

// Hosts
host('beta.domain.com')
    ->user('web')
    ->forwardAgent()
    ->stage('production')
    ->set('deploy_path', '/var/www/beta.domain.com');


// Run database migrations
after('deploy:symlink', 'db:migrate');

// Clear OPCache
after('db:migrate', 'cachetool:clear:opcache');

As you can see we have one single host where we added the user, turned on forward agent for ssh and set the stage and path. But now we want to add another host, user, stage production and a different path setup by Laravel Forge for example. How do we do this?

Hosts.yml

Well you can put all hosting details in a yaml file and refer to it in your deploy.php script. This you do using

inventory('hosts.yml');

instead of the host block in your deploy.php.The hosts.yml file you would have will look like this:

domain.com:
  stage:production
  roles:app 
  deploy_path:/home/forge/domain.com
beta.domain.com
stage: staging
 roles: app  
 deploy_path: /var/www/beta.domain.com

Roles are additional tasks you can add like test and build. For Laravel we have had no need really.

Additional Host in Deploy.php

You can also add an additional host and alias in your script using:

// Hosts 
host('beta') 
->hostname('beta.domain.com')
->user('web') 
->forwardAgent() 
->stage('staging') 
->set('deploy_path', '/var/www/beta.domain.com');
host('production') 
->hostname('domain.com')
->user('forge') 
->forwardAgent() 
->stage('production') 
->set('deploy_path', '/home/forge/domain.com');

Here you see two hosts with aliases: beta, production. These you can then use in your command:

dep deploy production 
dep deploy beta

 

Tagged in : Tagged in : , ,
Jasper Frumau

Jasper has been working with web frameworks and applications such as Laravel, Magento and his favorite CMS WordPress including Roots Trellis and Sage for more than a decade. He helps customers with web design and online marketing. Services provided are web design, ecommerce, SEO, content marketing. When Jasper is not coding, marketing a website, reading about the web or dreaming the internet of things he plays with his son, travels or run a few blocks.