- Prerequisites
- Step 1: Prepare Your Laravel Application
- Step 2: Create a Deployment Key in GitHub
- Step 3: Upload the SSH Key to cPanel
- Step 4: Clone Your Repository in cPanel
- Step 5: Configure the Public Folder
- Step 6: Set Up Environment and Permissions
- Step 7: Automate Deployment with Git Pull
- Conclusion
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:
- 
Set Environment Variables: Update your .envfile 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
- 
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
- 
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.
- 
Copy the Public Key: Copy the contents of your public key to your clipboard. cat ~/.ssh/id_rsa.pub
- 
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
- 
Log in to cPanel: Access your cPanel dashboard using your hosting account credentials. 
- 
Access SSH Access: In the Security section, click on SSH Access. 
- 
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.
 
- 
Authorize the SSH Key: After importing, make sure to Manage Authorization for the key and click Authorize. 
Step 4: Clone Your Repository in cPanel
- 
Open Terminal in cPanel: In the Advanced section, click on Terminal. 
- 
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
- 
Clone Your Repository: Use Git to clone your repository. git clone [email protected]:your_username/your_repository.git
Step 5: Configure the Public Folder
- 
Navigate to Your Laravel Directory: Go to the directory where you cloned your repository. cd your_repository
- 
Create a Symlink for the publicFolder:- 
Delete the existing public_htmlfolder in cPanel.rm -rf ~/public_html
- 
Create a symlink from your Laravel publicfolder topublic_html.ln -s ~/your_directory/your_repository/public ~/public_html
 
- 
Step 6: Set Up Environment and Permissions
- 
Copy Environment File: cp .env.example .env
- 
Set Application Key: php artisan key:generate
- 
Install Dependencies: If not already installed, install Composer and run: composer install --optimize-autoloader --no-dev
- 
Set Permissions: chmod -R 775 storage chmod -R 775 bootstrap/cache
Step 7: Automate Deployment with Git Pull
- 
Create a Deploy Script: Create a script to pull the latest changes and run necessary commands. nano deploy.shAdd 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:cacheSave and exit the editor. 
- 
Make the Script Executable: chmod +x deploy.sh
- 
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:
Interested in proving your knowledge of this topic? Take the PHP Fundamentals certification.
PHP Fundamentals
Covering the required knowledge to create and build web applications in PHP.
$99
 
                 
         
        