Set location of laravel .env

Viewed 23527

So I just published a build of laravel to my production server. But the .env file was readable when I uploaded it. So I uploaded it to root of my site next to the public_html/ directory.

My question is: How to tell laravel where the .env file is located? It worked when I had it in the public_html/ folder but how do I tell it to look in the root folder?

6 Answers

Set env path in bootstrap/app.php:

$app->useEnvironmentPath($env_path);

for example, directory layout for my project:

 webapp 
 -laravel 
 -public
 -.env

Custom path to env file

$app->useEnvironmentPath(
  dirname(__DIR__, 2)
);

This way you can access .env file from other location, but it is not good for security.

your-project/bootstrap/app.php

$app = new Illuminate\Foundation\Application( realpath(DIR.'/../') );

$app->useEnvironmentPath(base_path('foldername/'));

add this code in .htaccess file for security

<Files .env>
order allow,deny
Deny from all
</Files>

I ended up setting a symlink from public to public_html and everything worked as expected

Add code in bootstrap/app.php

$app = new Gecche\Multidomain\Foundation\Application(
    $_ENV['APP_BASE_PATH'] ?? dirname(__DIR__),
    dirname(__DIR__) . DIRECTORY_SEPARATOR . 'envfolder'
);
Related