php 如何解决错误“[ErrorException] file_get_contents(/var/www/laravel/.env): failed to open stream: No such file or directory”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36761778/
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-08-26 00:55:26  来源:igfitidea点击:

How to resolve the error "[ErrorException] file_get_contents(/var/www/laravel/.env): failed to open stream: No such file or directory"?

phplaravelubuntu-14.04file-get-contentslaravel-5.2

提问by PHPFan

I'm using Ubuntu 14.04on my machine. I installed composer and then laravelin the document root i.e. /var/www

我在我的机器上使用Ubuntu 14.04。我安装了composer,然后在文档根目录中安装了laravel,即/var/www

I also gave -R 777persmission to folder laravelpresent in directory /var/www

我还向/var/www目录中的文件夹laravel授予了-R 777权限

Then I go to directory laravelusing cd /var/www/laraveland run the following command php artisanand I got to see all the available commands there.

然后我使用cd /var/www/laravel进入目录laravel并运行以下命令php artisan,我看到那里所有可用的命令。

Then I typed in php artisan key:generateand got the error

然后我输入php artisan key:generate并得到错误

[ErrorException]  file_get_contents(/var/www/laravel/.env): failed to open stream: No such file or directory

enter image description hereHere I got stuck actually, can someone please help me in this regard?

在此处输入图片说明在这里我实际上卡住了,有人可以在这方面帮助我吗?

Thanks.

谢谢。

回答by Vinod VT

Rename .env.exampleto .envin your laravel root folder

在你的 Laravel 根文件夹中重命名.env.example.env

回答by Peter

The .envfile is not yet present because you will first need to create and configure it.

.env文件尚不存在,因为您首先需要创建和配置它。

Do the following

请执行下列操作

# Navigate to the correct folder
$ cd /var/www/laravel

# Copy the example file to make a .env file
$ cp .env.example .env

# Set the parameters
$ vi .env

回答by kejsu

Rename .env.example to .env and fill all properties. https://laravel.com/docs/5.0/configuration#environment-configuration

将 .env.example 重命名为 .env 并填充所有属性。 https://laravel.com/docs/5.0/configuration#environment-configuration

回答by Chonchol Mahmud

Probably you missed your .envfile in laravel project folder.So make .env.exampleto .envfile. Also give the required database connection.

也许你错过了.env在laravel项目文件folder.So让.env.example.env文件中。还要给出所需的数据库连接。

.envfile look like this: (Fill up with required database connection)

.env文件看起来像这样:(填写所需的数据库连接)

APP_ENV=local
APP_DEBUG=true
APP_KEY=SomeRandomString
APP_URL=http://localhost

DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

Hope this will help you.Thanks.

希望这会帮助你。谢谢。

回答by Steve Junior

You can create it & rerun the command.

您可以创建它并重新运行命令。

# cd /var/www/laravel 
# cp .env.example .env       //renames .env.example to .env
# php artisan key:generate 
Application key set successfully.

回答by jeff-h

If, like me, you didhave a .envfile, you may find it has permissions that are too tight to allow your current user to write to it (and by implication the php artisan command your current user is attempting to run). I had changed all my Laravel files to be owned by www-data:www-dataand made my current user a member of the www-datagroup, and was thus a little stumped by this error.

如果像我一样,你确实有一个.env文件,你可能会发现它的权限太紧,不允许你的当前用户写入它(暗示你当前用户试图运行的 php artisan 命令)。我已将我所有的 Laravel 文件更改为归www-data:www-data我所有,并使我当前的用户成为该www-data组的成员,因此这个错误有点难倒。

However, I soon realised that my .envfile has the following permissions:

但是,我很快意识到我的.env文件具有以下权限:

-rw-r--r--

-rw-r--r--

...meaning the user which owns the file gets read-write, but the group and world can only read. Since my current user is a member of the groupwww-data, it can only read, not write.

...意味着拥有文件的用户可以读写,但组和世界只能读取。由于我当前的用户是该的成员www-data,因此它只能读取,不能写入。

(You can check your file permissions by doing $ ls -la)

(您可以通过执行检查您的文件权限$ ls -la

If you have the same situation, you have two choices; loosen the file permissions on that file (with chmod) or use sudoto run your php artisan commands. I chose the latter, since this is a production server for me and I like the tight permissions.

如果你有同样的情况,你有两个选择;放宽对该文件的文件权限(使用chmod)或用于sudo运行您的 php artisan 命令。我选择了后者,因为这是我的生产服务器,我喜欢严格的权限。