How to Deploy Laravel Project on cPanel

Deploy Laravel Project on cPanel

How to Host Laravel Project on cPanel

To deploy Laravel project on cPanel can seem overwhelming, but it doesn’t have to be. Whether you’re launching a personal project or a client’s website, cPanel makes hosting Laravel applications straightforward.

In this guide, I’ll walk you through the entire process, from setting up your hosting environment to ensuring your application runs smoothly.

Prerequisites

Before diving into the deployment, make sure you have:

How to Deploy Laravel Project on cPanel

This guide outlines two methods for deploying a Laravel project within a cPanel environment: deployment via Terminal access and manual deployment (without Terminal access).

We will explore both options to accommodate users with varying cPanel configurations.

Deploy Laravel Project on cPanel: Manual Deployment (without Terminal)

This section details the process of deploying a Laravel project on cPanel for users who do not have Terminal access. It covers the necessary steps for manually uploading files, configuring directories, and setting up the application without command-line interface capabilities.

Step 1: Upload Your Laravel Project to cPanel

Option 1: Upload via File Manager

  1. Zip Your Project – On your local machine, compress your Laravel project into a .zip file. It’s crucial that your zipped file contains all of these folders and files.
Laravel project files
  1. Open cPanel – Log into your cPanel account and navigate to File Manager.
  2. Upload the Zip File – Go to the public_html directory (this is the root directory where your website files should go. Alternatively, you can use a subdirectory if you have created a subdomain), then upload the zipped project.
  3. To view hidden files (such as .htaccess, .env), click on Settings in the top-right corner and enable Show Hidden Files (dotfiles).
  4. Extract the Zip File – Once uploaded, right-click the file and select Extract.
  5. Move the Extracted Files – Move extracted files into the public_html folder or the subdirectory folder in case you are using a subdomain.
deploy your laravel project extracted files

Note: After extracting the files, delete the zip file. Once the files are moved to the public_html directory, delete the laravel project folder.

Suggested Reading: For detailed instructions on how to upload your project files to cPanel, follow this step-by-step guide: “How to Upload Your Project Files.”

Option 2: Upload via FTP

  1. Use an FTP client like FileZilla.
  2. Connect to your cPanel account using your FTP credentials.
  3. Upload the project files to the appropriate directory (usually public_html).

For detailed instructions on how to upload your project files to via FTP, follow this step-by-step guide: “How to Upload Your Project Files Via FTP“.

Create a MySQL Database and Import a Database File

Creating the Database

  1. In the cPanel dashboard, scroll down to the Databases section.
  2. Click MySQL Database Wizard.

Create a New Database

  1. Enter a name for your new database (e.g., my_db).
  2. Click Next Step.

Create a Database User

  1. Enter a username for the database user (e.g., my_db_usr).
  2. Set a strong password (use the password generator for extra security).
  3. Click Create User.

Assign Privileges to the User

  1. Check All Privileges to give full access to the user.
  2. Click Next Step to finalize the setup.

Import the Database

  1. Go back to cPanel and locate the phpMyAdmin tool in the Databases section.
  2. Click phpMyAdmin to open it.
  3. In phpMyAdmin, find the newly created database in the left sidebar and click on it.
  4. Click the Import tab at the top.
  5. Click Choose File and select the .sql database file from your PC.
  6. Scroll down and click Go to start the import process.

Suggested Reading:

Connecting to Your Database

  1. Locate the .env File
    • Navigate to your Laravel project directory.
    • If the .env file is not visible, click on Settings in the top-right corner and enable Show Hidden Files (dotfiles).
    • If the .env file is still missing, re-upload it from your local PC.
    • Alternatively, if a .env.example file exists, copy it and rename it to .env.
  2. Edit the .env File
    • Open the .env file in a text editor.
    • Update the necessary database configurations as required.
APP_URL=https://yourdomainname.com

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=yourCpanelUsername_yourDbName
DB_USERNAME=yourCpanelUsername_yourDbUsername
DB_PASSWORD=yourDatabasePassword

Save the changes, and your Laravel project should now be connected to the database.

Remove ‘/public’ from the URL of a Laravel installation

To remove /public from the URL, edit your .htaccess file or create a new one in the root folder and add:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

Save your changes to the .htaccess file. Your site should now just be loading as domain.com instead of domain.com/public.

Set File Permissions

For Laravel to work properly, you need to set the correct permissions for the storage and bootstrap/cache directories:

  1. Right-click on the storage folder and select Change Permissions.
  2. Set the permissions to 775 (or 777 if necessary).
  3. Repeat the same for the bootstrap/cache folder.

