I was wondering if any of you know of a way to install Laravel 4 in a web host SUBDIRECTORY / subfolder while not exposing the /app/ folder and other sensible files to the publicly accessible part of the host.
The idea is, I'd be able to access http://mydomain.example/mylaravel/
to be able to use Laravel, but at the same time I want to avoid anyone doing something like going to http://mydomain.example/app/
or http://mydomain.example/mylaravel/app/
and basically being able to see my config files and other code.
Go inside the File Manager, you will see directories of files and folders there. Next, compress your Laravel project folder that you want to deploy to your CPanel as a . zip file and upload it in the root directory. After your project folder is uploaded, extract it in the root directory.
The files and folders in laravels public folder are meant to be web accessible. For security, all other files and folders in the laravel framework should not be web accessible. Moving the index. php to laravels root will break the framework and defy best practices.
So I figured out how to do this. I'll explain with an example.
Suppose you a domain, http://domain.example
. Here's an example of the structure you might be using:
domain.example/ (the root of your web hosting)
|-- yourlaravel4_base/
|-- [some other folders...]
|-- public_html/ (where your html files and such go)
| |-- [some other folders...]
| |-- yourlaravel4/
/public_html/
is the root of the publicly accessible part of your web hosting files.
You want to create a subfolder in /public_html/
(in this case /public_html/yourlaravel4/
). In this subfolder you will store all the contents of the Laravel 4 public/
folder.
Now, for the rest of the files. You have to go to the root of your web hosting files, that is, you wanna be at domain.example/
level, therefore being able to see public_html/
and some other folders. Then, we need to create a folder here, where Laravel 4's base files will be stored. In this case, it's domain.example/yourlaravel4_base/
. Inside yourlaravel4_base/
we need to store every file and folder that exists in the base Laravel 4 directory. That would be app/
, bootstrap/
, vendor/
, server.php
, etc. Everything EXCEPT the /public/
folder, whose contents you already stored in public_html/yourlaravel4/
.
Finally, we need to edit 2 files: Laravel's /bootstrap/paths.php
and /public/index.php
.
In the paths.php
file, replace:
'app' => __DIR__.'/../app',
with:
'app' => __DIR__.'/../../yourlaravel4_base/app',
In the paths.php
file, replace:
'public' => __DIR__.'/../public',
with:
'public' => __DIR__,
In the paths.php
file, replace:
'base' => __DIR__.'/..',
with:
'base' => __DIR__.'/../../yourlaravel4_base',
In paths.php
, replace:
'storage' => __DIR__.'/../app/storage',
with:
'storage' => __DIR__.'/../../yourlaravel4_base/app/storage',
In index.php
, replace:
require __DIR__.'/../bootstrap/autoload.php';
with:
require __DIR__.'/../../yourlaravel4_base/bootstrap/autoload.php';
In index.php
, replace:
$app = require_once __DIR__.'/../bootstrap/start.php';
with:
$app = require_once __DIR__.'/../../yourlaravel4_base/bootstrap/start.php';
Upload changes. Now you should be able to have Laravel 4 installed in a subfolder in your website without actually exposing the app/
folder and other sensitive files. :)
You can use an alias: no hankey-pankey with widespread directories on your system.
Specify in your httpd.conf or admin-panel:
/mylaravel/ /path/to/laravel/public
Furthermore, specify the URL in your Laravel's app.php
Line 29
'url' => 'http://yourdomain.example/mylaravel/',
Eventually set the RewriteBase in your /public/.htaccess
RewriteBase /mylaravel/
Works like a charm and keeps your directory structure clean and to the point!
People, people! No need to over-complicate things!
(For other public folders, such as htdocs
or www
just replace public_html
in the following with your one.)
app
folder is next to the public_html
folder)public
to public_html
(be sure to also move hidden files, such as .htaccess
!)public
folderIn bootstrap/path.php
change
'public' => __DIR__.'/../public',
to
'public' => __DIR__.'/../public_html',
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With