Milind Daraniya

Deploying Laravel Applications on Shared Hosting

Published January 13th, 2023 17 min read

Deploying Laravel applications on shared hosting can be challenging due to the limited access and configurations provided by the hosting provider. However, with careful planning and some adjustments, it is possible to run a Laravel application on shared hosting environments. In this post, we'll explore the steps to deploy a Laravel application on shared hosting and discuss potential challenges and solutions.

Preparing the Laravel Application

Before deploying your Laravel application, make sure it is ready for production. Follow these steps:

  1. Environment Configuration: Update the .env file with the correct database credentials and other environment-specific settings.
  2. Optimize Autoloader: Run the following command to optimize the autoloader, which improves performance:
composer install --optimize-autoloader --no-dev
  1. Clear Caches: Clear the application cache and configuration cache:
php artisan cache:clear
php artisan config:cache

Generate Key: Generate an application key to secure session data and other encrypted data:

php artisan key:generate

Uploading the Application

  1. Zip the Application: Compress your Laravel application into a zip file.
  2. Upload to Shared Hosting: Access your shared hosting control panel or use FTP to upload the zip file to the desired directory.
  3. Unzip the Application: Extract the contents of the zip file in the hosting directory.
  4. Set Permissions: Set proper file and folder permissions for Laravel to function correctly:
chmod -R 755 storage bootstrap/cache

Adjusting .htaccess and Web Server Configuration

Shared hosting often uses Apache as the web server. You may need to adjust the .htaccess file to ensure Laravel's routing works properly.

Create or modify the .htaccess file in the root directory:

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

If your shared hosting allows custom Apache configurations, you can create or modify a php.ini file to set necessary PHP configurations.

Composer on Shared Hosting

Check if the hosting provider has installed Composer globally. If not, you may need to install Composer locally in your Laravel application directory.

SSH into your shared hosting and run the following command:

curl -sS https://getcomposer.org/installer | php

This installs Composer in the current directory.

Handling Cron Jobs

If your Laravel application relies on cron jobs, you may need to set up cron jobs through your hosting control panel or via SSH.

For example, to run Laravel's scheduled tasks, add the following cron job:

* * * * * cd /path/to/your/laravel/application && php artisan schedule:run >> /dev/null 2>&1

Deploying Laravel applications on shared hosting requires some extra considerations, but it is achievable with the right adjustments. By preparing your application, uploading it correctly, and configuring the web server, you can run a functional Laravel application on shared hosting.

Keep in mind that shared hosting may have resource limitations, so optimize your application and keep an eye on performance. Consider upgrading to VPS or dedicated hosting if your Laravel application requires more resources and control.