How To Set Up Auto Deploy For Laravel Applications On Cpanel
Tags: laravel, cpanel
Published: 2023-09-28 07:27:00

---
title: "How To Set Up Auto Deploy For Laravel Applications On Cpanel"
published: true
description: "In this post, I will show you how to set up auto deploy for your Laravel application on Cpanel."
tags: "laravel, CPanel"
cover_image: https://images.unsplash.com/photo-1510519138101-570d1dca3d66?ixlib=rb-4.0.3&auto=format&fit=crop&w=2947&q=80
# Use a ratio of 100:42 for best results.
published_at: 2023-09-28 07:27 +0000
---

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](https://stephenjude.me/articles/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](https://stephenjude.me/articles/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](https://stephenjude.me/articles/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.

```shell
#!/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.

```shell
chmod +x ~/public_html/deploy.sh
```
We can manually change the bash script file permission using CPanel file permission dialog.
![Cpanel File Permisson Dialog](https://stephenjude.me/assets/images/posts/script-permission.png)

### 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
<?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:

```shell
<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.