Comparisons & Alternatives

How to Deploy a Laravel Project on Shared Hosting via cPanel

Practical guide to deploying your Laravel application on a shared hosting plan with cPanel, handling folder structure and environment variables correctly.

Close-up of two professionals in a meeting, reviewing documents with a pen and paper.

To deploy a Laravel project on shared hosting with cPanel, you need to separate your application files from the public directory (public/) and point your domain to that folder. This adjustment is what makes Laravel work without a dedicated server or root access.

Can Laravel Run on Shared Hosting?

Yes, with some limitations. Laravel requires PHP 8.1 or higher, access to Composer, and control over the domain's document root. Most modern shared hosts — HostGator, Hostinger, Neubox — already offer PHP 8.2 and Composer access via SSH or the cPanel terminal. What shared hosting won't give you is background job queues, native WebSockets, or process control — for those, you need a VPS.

Step 1: Prepare Your Project Locally Before Uploading

Before transferring files, make sure your project is production-ready:

  1. Run composer install --optimize-autoloader --no-dev to install dependencies without development packages.
  2. Build your frontend assets if using Vite or Mix: npm run build.
  3. Copy .env.example to .env and fill in production values (database, APP_KEY, APP_ENV=production, APP_DEBUG=false).
  4. Generate the application key if you have not already: php artisan key:generate.

Do not upload the node_modules/ directory or your local .env file — you will create a production .env directly on the server.

Step 2: Folder Structure on the Server

This is the most critical and confusing step. In cPanel, every domain or subdomain points to a document root directory. The issue is that Laravel expects the server to serve only the public/ folder, not the entire project root.

The recommended structure looks like this:

home/
  yourusername/
    myapp/              ← Laravel files (app/, config/, vendor/, etc.)
    public_html/        ← Document Root for the main domain
      index.php         ← modified to point to myapp/
      .htaccess
      (other assets: css/, js/, images/)

If the project is for a subdomain or addon domain, cPanel lets you choose the Document Root — set it to something like public_html/myapp_public and keep the project files in myapp/ outside public_html.

Step 3: Upload Files to the Server

You have two main options:

Option A: cPanel File Manager

  1. Compress your project (without node_modules) into a ZIP file.
  2. Open the File Manager in cPanel, navigate to the destination directory, and upload the ZIP.
  3. Extract it in place via right-click → Extract.

Option B: SSH + Git (recommended)

If your hosting plan includes SSH access (many shared plans do):

ssh [email protected]
cd ~/myapp
git clone https://github.com/yourusername/my-project.git .
composer install --optimize-autoloader --no-dev

This approach makes future updates simple: just run git pull and composer install.

Step 4: Configure the Domain Entry Point

Once files are on the server, you need to redirect the domain's Document Root to your project's public/ folder. Two ways to do this:

Option 1: Change the Document Root in cPanel

In cPanel → Domains (or Addon Domains / Subdomains), edit the document root and point it directly to /home/user/myapp/public. This is the cleanest approach.

Option 2: Modify index.php in public_html

If you cannot change the Document Root, copy index.php from your public/ folder to public_html/ and update the paths to the autoloader and app bootstrap:

require __DIR__.'/../myapp/vendor/autoload.php';
$app = require_once __DIR__.'/../myapp/bootstrap/app.php';

Also copy the .htaccess file from public/ to public_html/.

Step 5: Configure .env and the Database

  1. Create a MySQL database from cPanel → MySQL Databases. Note the database name, username, and password.
  2. Create or upload a .env file in the project root (~/myapp/.env) with the correct production values:
    APP_ENV=production
    APP_DEBUG=false
    APP_KEY=base64:...
    DB_HOST=localhost
    DB_DATABASE=username_dbname
    DB_USERNAME=username_dbuser
    DB_PASSWORD=secure_password
    
  3. Run migrations via SSH: php artisan migrate --force.
  4. Set the correct permissions on writable folders:
    chmod -R 775 storage bootstrap/cache
    

If SSH is not available on your plan, you can run Artisan commands by creating a temporary PHP file with shell_exec('php artisan migrate --force') — though SSH is always preferable and safer.

Step 6: Verify the Installation

  • Visit your domain in a browser. If you see the Laravel welcome page, the deployment succeeded.
  • If you get a 500 error, check the logs at storage/logs/laravel.log.
  • If raw PHP source code appears instead of the page, the server is running PHP as CGI without the correct module — contact your host's support.
  • Enable configuration and route caching for better performance: php artisan config:cache && php artisan route:cache.

If this deployment is for a client project or a live product, professional setup services are worth considering. At elenlace.com we help agencies and businesses deploy PHP/Laravel applications in secure, optimized environments.

To compare which shared hosting plans best support Laravel in Mexico, check our hosting comparisons section.

Key Takeaways

  • Laravel works on shared hosting as long as the plan offers PHP 8.1+ and Composer access.
  • The essential step is keeping project files outside the Document Root and pointing the domain to public/ only.
  • SSH makes uploads and future updates far easier than manual file transfers.
  • The .env file must be created directly on the server with production credentials.
  • Set correct permissions on storage/ and bootstrap/cache/ to avoid write errors.
  • For larger projects requiring queues or WebSockets, a VPS is the right next step.

Need help deploying your Laravel application or migrating it to a more powerful environment? The team at elenlace.com can guide you through the right configuration from day one.

FAQ

Does every shared hosting plan support Laravel?

No. You need a plan that offers PHP 8.1 or higher, Composer access (via SSH or the cPanel terminal), and the ability to modify the domain's Document Root. Verify these features before purchasing.

Can I run php artisan commands without SSH access?

Yes, in a limited way. You can create a PHP file containing echo shell_exec('php artisan your-command'); and run it from a browser. However, this approach is less secure and the file must be deleted immediately after use.

Why do I get a 500 error after uploading Laravel?

The most common causes are: incorrect permissions on storage/ or bootstrap/cache/, a misconfigured .env file, or wrong autoloader paths in index.php. Check storage/logs/laravel.log for the exact error message.

Can I use server-level environment variables instead of a .env file?

It depends on your host. Some control panels allow you to define per-domain environment variables. If yours does not, placing a .env file directly in the project root (outside the Document Root) is the safe alternative.

Useful resources

Other providers and guides worth comparing:

← All