Laravel Deployments using Deployer at no cost
Tags: laravel, php
Published: 2021-03-27 10:19:39

---
title: Laravel Deployments using Deployer at no cost
published: true
date: 2021-03-27 09:31:32 UTC
tags: laravel,php
canonical_url: https://adithya.dev/deploying-laravel-using-deployer/
---

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](https://arbeitnow.com/) 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](https://deployer.org/) 7 by Anton Medvedev (wonderful [open source](https://github.com/deployphp/deployer) 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](https://github.com/deployphp/deployer/releases))

```
composer require deployer/deployer --dev

```

Or if you prefer a distribution version

```
composer require deployer/dist --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](https://github.com/deployphp/deployer/tree/master/recipe).

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](https://atymic.dev/blog/github-actions-laravel-ci-cd/) - it's what helped me discover this tool and guided me on the entire process.