Laravel 5 Structure for document root, public_html and sub-folder/sub-directory based access on VPS and Shared Hosting
Date: 2017-Nov-20
It becomes really annoying when you have to deal with these age old shared hosting servers. Yeah they might have PHP 7, but that doesn't do any justice if we can't change the Document Root to the one needed by our project.
Learning through various places guides us to only put public
files in the web/public folder. Which means setting the Document Root. But many of our ugly old shared hosting sites like godaddy, hostgator etc. do not let us set this simple document root for our primary domain, yet they allow when you add sub-domains (??? confused :S). I wish we could simply change these hostings to some VPS like digitalocean, but alas, your company boss not interested to change hosting. So, yeah, dealing with this problem for ages. And I HATE IT!!
Many of us Laravel developers have somehow managed to run laravel in these shared hosting sites, but what we usually do not take into consideration is the fact that we are also exposing our app codes and most importantly, our .env file filled with secrets. So with this, I wanted to make a structure & configuration in which our laravel app will:
- Will not change the way we develop laravel.
- Less hassle to deploy to VPS or Shared Hosting.
- Work on sub-folder / sub-directory for Shared Hosts (for Apache Web Server).
- Ensure our app codes and env file is secured.
So lets see what kind of format and modifications we do to achieve this.
PS. Many of these info, guide, settings can already be found throughout internet. I compiled all of them in one place and added my own flavor to make it look & function good.
..WIP..
Here is how our final folder structure would look something similar like.
Root
|- myapp/
| |- app/
| |- bootstrap/
| |- config/
| |- database/
| |- mobules/
| |- resources/
| |- routes/
| |- storage/
| |- vendor/
| |- .env
| |- .htaccess
| |- artisan
| |- composer.json
| |- package.json
| |- server.php
| ...
|
|- public_html/
| |- assets/
| |- modules/
| |- themes/
| |- .htaccess
| |- favicon.ico
| |- index.php
| |- robot.txt
|
|- .htaccess
|- index.php
There could be some incompatibility with some third party packages, mainly for the assets, so you might have to update those by modifying or extending.