Easily Deploy Laravel To Cpanel With Github Actions

2 years ago sysadmin

Using a shared hosting plan with cPanel can be a cost-effective alternative to a VPS. However, the lack of SSH access can complicate things if you're looking to host a Laravel application. Fortunately, GitHub Actions offers a practical solution for deploying code changes smoothly, even without direct server access.

On CPanel

  1. Create a folder on cpanel for your project
  2. Create a ftp user on cpanel that has access to that folder (hold on to that username and password)
  3. Take note of the ftp server address

On Github

  1. Add server address to your repo secrets as FTP_SERVER
  2. Add the ftp username to your repo secrets as FTP_USERNAME
  3. Add the ftp password to your repo secrets as FTP_PASSWORD
  4. Create Github Action file: my-repo-project/.github/workflows/deploy-via-ftp.yml
# This is a basic workflow to help you get started with Actions
name: Deploy via ftp

# Controls when the workflow will run
on:
  # Triggers the workflow on push or pull request events but only for the master branch
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v2

      - name: "Setup PHP"
        uses: "shivammathur/setup-php@v2"
        with:
          php-version: "8.1"

      - name: "Composer Install"
        uses: "ramsey/composer-install@v2"

      - name: "Upload To FTP"
        uses: SamKirkland/FTP-Deploy-Action@4.2.0
        with:
          server: ${{ secrets.FTP_SERVER }}
          username: ${{ secrets.FTP_USERNAME }}
          password: ${{ secrets.FTP_PASSWORD }}
          server-dir: "my-project/"

check if your branch is called master or main, or you can choose some custom branch.

change the “my-project/” to where you want your projects to be located on the host.

review all the versions of stuff used here to align with what you need, especially the php version.

Deploying

On the first deployment, all files are sent to the server, which can take some time. If you're concerned about exceeding your GitHub Actions free monthly quota, a good solution is to set up a self-hosted runner on your local machine. This way, you can handle the initial deployment locally, then switch back to the GitHub-hosted runner for subsequent deployments, which will only push the changed files.

How to Migrate on cpanel

Make sure to create a database on your shared hosting.
Create a cronjob that will run the laravel scheduler

app/Console/Kernel.php

protected function schedule(Schedule $schedule)
{
    $schedule->command('migrate --force')
        // Only run if .deploy-artifact exists
        ->when(function () {
            return file_exists('.deploy-artifact');
        })
        // When migrated, delete the .deploy-artifact
        ->then(function () {
            unlink('.deploy-artifact');
        });
}

Other Posts