Deploy Laravel Apps

How to Deploy Laravel Application to VPS Server

In the previous article, I wrote about how we can utilize an Envoy package, and gave a walk through to the Envoy use cases, and explained who it makes a lot easier on deploying your next Laravel application to the remote server.

Throughout this article, you are going to learn how you can deploy your laravel apps, with the precise use of Laravel Envoy, including how you can efficiently deploy your updates once you make changes in your version control platforms like Github, Bitbucket.

Read: Laravel Envoy to Deploy your Application to Hosting Server

Also, this article will cover a few more other ideas about the deployment process for Laravel application on different platforms.

Let's get started step by step about the deployment on how you can start deploying your Laravel projects now using the latest Laravel version.

Prerequisites

Before you start, you need to have a deployment-ready server with everything up and running to launch your next Laravel app, as we are not going to deal with any server set up stuff here.

However, you will need to make sure your server meets the requirements based on the Laravel version you are using.

I regularly follow the Digital Ocean technical articles to study about server set up things, as they have an outstanding community with great content.

Set up SSH

Now, the initial step is to generate an SSH key on your server and add it back to VCS platforms like Github, Bitbucket, or whatever you use.

I primarily use Linux Ubuntu flavor to host my apps, so this article takes reference based on the Linux Ubuntu but also would work for most of the Linux flavors.

Access your server via console using SSH connection and follow the command below.

$ ssh-keygen -t rsa -b 4096 -C "[email protected]"

Copy that SSH key based on where you save it.

On Linux, you can cat the contents like below.

$ cat<~/.ssh/id_rsa.pub

After adding it to the version control platform, you can test your SSH connection like below.

$ ssh -T [email protected]

If you are a beginner and need more assistance, I suggest you read this article by Bitbucket, which has in-depth information on it.

Set up Composer

For those who want to set up composer, follow the steps below.

$ cd ~
$ mkdir bin
$ cd bin
$ curl -sS https://getcomposer.org/installer | php

Set up Envoy

// Envoy.blade.php

@setup
    $branch = isset($branch) ? $branch : "master";
    $serverUser = 'deployer';
    $rootDirectory = '~/home/' . $serverUser;

    $server = $serverUser . '@server_ip';
@endsetup

@servers(['production' => $server])

@task('clone', ['on' => 'production'])
    echo '>> cd {{ $rootDirectory }}'
    cd {{ $rootDirectory }}

    echo '>> mkdir {{ $rootDirectory }}/project'
    mkdir {{ $rootDirectory }}/project

    echo '>> chmod 755 {{ $rootDirectory }}/project'
    chmod 755 {{ $rootDirectory }}/project

    echo '>> cd {{ $rootDirectory }}/project'
    cd {{ $rootDirectory }}/project

    echo '<< git clone [email protected]:username/project.git deploy'
    git clone [email protected]:username/project.git deploy
@endtask

@task('environment', ['on' => 'production'])
    echo '>> cd {{ $rootDirectory }}/project/deploy'
    cd {{ $rootDirectory }}/project/deploy

    echo '<< cp .env.example .env'
    cp .env.example .env

    echo '>> SSH to your server, paste your valid .env credentials & save them. Then run envoy run post-deploy'
@endtask

@task('composer-install', ['on' => 'production'])
    echo '>> cd {{ $rootDirectory }}/project/deploy'
    cd {{ $rootDirectory }}/project/deploy

    echo '<< /home/{{ $serverUser }}/bin/composer.phar install --prefer-dist --no-scripts --no-dev -q -o'

    /home/{{ $serverUser }}/bin/composer.phar install --prefer-dist --no-scripts --no-dev -q -o
@endtask

@task('composer-update', ['on' => 'production'])
    echo '>> cd {{ $rootDirectory }}/project/deploy'
    cd {{ $rootDirectory }}/project/deploy

    echo '<< /home/{{ $serverUser }}/bin/composer.phar dump -o && php artisan optimize'

    /home/{{ $serverUser }}/bin/composer.phar dump -o && php artisan optimize

@endtask


@task('migrate', ['on' => 'production'])
    echo '>> cd {{ $rootDirectory }}/project/deploy'
    cd {{ $rootDirectory }}/project/deploy

    php artisan migrate --force;
@endtask

@task('symlink', ['on' => 'production'])
    echo '<< ln -s /home/{{ $serverUser }}/project/deploy/public /var/www/html'

    ln -s /home/{{ $serverUser }}/project/deploy/public /var/www/html
@endtask

@task('deploy-changes', ['on' => 'production'])
    echo '>> cd {{ $rootDirectory }}/project/deploy'
    cd {{ $rootDirectory }}/project/deploy

    echo '>> git checkout {{ $branch }}'
    git checkout {{ $branch }}

    echo '<< git pull --rebase'
    git pull --rebase
@endtask

@story('deploy', ['on' => 'production'])
    setup
    environment
@endstory

@story('post-deploy', ['on' => 'production'])
    composer-install
    composer-update
    migrate
    symlink
@endstory

@story('update')
    deploy-changes
    composer-update
    migrate
@endstory

Deploy

You will only need to run this task when you launch your application for the initial phase on the remote server.

$ envoy run deploy

The initial task finishes with a message telling about preparing the .env file.

Set up .env

Prepare the .env configuration with the necessary credentials for your application to run on the server.

SSH to the server, copy & paste to the server where .env file lies, and save it.

Final Deploy

Once you save the server .env file with correct credentials, then run another command.

$ envoy run post-deploy

After the first successful deploy, you cannot run the same process to deploy your future updates on the repository. Now, your future job will be only pulling new changes or installing more new packages if needed.

$ envoy run update

Finishing

It is a reasonable question to come in your mind, this deployment script is likely to break the application, and it could take your application to go through downtime either on any of the deployment.

I agree with the above statement, as it is not a zero-downtime deployment solution, as the article was focused generally on the way of only deploying your code to the server.

As being a developer, having some server related knowledge is almost necessary for every day to work with fun on any application, no matter the size and infrastructure required to scale it.

People these days talk about zero-downtime deployment and have seen a lot of articles about it, and there are commercial platforms available to handle the concern.

If you want to deploy your code to your server without worrying about downtime, choose the service like Laravel Envoyer, which is a commercial product from the Laravel ecosystem.

Even more, you do not want to worry about managing servers and want to go hassle-free with auto-scaling your apps, use Forge, and Vapor a serverless platform.

Using those managed services, you need to pay for both your server cost as well as the service cost every month, using those platforms helps your team to focus more on the work rather than suffering from deployments and servers.

Also, as a free solution for zero-downtime deployment strategy, there is an open-source package built by Papertank.

Tip: People ask a lot of questions about how to deploy Laravel to shared hosting frequently. But, my answer to this is, Laravel works great on VPS service providers like Digital Ocean, Vultr Linode, AWS, or any Custom VPS, etc.

Conclusion

Thanks for being up to the end of the article. Please share this article to your circle, if you enjoyed reading it.

Follow me on Twitter for more updates or visit the blog more often to view new articles at a later date.