62

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.

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Emmanuel Figuerola
  • 1,620
  • 1
  • 15
  • 23

9 Answers9

65

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. :)

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Emmanuel Figuerola
  • 1,620
  • 1
  • 15
  • 23
  • 1
    Only files I needed to edit to get up and running were the config/app.php file, pointing the url to the location of the folder containing the public folder contents and the /public/index.php file paths as suggested. Tricky part is knowing how many "../../" to add. Also no slash after the quote (yes) __DIR__.'../ as suggested (no)__DIR__.'/../ – Duramba Sep 07 '13 at 05:52
  • Very well answered, thanks. For my case, however, it was an incomplete solution without the contribution from @TechyTimo By the way, why is this answer (highest votes, and accepted answer) showing lowest on the list of answers? – Hamman Samuel May 07 '14 at 16:50
  • Unfortunately this doesn't always work if your host does some silly rewrite base stuff and even though the root directory exists, you can't access it from the webapp. [This](http://driesvints.com/blog/laravel-4-on-a-shared-host) is a good summary of the different options, including this one. – Rob Grant Feb 05 '15 at 12:11
  • @EmmanuelFiguerola I have done everything you said and I got `Warning: require(/home/u962269517/public_html/vendor/swiftmailer/swiftmailer/lib/swift_required.php): failed to open stream: No such file or directory in /home/u962269517/public_html/vendor/composer/autoload_real.php on line 58 Fatal error: require(): Failed opening required '/home/u962269517/public_html/vendor/swiftmailer/swiftmailer/lib/swift_required.php' (include_path='/home/u962269517/public_html/vendor/phpseclib/phpseclib/phpseclib:.:/usr/lib/php') in /home/u962269517/public_html/vendor/composer/autoload_real.php on line 58` – alexventuraio Apr 06 '15 at 21:10
  • 1
    Just curious, will it work for Laravel 5? I am planning for L5 – VijayRana May 08 '17 at 08:01
28

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!

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Tom
  • 3,281
  • 26
  • 33
  • This seems like the actual answer. – Dan Sep 09 '14 at 03:16
  • And if for example you access a page that do not exists, what you will get? The laravel 'Whoops, looks like something went wrong.' message, an internal server error or a not found page? Because it should return the laravel message. – paulalexandru Oct 15 '14 at 14:07
  • 1
    Specify in your httpd.conf or admin-panel /mylaravel/ /path/to/laravel/public - Can you tell me how it's done? – Gaurav Gupta Apr 21 '16 at 08:00
7

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.)

  1. Put all your Laravel stuff in the top-level folder (so that the app folder is next to the public_html folder)
  2. Move all files from public to public_html (be sure to also move hidden files, such as .htaccess!)
  3. Delete the now empty public folder
  4. In bootstrap/path.php change

    'public' => __DIR__.'/../public', to

    'public' => __DIR__.'/../public_html',

  5. Done.
sisou
  • 323
  • 2
  • 7
  • 2
    That's what I do.. bullet proof, right? If you don't want `app/` to be visible inside your public directory, **don't** put it inside your public directory! – Adam Marshall Feb 11 '15 at 13:15
  • For GoDaddy Shared Hosting: https://medium.com/@kunalnagar/deploying-laravel-5-on-godaddy-shared-hosting-888ec96f64cd – PhatHV Aug 10 '15 at 08:17
  • 1
    This does not answer the question while the question is how to put it in a subfolder. – Trekdrop Apr 11 '16 at 10:29
3

emmanuel answer doesnt work for me but i did some changes to make it work . although i used my web root to be the source of a project . there it is ; i have htdocs folder as a root folder but you may have other, such as public_html , www or so on . you can easily replace my htdocs with the one you have .

fine .

my dircetory

root/ 
      htdocs
      .htaccess
      .override

first i solved all of laravel dependencies on my localhost on windows with composer . after that i did pack the whole package into laravel4.zip . in my htdocs i create a laravel folder and i did upload and unzip the zip file in it.

htdocs/
        laravel/
                 app
                 bootstrap
                 public 
                 vendors
                 ...

so after i did unzip the file i move everything in public folder into htdocs folder

htdocs/
        laravel
        packages
        .htaccess
        favicon.ico
        index.php
        robots.txt

now we have to rewrite some pathes in htdocs/index.php and htdocs/laravel/bootstrap/paths.php

but lets open htdocs/laravel/bootstrap/paths.php . you would see

