Get started with 33% off your first certification using code: 33OFFNEW

How to deploy a Laravel App on cPanel using git

3 min read
Published on 19th June 2024

Deploying a Laravel application on cPanel using Git streamlines the process and makes updates more manageable. This tutorial will guide you through deploying a Laravel app on cPanel via Git, ensuring your app is ready for production.

Prerequisites

Before you begin, ensure you have the following:

  • A cPanel hosting account
  • A Laravel application hosted on GitHub
  • Basic knowledge of using cPanel and command-line interface
  • A database (MySQL) created in cPanel

Step 1: Prepare Your Laravel Application

Before deploying your Laravel app, ensure it's ready for production:

  1. Set Environment Variables: Update your .env file with production database credentials and other environment-specific settings.

    APP_NAME=YourAppName
    APP_ENV=production
    APP_KEY=base64:your-app-key
    APP_DEBUG=false
    APP_URL=https://yourdomain.com
    
    DB_CONNECTION=mysql
    DB_HOST=127.0.0.1
    DB_PORT=3306
    DB_DATABASE=your_database_name
    DB_USERNAME=your_database_username
    DB_PASSWORD=your_database_password
    
  2. Optimize Your Application: Run the following commands to optimize your Laravel application:

    php artisan config:cache
    php artisan route:cache
    php artisan view:cache
    

Step 2: Create a Deployment Key in GitHub

  1. Generate an SSH Key: On your local machine, generate an SSH key pair if you don't already have one.

    ssh-keygen -t rsa -b 4096 -C "[email protected]"
    

    Follow the prompts to save the key pair in the default location (~/.ssh/id_rsa). This command generates a public and a private key.

  2. Copy the Public Key: Copy the contents of your public key to your clipboard.

    cat ~/.ssh/id_rsa.pub
    
  3. Add the SSH Key to GitHub:

    • Go to your GitHub repository.
    • Navigate to Settings > Deploy keys.
    • Click Add deploy key, give it a title, and paste your public key into the Key field.
    • Ensure you check Allow write access if you need to push to the repository from cPanel.
    • Click Add key.

Step 3: Upload the SSH Key to cPanel

  1. Log in to cPanel: Access your cPanel dashboard using your hosting account credentials.

  2. Access SSH Access: In the Security section, click on SSH Access.

  3. Manage SSH Keys:

    • Click Manage SSH Keys.
    • Click Import Key.
    • Give the key a name (e.g., GitHubDeployKey).
    • Paste your private key (from ~/.ssh/id_rsa) into the Private Key field.
    • Click Import.
  4. Authorize the SSH Key: After importing, make sure to Manage Authorization for the key and click Authorize.

Step 4: Clone Your Repository in cPanel

  1. Open Terminal in cPanel: In the Advanced section, click on Terminal.

  2. Navigate to Your Desired Directory: Navigate to the directory where you want to deploy your Laravel app. This is usually ~/ or a subdirectory within it.

    cd ~/your_directory
    
  3. Clone Your Repository: Use Git to clone your repository.

    git clone [email protected]:your_username/your_repository.git
    

Step 5: Configure the Public Folder

  1. Navigate to Your Laravel Directory: Go to the directory where you cloned your repository.

    cd your_repository
    
  2. Create a Symlink for the public Folder:

    • Delete the existing public_html folder in cPanel.

      rm -rf ~/public_html
      
    • Create a symlink from your Laravel public folder to public_html.

      ln -s ~/your_directory/your_repository/public ~/public_html
      

Step 6: Set Up Environment and Permissions

  1. Copy Environment File:

    cp .env.example .env
    
  2. Set Application Key:

    php artisan key:generate
    
  3. Install Dependencies: If not already installed, install Composer and run:

    composer install --optimize-autoloader --no-dev
    
  4. Set Permissions:

    chmod -R 775 storage
    chmod -R 775 bootstrap/cache
    

Step 7: Automate Deployment with Git Pull

  1. Create a Deploy Script: Create a script to pull the latest changes and run necessary commands.

    nano deploy.sh
    

    Add the following content to deploy.sh:

    #!/bin/bash
    git pull origin main
    composer install --optimize-autoloader --no-dev
    php artisan migrate --force
    php artisan config:cache
    php artisan route:cache
    php artisan view:cache
    

    Save and exit the editor.

  2. Make the Script Executable:

    chmod +x deploy.sh
    
  3. Run the Deploy Script:

    ./deploy.sh
    

Conclusion

Deploying a Laravel application on cPanel using Git is a streamlined process that makes updates easier and more efficient. By following this tutorial, you've set up a robust deployment pipeline that leverages Git for version control and simplifies the deployment process.

For further reading, check out these resources: