Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

oka_'s avatar
Level 1

Laravel Deployment on Shared Hosting

I'm trying to deploy my laravel project on shared hosting Cpanel. But I'm getting a 404 error. When I'm trying to move some files, I accidentally make a public_html folder, so I deleted it (I'm pretty sure I deleted the folder I make, not the real public_html).

I changed the following lines in index.php

require __DIR__.'/../vendor/autoload.php';

to

require __DIR__.'/../laravel/vendor/autoload.php';

I also did the same thing to

$app = require_once __DIR__.'/../bootstrap/app.php';

What could be wrong with this deployment approach?

0 likes
15 replies
Sinnbeck's avatar

Everything. Don't change the structure in the php files. 2 options

  1. Put the files in the same folder as public_html. Delete public_html and symlink public to it
  2. Put them inside public_html and use an htaccess file to point to the public folder
1 like
oka_'s avatar
Level 1

@Sinnbeck I divide my laravel project into 2 folders (i do this so the user can't check out my .env file):

  1. laravel (all laravel files except public)
  2. public (I moved all the contents of the file to public_html)

Based on your second solution, do I have to move the laravel folder to public_html? then do something with htaccess file?

Sinnbeck's avatar

@oka_ Yeah move everything into public_html and add a htaccess file that tell its to use /public as root. And double check that you cannot access .env or /storage as they will be left open (but can be denied in htaccess)

Something like

RewriteEngine On 

#Rewrite everything to https
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

#Rewrite everything to subfolder 
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/public
Rewriterule ^(.*)$ public/ [L]
oka_'s avatar
Level 1

@jlrdw is the user can see my .env file if I set it back to the correct structure?

oka_'s avatar
Level 1

@Sinnbeck yes, it's like this:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Send Requests To Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

Sinnbeck's avatar

@oka_ As far as I know this htaccess should deny all folders and files that isnt /public (I dont use shared hosting myself, so I just borrowed the example from another thread)

Sinnbeck's avatar

@oka_ That looks like the one from /public? Do you have the same file in both places?

oka_'s avatar
Level 1

@Sinnbeck /home/myWebsite

+...

+...

-public_html
	(there's only one htaccess file)

+...

It's my routes I guess, so there's only one htaccess file on it. (htaccess & index.php is in the same folder)

oka_'s avatar
Level 1

@Sinnbeck I wonder about the structure of the /public you said, it's public/public_html or public_html/public ?? I'll try to make it as a root, but I'm getting confused about the structure

Sinnbeck's avatar

@oka_ The way my suggestion 2 is

- public_html
   - new .htaccess from above
   - ... all of your laravel files and directories unedited

So its /public_html/public. But as I said, all files and folders. Not just public

oka_'s avatar
Level 1

@Sinnbeck Okay I did that, now I got an internal error like this (i guess the problem is on my htaccess file now):

Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator at (my webmaster mail) to inform them of the time this error occurred, and the actions you performed just before this error.

More information about this error may be available in the server error log.

Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.
kiri1101's avatar

@oka_ I suggest you use git for your hosting problem. Here is what you do

  1. Edit the .gitignore file in your project root directory to avoid file conversion errors during push
  2. Push your project to a repository in git
  3. Access your shared hosting account through SSH and in the terminal move to your root directory
  4. Make a git pull and edit your .env file directly in your terminal
  5. Then you can run the series of artisan commands stated here: https://laravel.com/docs/9.x/deployment.

I'll drop a link to a YouTube tutorial which may help here: https://www.youtube.com/watch?v=X6t7xw1HU-s&t=7s

Please or to participate in this conversation.