DEV Community

Md. Hazzaz Bin Faiz
Md. Hazzaz Bin Faiz

Posted on • Edited on

Linking public_html to laravel public directory in shared hosting

Putting laravel application in public_html directory is very dangerous.

User should land on public directory.
If you can edit the document root, update it from /home/user/public_html to /home/user/public_html/public

But some shared hosting provider forbids updating document root of main domain.

Some people stores application to other directory and copies the index.php file from public directory and update path.
It works fine, but if you are serving asset like css and js files from server and try to deploy updates by git, your server will server old contents unless you copy latest assets to public_html directory.

There is another trick, to do it and your assets will remain update to original asset and no need to copy index.php file.

Let's assume that you have laravel application in /home/user/laravel directory.

SSH to your server and cd to home by cd ~.

Back up public_html directory if you have any important file in it.

Delete public_html directory by running rm -rf public_html.

Now create a symbolic link with name public_html point to your laravel applications public directory by running
ln -s /home/user/laravel/public/ public_html

Now the public_html is just a symbolic link of your applications public directory.

If anything change in your applications public directory, it will immediately available for user.

Have Fun 🎉 !!!

Top comments (13)

Collapse
 
ghawsi profile image
Obaidullah Ghawsi

After linking the public to public_html I get a 403 Forbidden error.
I am using laravel 9
Everything else works fine but cant access the website.

Collapse
 
hazzazbinfaiz profile image
Md. Hazzaz Bin Faiz

I think directory permission is not correct.

Check directory permission.

Collapse
 
ghawsi profile image
Obaidullah Ghawsi

Which directory do I need to check for permission.
I have tried 775, 777, for public html, and all the symlinks.
I am not sure which one to set to which one.
If you can help me with that I would be thankful.
Thanks in advance

Thread Thread
 
hazzazbinfaiz profile image
Md. Hazzaz Bin Faiz

Check for permission as well as owner. Of current user is not an owner, it can cause forbidden issue.

Thread Thread
 
ghawsi profile image
Obaidullah Ghawsi

Current user is the owner. I have full access to the cpanel hosting.

Thread Thread
 
hazzazbinfaiz profile image
Md. Hazzaz Bin Faiz

You can check server log for the exact error log

Collapse
 
mdanowarhosen profile image
Anowar Hosen

after I do that my website including /index.php to all pages. Example www.domain.com/index.php/about,www.domain.com/index.php/contact. How do I fix that?

Collapse
 
hazzazbinfaiz profile image
Md. Hazzaz Bin Faiz

May be you are using url helper. Can't exactly say as I don't know that much detail about the context of your issue.

Collapse
 
misteryomi profile image
'Yomi Omotoso 🇳🇬 ✈ 🌍

Very helpful! Thank you.

Collapse
 
akashdas profile image
Akash Das

Here is another resource full article about Laravel project deployment in Shared Hosting
nihardaily.com/21-deploy-guideline...

Collapse
 
hazzazbinfaiz profile image
Md. Hazzaz Bin Faiz

You can deploy Laravel app by uploading whole code as defined in mentioned article. But for security reasons, we shouldn't expose laravel's root directory, we should only expose and serve from public directory.

Collapse
 
azomapache profile image
Azom Sarker

Very Nice Its Working. Sir.

Collapse
 
hazzazbinfaiz profile image
Md. Hazzaz Bin Faiz

Thanks