How To Set Up Auto Deploy For Laravel Applications On Cpanel

stephenjude

Stephen Jude

Posted on September 28, 2023

How To Set Up Auto Deploy For Laravel Applications On Cpanel

In this post, I will show you how to set up auto for your Laravel application on Cpanel. I want to assume you already have your Laravel app deployed on CPanel, and your version control provider(Github, Gitlab, or Bitbucket) connected to the Cpanel server. If you haven't, please check out my previous posts below:

Setup Deployment Script

We need to create a deploy.sh bash script inside the public_html directory of our CPanel and paste the code snippet below.



#!/bin/sh

# Change to the project directory. 
cd ~/path/to/project

# Pull the latest changes from the git repository
git pull origin main

# Install/update composer dependencies
composer install --no-interaction

# Run database migrations
php artisan migrate --force

# Clear caches
php artisan cache:clear

# Clear and cache routes
php artisan route:cache

# Clear and cache config
php artisan config:cache

# Clear and cache views
php artisan view:cache



Enter fullscreen mode Exit fullscreen mode

The code snippet above when executed, will then change to the project directory and pull the latest changes, install composer dependencies, and other necessary artisan commands for our Laravel application.

Let's make the deploy.sh bash script executable by running the command below on our CPanel terminal.



chmod +x ~/public_html/deploy.sh


Enter fullscreen mode Exit fullscreen mode

We can manually change the bash script file permission using CPanel file permission dialog.
Cpanel File Permisson Dialog

Setup Cpanel Webhook

  • Create a Cpanel subdomain like this: webhook.yourdomain.com or whatever works for you.
  • Create an index.php file inside the subdomain directory ~/public_html/webhook.yourdomain.com and paste the code snippet below:


<?php
// Execute deploy script
$output = shell_exec('. ~/public_html/deploy.sh');

// Send back execution output to the webhook originator.
echo $output; 

// Return OK response
header("HTTP/1.1 200 OK");


Enter fullscreen mode Exit fullscreen mode

This PHP script will execute our bash script, echo the output, and return 200 HTTP responses. You can test this by making an HTTP request to this subdomain using any HTTP client of your choice.

If your subdomain is returning error 500, create a .htaccess file inside the subdomain root directory and paste the code snippet below:



<IfModule mod_rewrite.c>
RewriteEngine On
</IfModule>

Enter fullscreen mode Exit fullscreen mode




Github, Gitlab & Bitbucket Setup

Navigate to the webhook settings of your repository and add your cpanel webhook URL(https://webhook.yourdomain.com/) for push events. You can test this out by adding a commit to your code repository.

Bonus:

If you are using Github, you can check your webhook logs under Recent Deliveries tab in your repository webhook settings. You will see a log of all the webhook requests that have been sent to your Cpanel server. The same applies to Bitbucket but not Gitlab.

💖 💪 🙅 🚩
stephenjude
Stephen Jude

Posted on September 28, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related