Dev.to WebDev πŸ›  Dev πŸ‘ 0 πŸ“– 7 min read

How to Deploy a Laravel Application on Shared Hosting: A Complete Guide

How to Deploy a Laravel Application on Shared Hosting: A Complete Guide Deploying a Laravel application on shared hosting can be challenging, especially when you're working with cPanel, limited server permissions, or a

How to Deploy a Laravel Application on Shared Hosting: A Complete Guide

Deploying a Laravel application on shared hosting can be challenging, especially when you're working with cPanel, limited server permissions, or an environment without full SSH access.

While VPS and cloud hosting offer more flexibility, shared hosting remains a practical and affordable choice for many websites, small businesses, and client projects.

In this guide, I'll walk through the essential steps for deploying a Laravel application on shared hosting, from preparing your project to configuring your database, optimizing performance, and avoiding common deployment mistakes.

1. Check Your Hosting Requirements

Before uploading your Laravel project, make sure your hosting provider supports the requirements of your specific Laravel version.

You will typically need:

  • A compatible PHP version and required PHP extensions
  • MySQL, MariaDB, or another supported database
  • Composer or the ability to upload installed dependencies
  • Apache with URL rewriting enabled, or an equivalent web server configuration
  • File Manager or FTP access
  • SSH access (recommended but not always required)
  • Cron Jobs (if your application uses scheduled tasks)

Always check the official Laravel documentation for the version you're deploying.

2. Prepare Your Laravel Project

Before deployment, make sure the application works correctly in your local environment.

If your project uses Vite to compile frontend assets, run:

npm ci
npm run build

This generates production-ready CSS and JavaScript files, typically inside public/build.

Make sure those generated files are included in your deployment.

Important: Never use npm run dev as your production asset strategy. The Vite development server is intended for local development.

3. Upload Your Project Securely

One of the most important aspects of Laravel deployment is choosing the correct document root.

Laravel's web entry point is located inside the public directory.

A recommended directory structure looks like this:

/home/username/
    laravel-app/
        app/
        bootstrap/
        config/
        database/
        public/
            index.php
            .htaccess
            build/
        resources/
        routes/
        storage/
        vendor/
        .env

Your domain should point directly to:

/home/username/laravel-app/public

This keeps sensitive Laravel files outside the publicly accessible directory.

If your hosting provider allows you to change the document root in cPanel, configure it to point to the public folder.

What if your main domain is locked to public_html?

Some shared hosting plans don't allow changing the main domain's document root.

In that case, an alternative is to keep the application outside public_html, copy only the contents of Laravel's public directory into public_html, and update the bootstrap paths in index.php to match the actual directory structure.

You must also ensure that public assets and storage links point to their correct locations.

Never upload your entire Laravel project into a publicly accessible directory without properly securing it.

4. Install Composer Dependencies

If SSH access is available, navigate to your Laravel project:

cd /home/username/laravel-app

Install the production dependencies:

composer install --no-dev --optimize-autoloader

This installs the dependencies from your lock file without development packages.

If your hosting provider doesn't support Composer, you may need to prepare compatible dependencies in another environment and upload the generated vendor directory.

Make sure the dependencies are compatible with the PHP version running on your hosting server.

5. Configure Your Environment File

Create your production .env file in the Laravel application root, outside the web document root.

A typical configuration looks like this:

APP_NAME="My Laravel App"
APP_ENV=production
APP_DEBUG=false
APP_URL=https://example.com

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=your_database
DB_USERNAME=your_username
DB_PASSWORD=your_password

Replace these example values with your actual hosting configuration.

Also make sure your application has a valid APP_KEY.

For a brand-new application, you can generate one using:

php artisan key:generate

Never regenerate your application key when deploying an existing application that already uses encryption. Doing so can invalidate encrypted data and sessions.

Two important production settings are:

APP_ENV=production
APP_DEBUG=false

Leaving debug mode enabled in production can expose sensitive application information.

Never commit your production .env file to Git.

6. Set Up Your Database

Open cPanel and navigate to the database management section.

Depending on your hosting provider, you may need to:

  1. Create a new MySQL database.
  2. Create a database user.
  3. Assign the user to the database.
  4. Grant the required permissions.
  5. Update your .env configuration.

If you're deploying an existing application, import the database backup using phpMyAdmin or another database management tool.

For an application that uses Laravel migrations, run:

php artisan migrate --force

The --force flag allows migrations to run in production without an interactive confirmation.

Always back up your database before applying production migrations.

Avoid destructive commands such as migrate:fresh on a live application.

7. Configure Directory Permissions

