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

荒凉一梦 提交于 2019-11-29 03:14:37

Rename .env.example to .env in your laravel root folder

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

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

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

Probably you missed your .env file in laravel project folder.So make .env.example to .env file. Also give the required database connection.

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

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.

If, like me, you did have a .env file, 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-data and made my current user a member of the www-data group, and was thus a little stumped by this error.

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

-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 group www-data, it can only read, not write.

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

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

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.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!