'app' => __DIR__.'/../app',

what does it mean . it means that we are standing at htdocs/laravel/bootstrap/ path . and there for we need exit this folder to laravel folder then going for app folder . ok . this is the structure of the laravel for routing . if you get this right you can make your laravel run .

find

'public' => __DIR__.'/../public',

and change it to

'public' => __DIR__.'/../../htdocs',

(simple ha ) with one (../) of these we can go up one folder . so now we are at laravel folder . but we need to be in htdocs . so we have to write another (../) to get there .

open htdocs/index.php and find

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

change it into

require 'laravel/bootstrap/autoload.php'; 

require is a built-in php function so we are going to treat it as a simple php . we dont realy need laravel style for fetching autoload.php file . so simply remove it and go for classic style

find

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

change it into

$app = require_once 'laravel/bootstrap/start.php';
Hassan Gilak
  • 691
  • 9
  • 14
  • Glad to see Laravel working for you. The paths thing is tricky, I even had a project once in which for some strange reason I needed to use full paths to files. :/ – Emmanuel Figuerola Feb 08 '14 at 13:43
2

If you are trying to run Laravel 5.1 into a shared hosting space and all of the above answers seems to be a bit different from the actual Laravel files/folders or you are looking how to put your Laravel 5/5.1 into a sub directory on your shared hosting space so you can access it like this:

http://mywebsite.example/mylaravel/

First of all make sure you meet the Laravel 5.1 requirements:

  • PHP 5.5
  • PHP extension Mcrypt
  • PHP extension Mbstring
  • PHP extension OpenSSL

Here an easy two tutorials for you:

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Mohamed Salem Lamiri
  • 5,767
  • 6
  • 34
  • 47
1

If you are using add-on domain or subdomain wizard in cPanel, the wizard should ask you for document root.

For example, I want to add on droidman.example. The wizard should suggest that I make a droidman.example folder. In this step, tell the wizard to make droidman.example/public folder. Then put your application in droidman.example folder. Your host will look at public folder.

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Hassan Gilak
  • 691
  • 9
  • 14
1

If you want to simply setup Laravel Project on shared hosting and you have cPanel access then you can do following steps which I have followed and it worked well.

Step 1 : Make zip or tar file of your entire Laravel Application on your Local Machine.

Step 2 : Go to Cpanel. Upload zip/tar file to public_html directory and then extract it. (You can also create separate folder for your laravel application. As I want to upload more than one Laravel applications I have created separate directory for each one as shown in below image.)

enter image description here

Step 3 : Now go to 'Addon Domain' and change document root of your specified domain. Point the domain to public folder.

enter image description here

Now check your domain it will redirect to public/index.php file. There is no need to change any paths.

Sumit Patil
  • 556
  • 1
  • 5
  • 19
  • This one worked for me. Important to check if the current PHP version is 5.6, since the others prevent the laravel app from working altogether. – Cornwell Nov 06 '16 at 19:41
0

If you're using an apache server, the simplest way to do this is to host store your Laravel application outside of the www root (or in a protected folder in www root) then symlink the desired subfolder to point it the public folder in laravel

For example, I place my laravel installations in:

/usr/local/share/myLaravel

Then, in your public www folder, create a symlink as so:

ln -s /usr/local/share/myLaravel/public mylaravel

You'll need to make some adjustments to your installation, like putting your Rewrite Base to /mylaravel/ and make sure that your config files allow overrides in the base location, but this is simpler than modifying everything.

Troy
  • 961
  • 6
  • 13
  • 24
-4

Make sure to check your permissions. I deployed through the following although I don't recommend it.

I used SSH and did something like:

rm -rf public_html/ // WARNING: READ NOTE AT THE BOTTOM BEFORE RUNNING

ln -s project_folder/public/ public_html  // where the format is ln -s target name

Afterwards, I would receive a message like "Whoops, looks like something went wrong" (or an error depending on your debug settings). To fix this, I just had to change my permissions to 755 for my folders. reference: http://kb.webhostface.com/laravel/fix-permission-denied-error-laravel

Overall, I'm going to go back and try the original way with changed permissions as I find this route to be less secure, and more reliant on my hosting customer service.

*****Note***** This command obviously removes your public_html (also sometimes called public or www) folder. You need to have a backup of all the files in it because you are deleting it. Also, you will need to be prepared to go through customer service to restore it. Many hosting services don't allow you to simply recreate the folder on your own.