Stephen Jude
Posted on September 28, 2023
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:
- Run Composer & PHP Commands On Cpanel Terminal Using Any PHP CLI Version Installed — CentOS & CloudLinux
- How To Connect Cpanel To Github, Gitlab, And Bitbucket And Deploy Private Repositories
- How To Deploy A Laravel Project On Linux Shared Hosting In 5 Minutes
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
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
We can manually change the bash script file permission using CPanel file permission 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");
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>
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.
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
November 24, 2019