Laravel Deployments using Deployer at no cost
2 min read

Laravel Deployments using Deployer at no cost

Automating deployments for your Laravel 8 project (or any other version) on your dedicated or shared hosting sounds daunting at first. I have been at that place. For my job board back when I launched it, I started looking at tools that I could use for deployment for free. If you don't have a budget, or you're keen on learning how to deploy automatically or you like setting up things yourself, this is a decent option.

Setup

  • Ubuntu 20.04
  • PHP 8 & PHP FPM
  • Laravel 8.x
  • Deployer 7 by Anton Medvedev (wonderful open source tool) - we will use the latest beta with support for PHP 8

On your terminal in the Laravel project, install the package (watch out for the latest releases of Deployer)

composer require deployer/deployer"^v7.0.0-beta.16" --dev

Or if you prefer a distribution version

composer require deployer/dist:"^v7.0.0-beta.16"  --dev

Once that is done, let's initiate which will create a deploy.php ideally at the root of the project which we'll edit

dep init

Most of the

<?php

namespace Deployer;
require 'contrib/rsync.php';

// Include the Laravel & rsync recipes
require 'recipe/laravel.php';

set('application', 'dep-demo'); //for your reference
set('ssh_multiplexing', true); // Speed up deployment

set('rsync_src', function () {
    return __DIR__; // If your project isn't in the root, you'll need to change this.
});

// Files you don't want in your production server.
add('rsync', [
    'exclude' => [
        '.git',
        '/storage/',
        '/vendor/',
        '/node_modules/',
        '.github',
        'deploy.php',
    ],
]);

task('php-fpm:restart', function () {
    run('service php8.0-fpm restart');
});


// Hosts
host('your_website_or_IP')
->setRemoteUser('your_SSH_user') // SSH user
->setDeployPath('/var/www/website')  // Deploy path
->setIdentityFile('~/.ssh/id_rsa'); // Your SSH key

after('deploy:failed', 'deploy:unlock'); // In case your deployment goes wrong

desc('Deploy the application');
task('deploy', [
    'deploy:info',
    'deploy:prepare',
    'deploy:lock',
    'deploy:release',
    'rsync',
    'deploy:secrets', 
    'deploy:shared',
    'deploy:vendors',
    'deploy:writable',
    'php-fpm:restart',
    'artisan:storage:link',
    'artisan:view:cache',   
    'artisan:config:cache', 
    'artisan:optimize',     
    'artisan:migrate',     
    'deploy:symlink',
    'deploy:unlock',
    'deploy:cleanup',
]);

Deployer 7 has the recipes for most of your common tasks built-in, which is great. You can see the list of available recipes.

Once this is all setup, all you have to do is run and that should be do everything you've described

dep deploy

You should see a new folder structure in your host, where it has releases and a release folder. Deployer syncs your code to your server using rsync, runs your tasks and then creates a symlink to point to the release folder which contains your latest code.

So make sure that you point your webserver to /var/www/website/release, restart your webserver, and you should be good to go.

If you're looking for automating this using GitHub Actions, take a look at this great guide by Atymic - it's what helped me discover this tool and guided me on the entire process.

Receive tips / tricks on PHP, Laravel and more