Alternatively, if you have SSH access, you can run the following command in the terminal:

chmod -R 775 storage bootstrap/cache

If you run into issues, try setting them to 777 temporarily:

chmod -R 777 storage bootstrap/cache

(However, setting 777 is not recommended for security reasons.)

Deploy Laravel Project on cPanel Via Terminal

Deploying a Laravel project on cPanel using the terminal (SSH) ensures better performance and flexibility compared to manual uploads. Follow these steps to deploy your Laravel application successfully.

Step 1: Log in to cPanel and Access Terminal (SSH)

  1. Log into your cPanel by visiting domain.com/cpanel and entering your credentials.
  2. Open the Terminal (if enabled) by navigating to Advanced > Terminal.

Step 2: Navigate to the Correct Directory

  1. By default, cPanel’s public_html is the web root directory.
  2. Move to the public_html directory using:
cd public_html

Step 3: Clone Your Laravel Project

If your Laravel project is hosted on GitHub, GitLab, or Bitbucket, use the following command to clone it:

git clone https://github.com/yourusername/your-laravel-project.git

OR if you’re uploading manually, use FTP/File Manager to upload your Laravel files into public_html.

Step 4: Move Laravel Files to the Root Directory

If your project is inside a folder (e.g., your-laravel-project), move all files to public_html:

mv your-laravel-project/* your-laravel-project/.* .

Remove the empty directory:

rmdir your-laravel-project

Step 5: Install Dependencies (Composer)

  1. Run the following command to install Laravel dependencies:
composer install --no-dev --optimize-autoloader

If Composer is not available, install it using:

curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer

Step 6: Set Permissions

Run the following commands to set correct file permissions:

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

Step 7: Configure the .env File

  • If .env doesn’t exist, rename the .env.example file to .env:
cp .env.example .env
  • Open the .env file and update the database credentials:
nano .env

Modify these values to match your cPanel MySQL details:

APP_URL=https://yourdomainname.com

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=yourCpanelUsername_yourDbName
DB_USERNAME=yourCpanelUsername_yourDbUsername
DB_PASSWORD=yourDatabasePassword

Step 8: Generate Application Key

Run the command:

php artisan key:generate

Step 9: Run Database Migrations

If your Laravel project has database migrations, execute:

php artisan migrate --force

Step 10: Remove ‘/public’ from the URL of a Laravel installation

To remove /public from the URL, edit your .htaccess file or create a new one in the root folder and add:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

Save your changes to the .htaccess file. Your site should now just be loading as domain.com instead of domain.com/public.

Step 11: Set Up File Permissions

Run the following commands:

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

Optimize Laravel for Production

Run the following optimization commands:

php artisan config:clear
php artisan route:clear
php artisan view:clear

This will speed up your application by caching configurations and routes.

Set Up Cron Jobs (Optional)

Laravel uses scheduled tasks that should run automatically. In cPanel:

  1. Go to Cron Jobs under the Advanced section.
  2. Add a new cron job with the command:
php /home/your-cpanel-username/public_html/artisan schedule:run >> /dev/null 2>&1

Set it to run every minute (*/5 * * * *).

Final Checks

Now that everything is set up, visit your website and check if it loads correctly. If you encounter errors:

  • Check file permissions.
  • Review the .env file for database connection issues.
  • Run php artisan cache:clear and php artisan config:clear.

If all goes well, congratulations! Your Laravel project is now successfully deployed on cPanel.

FAQs About Deploying Laravel on cPanel

Conclusion

Deploying a Laravel project on cPanel might seem complex at first, but by following these step-by-step instructions, you can get your application up and running smoothly. Whether you’re manually uploading files or using Git, optimizing for production is key to ensuring speed and security.

If you found this guide helpful, feel free to share it or drop a comment below if you have any questions!

Happy coding!

Suggested Reading:

Picture of Kennedy Dzigbenyo

Kennedy Dzigbenyo

Kennedy Dzigbenyo is a dedicated Software Engineer and WordPress Expert whose expertise lies in turning conceptual digital dreams into tangible realities. He excels in the art of creating digital solutions that respond to contemporary needs.

Leave a Reply

Enjoy 20% Off Your First Order!

Use promo code WELCOME at checkout to claim your exclusive discount.

Get 20% OFF on your first order

Oh hi there! It’s nice to meet you.

Please Kindly, sign up to receive awesome content in your inbox, every month.