October, 17 2023
Lately I have been experimenting on how to improve the CPanel experience for Laravel developers and luckly I have been successful with my attemps. This article is one of my many cPanel experiments.
I already wrote an article on how to set up auto deploy but this time we will learn how to do it using GitHub Actions.
First, We need to ensure that our cPanel has SSH enabled and connected to our GitHub account. This will enable us to clone our private repositories.
Read more: How To Connect Cpanel To GitHub, Gitlab, And Bitbucket And Deploy Private Repositories
Next, we need to set up our Laravel application on Cpanel. We also have to set up the application database, smtp and other credentials for our production environment and ensure our CPanel is serving the Laravel public directory.
Read more: How To Deploy A Laravel Project On Linux Shared Hosting In 5 Minutes
Replace /path/to/project with the path your Laravel application on CPanel.
#!/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
This script above contains all the necessary commands that runs everytime a new code is deployed. We can always customize this script based on our project need.
Replace cpanel_username with your CPanel username and project_dir with your deployed application directory on CPanel.
name: Deploy
on:
push:
pull_request:
jobs:
tests:
runs-on: ubuntu-latest
name: Build & Deploy Assets
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Setup SSH
run: |
mkdir -p ~/.ssh/
echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa
echo "${{ secrets.SSH_PUBLIC_KEY }}" > ~/.ssh/id_rsa.pub
chmod 600 ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa.pub
ssh-keyscan -H ${{ secrets.SERVER_IP }} >> ~/.ssh/known_hosts
- name: Deploy
run: |
ssh syncflux@${{ secrets.SERVER_IP }} 'bash -s' < ${{ github.workspace }}/.github/workflows/deploy.sh
The Setup SSH section of our workflow connects our GitHub Action instance to our cPanel.
The Deploy section of our workflow executes our deployment bash script on CPanel server via the SSH.
With this setup, our Laravel project will be deployed on CPanel whenever our GitHub actions runs successfully.
If you also have frontend assets that needs to be compiled and deployed on cPanel, you can do that also with GitHub Actions.
Read more: Asset Bundling For Laravel Applications On CPanel Using GitHub Action
Be the first to hear about anything I publish, launch, or think is helpful for you. Subscribe here
Litehost is a web hosting platform for PHP & Laravel developers with Composer, Git, PHP & CLI pre-installed. Try it now