Deploy Laravel app on shared server using Envoy

Last updated: Friday 19th April, 2024 06:08 PM IST
  1. Home
  2. Articles
  3. Deploy Laravel app on shared server using Envoy
Deploy Laravel app on shared server using Envoy

Introduction

On the shared hosting servers, it becomes a chore SSHing every time and executing the commands like cd to a domain, git pull to sync all the files and folders, npm build to build the production-ready app, etc. Then I realized about Laravel Envoy which is an official package from Laravel that does that same thing for us. All we have to do is write all the steps in a blade file that we usually perform after SSHing.

Things Required

  • Laravel application on your system
  • Same application on GitHub or any version control of your choice
  • Shared hosting account with SSH access

Installation

In your application, install the Laravel Envoy package which is an official package of Laravel using the following command

composer require laravel/envoy --dev

Writing the steps

Now, we will create a blade file called Envoy.blade.php at the root of your project.

touch Envoy.blade.php

Content of Envoy blade file

Let us learn how to write the content of this special blade file.

Servers

The first line should always be the @servers directive. The following is the syntax

@servers(['web' => '<your-username>@<ip-address> -p <port-number>'])

You can name the web key with anything of your choice. I will keep it as web.

<your-username> is the username that is given to you by your shared hosting provider. <ip-address> is the IP Address of your website. <port-number> is the port number that will be used by you for SSHing into your server.

Usually, the hosting provider will give you the entire command. You simply have to copy and paste it.

Tasks

Next comes your tasks or the steps that you will be executing on your server.

Each and every line written will be executed on your server, as they are all commands that you would be executing yourself after SSHing.

It has @task(<task-name>) directive. Now, we will write the tasks that are required for us to execute all the steps in order to deploy our Laravel application on our shared hosting.

First is cd to where your website files are.

cd /home/<your-username>/domains/<domain-name>

Replace your-username with your actual username. If you're on different shared hosting provider, then please check where your files are stored.

Remove the old vendor/ directory

rm -rf vendor/ 
echo "Removed vendor/ directory"

You see we are now removing the old vendor directory as we will be reinstalling the composer packages in the upcoming steps

Now, we will pull the latest code from the GitHub or any other version provider you are using

git pull origin main

This will pull all the latest changes onto your website from the GitHub. Note: Please make sure that you have added the private SSH key to your GitHub account which is related to your Hosting Service Provider.

Install composer packages as we have deleted the vendor/ directory.

composer install --optimize-autoloader --no-dev

Run the migration

php artisan migrate --force

Clearing the previous cache

php artisan config:clear
php artisan route:clear
php artisan view:clear

This will clear all the previous cache for the config route and view.

Creating a new cache

php artisan config:cache
php artisan route:cache
php artisan view:cache

This will cache the config, route, and views once again.

Installing the node packages

# Please make sure that you have installed
# the node and nvm on your shared hosting server
# If not, you can google how to install it
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
nvm use 20.9.0
npm install
npm build

Removing the node_modules/ directory

rm -rf node_modules/

Why delete the node_modules/ directory? That is because we are not going to use it anytime in our entire application. Plus it also increases the storage space on your shared hosting.

This is how it looks in its entirety.

@servers(['web' => '<username>@<ip-address> -p <port-number>'])

@task('deploy')
    cd /home/<username>/domains/<domain>
    echo "Inside <domain> directory..."

    rm -rf vendor/
    echo "Removed vendor/ directory"

    git pull origin main

    composer install --optimize-autoloader --no-dev

    php artisan migrate --force

    php artisan config:clear
    php artisan route:clear
    php artisan view:clear

    php artisan config:cache
    php artisan route:cache
    php artisan view:cache

    export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
    [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
    nvm use 20.9.0

    npm install

    npm run build

    rm -rf node_modules/
    echo "Removed node_modules/ directory.."
@endtask

Done. That should all be there in your Envoy.blade.php.

Execute the task

Now, in your terminal execute the following command:

php vendor/bin/envoy run deploy

The deploy is the same name that you write for the @task directive. Give your password in the prompt if it asks, and your tasks will be executed one by one.


Reference Links


Courtesy Links

Mehul Bawadia

Author:

Mehul Bawadia is a Full Stack Developer in Laravel and VueJs, based in Mumbai, India. He has an overall experience of 8 years in the website development field. When he is not working, you will find him learning new stuff that is not related to work.

Process Followed

1. Discover

In this process, I learn more about the requirements from you and/or from the client, and come up with varios permutations and combinations to meet the requirements.

2. Design

Once I learn properly, I do the design of the requirements that you gave keeping things aesthetically pleasing & useable for your audience.

3. Develop

Once you and/or the client is happy with the design(s), I start with the development process of the said requirements.

4. Deploy

After development, I will send the developed task to the client for reviewing. Once confirmed, will be deployed to the live server.

Tech Stack

HTML 5

HTML 5

CSS 3

CSS 3

TailwindCSS

TailwindCSS

JavaScript

JavaScript

PHP

PHP

MySQL

MySQL

Laravel

Laravel

VueJs

VueJs

Spare time Projects

Few of the simple projects simply to learn.

View Projects
Copyright © 2024, BMehul. All Rights Reserved.
Built with A heart icon by Mehul Bawadia.
An arrow that takes you to the top of the page when clicked