How I Deploy My Laravel Website With Envoy
Tags: laravel, webdev, php, tutorial
Published: 2021-01-07 21:56:45

So you‘ve built the perfect website, poured hours of time and effort making it perfect.

You’ve tested it in as many browsers and on as many devices you possibly can.

It’s finally ready - it’s time to deploy your website and show the world.

Luckily, Laravel is there for you - as it always is - with Envoy.

In this article I show you how I deploy my own website using Laravel Envoy, so you can do the same with yours.

## What is Laravel Envoy?
Envoy is your new friend that will log in to your server for you and run any tasks you want it to.

By creating a single blade file, you can get Envoy to deploy your site at the run of a single command.

Let’s walk through how…

## Setting Up
Before we begin, you need to have a web server set up and ready to go. Setting up a server ready to host your production site is out of the scope of this article, I’d recommend this article by [Digital Ocean to get started](https://www.digitalocean.com/community/tutorials/how-to-install-linux-apache-mysql-php-lamp-stack-on-ubuntu-20-04).

You will need to be able to log in to your server via ssh. If you can log in from your machine via the command line, Envoy will be able to do its thing.

Once you’re set up, install Envoy with the following command:

```bash
composer require laravel/envoy --dev
```

## Creating Your Envoy File
The Envoy file is where you tell Envoy what you want it to do.

In the root of your application, create a file named Envoy.blade.php.

The Envoy file will contain 2 parts. First, we set the server information.

```php
@servers(['prod' => ['user@ip_address']])
```

This would be populated with your ssh user and ip address. Next, we can define any number of tasks. In our case well just create one called deploy.

```php
@task('deploy', ['on' => 'prod'])
    
@endtask
```

Within our task, we list the commands we want Envoy to perform. Imagine you’ve logged in to the server yourself to deploy or update your site.

```php
@task('deploy', ['on' => 'prod'])
    cd /path/to/codingwithstef.com
    git pull origin master
    composer install --optimize-autoloader --no-dev
    php artisan cache:clear
    php artisan view:cache
    php artisan optimize
@endtask
```

> **No Migrations?**
>
> My site isn't database driven, so there's no need for them to run in my case.

Here we are telling Envoy to:
- Navigate to the website root directory
- Git pull to get the latest code
- Composer install in case any dependencies need to be updated
- Clear our caches

> **composer install --optimize-autoloader --no-dev?**
>
> Adding the **--optimize-autoloader** and **--no-dev** flags optimises the autoloader for production and so is recommended for deployment.

Here's the full file:

```php
@servers(['prod' => ['user@ip_address']])

@task('deploy', ['on' => 'prod'])
    cd /path/to/codingwithstef.com
    git pull origin master
    composer install --optimize-autoloader --no-dev
    php artisan cache:clear
    php artisan view:cache
    php artisan optimize
@endtask
```

That’s it, now we can run Envoy.

## Running Laravel Envoy
You can use the following command to run Envoy.

```bash
php vendor/bin/envoy run deploy
```

You will see the commands being run in your command line so you can check everything’s been done correctly and also see any errors.

That’s it - how easy is that?

## Taking it Further
For more complex set ups, you can run tasks on multiple servers, and even run them in parallel like so.

```php
@servers(['server-1' => 'user@ip_address', 'server-2' => 'user@ip_address'])

@task('deploy', ['on' => ['server-1', 'server-2'], 'parallel' => true])
    cd /path/to/codingwithstef.com
    git pull origin master
    composer install --optimize-autoloader --no-dev
    php artisan cache:clear
    php artisan view:cache
    php artisan optimize
@endtask
```

You can even pass in arguments, for example you could tell it which branch to deploy.

```php
@servers(['server-1' => 'user@ip_address', 'server-2' => 'user@ip_address'])

@task('deploy', ['on' => ['server-1', 'server-2'], 'parallel' => true])
    cd /path/to/codingwithstef.com
    git pull origin {{ $branch }}
    composer install --optimize-autoloader --no-dev
    php artisan cache:clear
    php artisan view:cache
    php artisan optimize
@endtask
```

```bash
php vendor/bin/envoy run deploy --branch=master
```

To read further, check out the [Envoy documentation here](https://laravel.com/docs/8.x/envoy).

Thanks for reading, hopefully you found this article useful. Having this in place on my site makes life so easy updating things!

As always, if you have any feedback or just want to chat about code, drop me a comment below.