DEV Community

Mohammad Salim Hosen
Mohammad Salim Hosen

Posted on • Edited on

How to deploy laravel project on cpanel or shared hosting

Deploying a Laravel project on shared hosting can be a bit tricky, but it’s entirely possible with the right steps. In this guide, we will walk you through the process of uploading and deploying your Laravel project to a cPanel or shared hosting environment.

Step 1: Prepare Your Laravel Project Files

The first step is to zip your entire Laravel project, ensuring all necessary files are included. This includes everything from your app, config, database, and other core directories to the .env file and your public assets.

Step 2: Upload the Zip File to cPanel

Log in to your cPanel account and go to the File Manager. Navigate to the public_html directory (this is typically the root folder for your website on shared hosting).

Upload the zip file of your Laravel project here.

Step 3: Unzip the File in the Public Directory

Once the zip file is uploaded, right-click the file and select Extract to unzip it directly into the public_html directory.

Make sure that all Laravel project files are correctly placed in the public_html folder. You should see your project’s root structure (e.g., app, bootstrap, config, etc.) inside public_html.

Step 4: Configure .htaccess for Laravel

Laravel projects are built to route requests through the public folder. To ensure this works properly in a shared hosting environment, you need to create or modify the .htaccess file in the root public_html directory.

Here’s the content you need to add to the .htaccess file:
`

RewriteEngine On
# Redirect all requests to the public folder
RewriteRule ^(.*)$ public/$1 [L]

# Deny access to all directories except public
RewriteRule ^(app|bootstrap|config|database|resources|routes|storage|tests|vendor)/(.*)$ - [R=404,L]

# Deny access to sensitive files
RewriteRule ^(\.env|\.log|\.git|composer\.(json|lock)|artisan|phpunit\.xml)$ - [R=404,L]
Enter fullscreen mode Exit fullscreen mode

`

This code will ensure that:

All incoming requests are redirected to the public folder.
Direct access to sensitive directories like app, bootstrap, config, etc., is denied.
Access to sensitive files such as .env, .git, and composer.json is blocked.

Step 5: Configure the .env File

You will need to configure the .env file for your production environment. Make sure to set the following parameters:
APP_ENV=production
APP_DEBUG=false
APP_KEY=base64:yourAppKey
DB_CONNECTION=mysql
DB_HOST=yourDatabaseHost
DB_PORT=3306
DB_DATABASE=yourDatabaseName
DB_USERNAME=yourDatabaseUser
DB_PASSWORD=yourDatabasePassword

Make sure to generate a new application key if you haven’t already. You can do this locally and update it in your .env file:
php artisan key:generate
Update your database credentials based on the information provided by your hosting provider.

Step 6: Set Correct Permissions for Directories

Some directories, such as storage and bootstrap/cache, need to be writable by the web server. Set the correct permissions for these directories.

To do this:

  • Navigate to storage and bootstrap/cache directories in the cPanel File Manager.
  • Right-click the folder and select Change Permissions.
  • Set the permissions to 775 for both directories.

Step 7: Browse Your Website

Once everything is set up, visit your website’s domain, and you should see your Laravel application live! Make sure to test all functionality to ensure everything is working as expected.

Conclusion

Deploying a Laravel application on shared hosting is achievable with the right configuration. By following the steps above, you can have your Laravel project up and running on cPanel hosting in no time.

“If you found this guide helpful, check out more tutorials and resources on my personal blog https://bitbotlab.com

Image of Datadog

Master Mobile Monitoring for iOS Apps

Monitor your app’s health with real-time insights into crash-free rates, start times, and more. Optimize performance and prevent user churn by addressing critical issues like app hangs, and ANRs. Learn how to keep your iOS app running smoothly across all devices by downloading this eBook.

Get The eBook

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs