Laravel

How to Deploy a Laravel Project on cPanel: A Step-by-Step Guide

Deploying a Laravel project on cPanel can seem like a challenge if you’re new to it, but with a little guidance, you’ll be able to get your project up and running in no time. Whether you’re hosting your Laravel app for the first time or need a quick refresher, this article will walk you through the process in simple steps.

Prerequisites Before You Begin

Before you start deploying your Laravel project on cPanel, make sure you have the following:

  • A cPanel hosting account
  • A Laravel project ready to be deployed
  • SSH access (optional, but recommended)
  • PHP version 7.4 or higher (as Laravel 8.x requires this)

Step 1: Prepare Your Laravel Project

The first step in deploying your Laravel project is preparing it for deployment. Here’s what you need to do:

  1. Composer Install: Ensure that all necessary Composer dependencies are installed on your local machine. Run the following command in your project’s root directory:
composer install --optimize-autoloader --no-dev

This will install all dependencies required for production.

Environment Configuration: Update your .env file to reflect the correct database settings and any other environment-specific configurations, like mail settings, cache configurations, and API keys.

App Key: Make sure that your Laravel app key is set. If you haven’t set it up yet, you can generate a new one by running:

php artisan key:generate

Optimize Laravel for Production: Run the following Artisan command to optimize your Laravel project for production:

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

Step 2: Upload Laravel Files to cPanel

Now that your Laravel project is ready, it’s time to upload it to cPanel. Follow these steps:

  1. Access cPanel File Manager: Log in to your cPanel account and go to the File Manager.
  2. Upload Your Laravel Project: In the File Manager, navigate to the public_html directory or any subdirectory where you want to host the project. Upload the files of your Laravel project (excluding the .env file for security reasons) to this directory.
  3. Set the Correct Permissions: Ensure that the necessary directories are writable by the web server. Common directories that need permissions include:
    • storage/bootstrap/cache/ You can set the correct permissions by running:
chmod -R 775 storage bootstrap/cache

Step 3: Configure the Web Server (Apache/Nginx)

For the Laravel app to work properly, we need to ensure the web server points to the correct directory. On cPanel, Apache is usually used.

  1. Update the Document Root: The public directory of your Laravel app should be the public-facing directory. If you’ve uploaded your app to a subfolder (e.g., yourdomain.com/laravel), make sure to set the document root of the domain or subdomain to yourdomain.com/laravel/public.
  2. Create a Subdomain (optional): If you want to access the Laravel app via a subdomain (e.g., app.yourdomain.com), you can create a subdomain through cPanel and point it to the public folder of your Laravel app.

Step 4: Set Up the Database

Most Laravel apps will require a database. To set it up:

  1. Create a Database in cPanel: Go to cPanel > MySQL Databases and create a new database, user, and assign the user to the database.
  2. Update the .env File: In your .env file, update the database settings to match those of the cPanel-created database:
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=your_database_username
DB_PASSWORD=your_database_password

Migrate the Database: Run the following command to migrate your database schema to the live server:

php artisan migrate --force

Step 5: Set Up Storage and Permissions

Laravel requires certain directories to be writable. Here’s how to ensure proper permissions:

  1. Storage Link: If you have file uploads or other assets stored in the storage folder, create a symbolic link to the public directory:
php artisan storage:link
  1. Set Directory Permissions: Make sure the storage/ and bootstrap/cache/ directories have the correct permissions (775).

Step 6: Configure .htaccess for Laravel (Apache)

If your server is running Apache, you’ll need to configure the .htaccess file to make sure everything works properly. Laravel comes with a default .htaccess file located in the public directory. Make sure this file is uploaded to the public_html directory (or wherever your public folder is).

Step 7: Test Your Laravel Application

Once everything is set up, it’s time to test your Laravel app. Visit your domain or subdomain and check if the Laravel application is loading correctly. If you encounter any issues, make sure your .env file is correctly configured, all paths are correct, and the database is set up properly.

Conclusion

Deploying a Laravel project on cPanel doesn’t have to be difficult. By following these simple steps, you can easily deploy your Laravel app and get it running on your hosting provider. Just make sure to keep your environment configurations up to date and ensure proper file permissions. With these basics covered, you can now focus on your app’s features and functionality. Happy deploying!


If you’re looking for a reliable and experienced partner to help deploy and maintain your Laravel projects, CodeHunger is a fantastic option. As one of the leading website development companies, they specialize in creating dynamic web applications tailored to your needs. Whether you’re working on a complex Laravel project or need ongoing support, CodeHunger’s team of experts can help you deliver high-quality solutions with seamless performance. Their professional web development services ensure that your project runs smoothly from start to finish.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button