Laravel needs write access to two important directories:

storage/
bootstrap/cache/

The exact permissions depend on your hosting environment and PHP process ownership.

A common configuration uses directory permissions such as 755 or 775, provided the appropriate process has write access.

Avoid setting your entire Laravel project to 777.

If you experience errors involving logs, sessions, caching, or compiled views, verify the ownership and permissions of these directories first.

8. Create the Storage Link

If your application stores public files using Laravel's public disk, run:

php artisan storage:link

This normally creates a symbolic link:

public/storage
    -> storage/app/public

Some shared hosting providers restrict symbolic links.

If this command fails, check whether your hosting environment supports them and verify your public directory configuration.

When using a modified public_html deployment structure, make sure the storage link is created in the actual web document root.

9. Optimize Laravel for Production

Laravel provides several built-in optimization commands.

For production deployments, run:

php artisan optimize:clear
php artisan optimize

The optimize command caches the applicable configuration, routes, events, and views to reduce unnecessary work during requests.

You can also run individual commands when appropriate:

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

One thing to remember: after caching configuration, you should not rely on calling env() directly from application code outside configuration files.

Use Laravel's config() helper instead.

10. Configure Laravel Scheduler

If your application uses scheduled commands, you will need to configure a Cron Job.

Open cPanel and navigate to Cron Jobs.

A typical scheduler entry looks like this:

* * * * * cd /home/username/laravel-app && /usr/bin/php artisan schedule:run >> /dev/null 2>&1

Replace the paths with the correct project and PHP executable paths on your server.

This executes Laravel's scheduler every minute.

Your scheduled tasks can then be defined in Laravel without creating a separate cron entry for each task.

Be aware that some shared hosting providers restrict the minimum cron frequency.

For applications that depend heavily on persistent queue workers, WebSockets, or long-running processes, a VPS or managed Laravel hosting solution may be more appropriate.

11. Common Laravel Deployment Problems

Here are some common issues you may encounter.

500 Internal Server Error

Possible causes include:

  • Incorrect PHP version or missing extensions
  • Missing Composer dependencies
  • Invalid environment configuration
  • Incorrect file permissions
  • Incorrect document root
  • Web server configuration problems

Start by reviewing your application logs and hosting error logs.

CSS and JavaScript Are Not Loading

If your application loads but looks broken, check whether the Vite production assets were uploaded correctly.

Run:

npm run build

Then deploy the generated assets.

Also verify that your web server can access the public/build directory and that no stale Vite development-server configuration remains on production.

Images Are Not Displaying

Check the configured filesystem disk, uploaded file locations, and the public storage link.

For applications using Laravel's public disk, the storage link is often the missing piece.

Laravel Routes Return 404

If the homepage works but other routes return 404 errors, verify that:

  • The domain points to the correct public directory.
  • Apache rewrite rules are enabled.
  • Laravel's public/.htaccess is present when using Apache.
  • Your web server forwards application requests to index.php.

Changes Are Not Appearing

Cached application configuration or views may explain why a recent change isn't visible.

After reviewing your deployment, clear and rebuild the appropriate caches.

12. Final Deployment Checklist

Before considering your Laravel deployment complete, verify the following:

  • [ ] The domain points to the correct public directory.
  • [ ] Production debug mode is disabled.
  • [ ] Sensitive configuration files are not publicly accessible.
  • [ ] Composer dependencies are installed.
  • [ ] Database connectivity works.
  • [ ] Migrations have been applied safely.
  • [ ] Frontend production assets are available.
  • [ ] Required directories are writable.
  • [ ] Public file storage works.
  • [ ] Scheduled tasks are configured when needed.
  • [ ] HTTPS is enabled.
  • [ ] Application logs show no unexpected errors.
  • [ ] Authentication, forms, uploads, and critical application features have been tested.

Final Thoughts

Shared hosting isn't the perfect solution for every Laravel project, but it can be a reliable and affordable option for smaller applications when configured correctly.

The most important things are keeping application files secure, pointing the domain to the correct public directory, using production-ready assets, and maintaining a predictable deployment process.

As your application grows, you can consider moving to a VPS, managed Laravel hosting, or a cloud platform that offers better control over queues, workers, scaling, and automated deployments.

About the Author

I'm Fares Youssef, a Full-Stack Developer specializing in Laravel, PHP, React, REST APIs, and custom web applications.

I enjoy building practical software solutions and sharing insights from web development.

You can explore my work and projects on my personal website.

References:

Have you deployed Laravel on shared hosting? Share the challenges you encountered in the comments!

πŸ“° Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.