设置 Laravel .env 的位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36127616/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:27:13  来源:igfitidea点击:

Set location of laravel .env

phplaraveldeploymentlaravel-5.2env

提问by Sjoerd de Wit

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.

所以我刚刚发布了一个 Laravel 版本到我的生产服务器。但是 .env 文件在我上传时是可读的。所以我把它上传到我网站的根public_html/目录旁边。

My question is: How to tell laravel where the .envfile 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?

我的问题是:如何告诉 laravel.env文件所在的位置?当我将它放在public_html/文件夹中时它起作用了,但是我如何告诉它在根文件夹中查找?

采纳答案by Sjoerd de Wit

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

我最终设置了一个从 public 到 public_html 的符号链接,一切都按预期工作

回答by Aigars Matulis

Set env path in bootstrap/app.php:

bootstrap/app.php 中设置 env 路径:

$app->useEnvironmentPath($env_path);

for example, directory layout for my project:

例如,我的项目的目录布局:

 webapp 
 -laravel 
 -public
 -.env

Custom path to env file

env 文件的自定义路径

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

回答by Zamrony P. Juhara

What you need to upload to public_htmlis contents of publicdirectory 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.

你需要上传的public_htmlpublicLaravel 安装目录的内容,包括客户端应该可以访问的任何 JavaScript 文件、CSS 文件或图像,其他所有内容都应该放在公共目录之外。

回答by Stefano Ortisi

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

正如您在官方文档中所读到的,.env 文件必须位于 Laravel 应用程序的根目录中。无法更改文件位置(我认为也没有意义)。

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.

此外,根文件夹不应该是公共文件夹,因为 .env 不应该被公开访问,否则它的主要安全目标将完全丢失。

回答by Ramesh

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

这样你可以从其他位置访问 .env 文件,但不利于安全。

your-project/bootstrap/app.php

你的项目/引导程序/app.php

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

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

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

$app->useEnvironmentPath(base_path('文件夹名/'));

add this code in .htaccess file for security

将此代码添加到 .htaccess 文件中以确保安全

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