Set location of laravel .env
Asked Answered
S

6

10

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?

Sundaysundberg answered 21/3, 2016 at 9:47 Comment(0)
S
0

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

Sundaysundberg answered 28/5, 2018 at 8:38 Comment(0)
W
18

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)
);
Willmert answered 24/5, 2018 at 14:14 Comment(0)
T
1

What you need to upload to public_html is contents of public directory in Laravel installation including any JavaScript files, CSS files or images that should be accessible to client, everything else should be placed out of public directory.

Tanatanach answered 21/3, 2016 at 9:52 Comment(0)
M
0

As you can read in the official documentation, the .env file must be in the root directory of your Laravel app. There's no way to change the file location (and I think there's no point too).

Moreover, the root folder SHOULDN'T be a public folder, as .env shouldn't be exposed to a public access, otherwise the main security aim of it would be completely lost.

Majormajordomo answered 21/3, 2016 at 11:14 Comment(2)
Yes, but that's what I did and now it can't find the .env file while it worked in the public directory. how should I proceed?Sundaysundberg
although there may be no need to change location, this could be helpful when you are building a sample application with multiple backends (laravel, slim, express, etc.) and you wish to put each back end framework in it's own folder. To avoid redundancy, one would wish to put one .env file in a root folder above these folders.Drumstick
A
0

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>
Adamsen answered 10/2, 2018 at 10:2 Comment(1)
What exactly does the htaccess file do? Deny requests from all client requests except my own server?Aguiar
S
0

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

Sundaysundberg answered 28/5, 2018 at 8:38 Comment(0)
C
0

Add code in bootstrap/app.php

$app = new Gecche\Multidomain\Foundation\Application(
    $_ENV['APP_BASE_PATH'] ?? dirname(__DIR__),
    dirname(__DIR__) . DIRECTORY_SEPARATOR . 'envfolder'
);
Counterspy answered 17/11, 2021 at 16:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.