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

8

30

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

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

Then I go to directory laravel using cd /var/www/laravel and run the following command php artisan and I got to see all the available commands there.

Then I typed in php artisan key:generate and got the error

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

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

Thanks.

Sudorific answered 21/4, 2016 at 6:51 Comment(2)
Check whether there is a .env file in your project root. Normally there would be a .env.example file which you need to change to .env file with your own settingsUnbound
check whether .env exist in /var/www/laravel. It may be hidden. If not exist, rename .env.example to .env.Mott
A
59

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

Aerodyne answered 21/4, 2016 at 6:56 Comment(1)
for me exist both file and i removed .env.example and updated .env but getting same error.Whitsuntide
W
6

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
Withstand answered 21/4, 2016 at 7:8 Comment(0)
S
6

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.
Stopoff answered 20/4, 2019 at 20:52 Comment(0)
C
4

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

Cohort answered 21/4, 2016 at 6:54 Comment(0)
M
3

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.

Mccallion answered 21/4, 2016 at 6:58 Comment(0)
P
3

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.

Pertinacity answered 20/2, 2017 at 9:3 Comment(1)
Allowing a web server to own (i.e. write to) your code and configuration files is not "tight permissions" at all. Files should be owned by another user and www-data only given read permissions, except for the storage directory. It doesn't even need read access to .env in a production environment, since configs should be cached with artisan config:cache before serving.Usage
A
0

Might be someone is looking for the answer so here is the answer by which I solved the issue Normally copying the .env.example file and then renaming it ,does not work so here is the suggestion Dont do in that way Just do cp .env.example .env in the command lin

and then change the db name Or Password etc

Antagonistic answered 29/9, 2021 at 17:27 Comment(0)
F
0

Never change The permission to 777

Make a Copy of the .env.example file:

cp .env.example .env

Make sure it has the same user and group as the other files:

ls -al
-rwxr-xr-x  1 user user     778 Dec  7 13:29 .env
-rwxr-xr-x  1 user user    778 Dec  7 13:25 .env.example

If it is a different user and group change it using:

sudo chown -R user:usergroup .env

Now it should work.

Fumarole answered 7/12, 2022 at 10:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.