ChatGPT解决这个技术问题 Extra ChatGPT

No Application Encryption Key Has Been Specified

I'm new to Laravel and I'm trying to use the Artisan command...

php artisan serve

It displays...

Laravel development server started: http://127.0.0.1:8000

However, it won't automatically launch and when I manually enter http://127.0.0.1:8000 it shows this error:

RuntimeException No application encryption key has been specified.

Any ideas? I'm using Laravel framework 5.5-dev.

or if your .env file needs to be changed ref: stackoverflow.com/questions/36276767/…
The answers below are correct: however, if you first run "php artisan serve" and have an active web server session you will need to restart the server (control-c in the terminal to stop) to get the change to take effect after you add the key to your .env file.

b
brianlmerritt

From Encryption - Laravel - The PHP Framework For Web Artisans:

"Before using Laravel's encrypter, you must set a key option in your config/app.php configuration file. You should use the php artisan key:generate command to generate this key"

I found that using this complex internet query in google.com:

"laravel add encrption key" (Yes, it worked even with the typo!)

Note that if the .env file contains the key but you are still getting an application key error, then run php artisan config:cache to clear and reset the config.


I googled the whole error and nothing apeared, just googled set encriptyon key and found out. Feel like a fool. Thanks. For anyone who arrives here just run: php artisan key:generate
For anyone else that ran into my issue input this into the terminal cp .env.example .env php artisan key:generate
php artisan key:generate does not even work for me. Gives me the same error No application encryption key has been specified, please any solution?
This worked for me, but I first had to rename the file .env.example to .env for this to work, otherwise I was getting ".env: no such file" error.
Works, but first you need to create an empty .env file, and then copy the contents of .env.example, to your .env file. The .env.exampe file has an 'APP_KEY=' line, where 'APP_KEY' is the key, and nothing is specified as the value. If you don't have the the 'APP_KEY=' line, it seems it will not add the generated key to the .env file. I also then had to run 'php artisan config:cache' and then restart the server, for it to work.
L
Leonid Dashko

In my case, I also needed to reset the cached config files:

php artisan key:generate
php artisan config:cache

What is php artisan config:cache for? Why is it useful?
@Pathros to create/regenerate the cached config files
In addition to this, first, ensure that your ".env" file at least contains the empty key-option, like: APP_KEY=
Or, for local development use php artisan config:clear, and it won't generate a new cached version.
php artisan config:cache is what I missed. I've typed it in and the very same moment this page loaded :)
s
skm

Open command prompt in the root folder of your project and run below command:

php artisan key:generate

It will generate Application Key for your application.

You can find the generated application key(APP_KEY) in .env file.


A
Adam Pery

Copy .env.example to .env: cp -a .env.example .env Generate a key: php artisan key:generate Only then run: php artisan serve


Whoops. This was the critical step! A fresh Laravel project needs a .env file or nothing will work. Thanks, Adam.
U
Udhav Sarvaiya

Simply run this command:

php artisan key:generate

Yes but many hosting server do not have command line access then how to do?
a majority support ssh connection. from your terminal run ssh yourusername@server_ip
K
Kamran Syed

cp .env.example .env if there is no .env file present. php artisan key:generate command works for me. It generates the encryption key


clear the cache using php artisan config:cache command after the above steps.
Cache clearing would mostly be required of the production / staging but not during development. (Unless you like to cache your views during development).
H
Hashmat

Open command prompt in the root folder of your project and run

php artisan key:generate

Then

php artisan config:cache

and Then

If you're getting the same error after having key-value, then just copy the APP_KEY value from .env file and paste it to config/app.php with 'key' => 'YOUR KEY',

and then again run

php artisan config:cache

S
Sᴀᴍ Onᴇᴌᴀ

I actually had to add a .env file to my project and then copy the contents of .env.example so that the key:generate would work. Not sure why a .env file was not created when I started the project.


It might depend on how you installed Laravel ... "In a fresh Laravel installation, the root directory of your application will contain a .env.example file. If you install Laravel via Composer, this file will automatically be renamed to .env. Otherwise, you should rename the file manually." -#Environment Configuration
also, if you cloned the project from a repo where the .env file was likely in .gitignore, then you would neeed to create it as well. From the Laravel docs: 'Your .env file should not be committed to your application's source control, since each developer / server using your application could require a different environment configuration'
I also found that if you download it from the cloud (I'm using windows) i.e. google-drive, sky-drive etc. the .env, .env.example files are named _env, _env.example etc. In that case I renamed the files accordingly and then just ran 'php artisan key:generate'.
J
José Maria
php artisan key:generate
php artisan config:cache

worked for me, but it had to be done in a command prompt on Windows.

Doing it inside the terminal in PHPStorm didn't worked.


U
Udhav Sarvaiya

A common issue you might experience when working on a Laravel application is the exception:

RuntimeException No application encryption key has been specified.

You'll often run into this when you pull down an existing Laravel application, where you copy the .env.example file to .env but don't set a value for the APP_KEY variable.

At the command line, issue the following Artisan command to generate a key:

php artisan key:generate

This will generate a random key for APP_KEY, After completion of .env edit please enter this command in your terminal for clear cache:php artisan config:cache

Also, If you are using the PHP's default web server (eg. php artisan serve) you need to restart the server changing your .env file values. now you will not get to see this error message.


D
Darlan Dieterich

In 3 steps:

Generate new key php artisan key:generate

Clear the config php artisan config:clear

Update cache php artisan config:cache


U
Udhav Sarvaiya

Follow this steps:

php artisan key:generate php artisan config:cache php artisan serve


even after generating key in .env file i was getting error. When i ran "php artisaan config:cache" it cleared cache and then only my problem solved. Thanks a lot.
Remember to point to the database and close the .env file. If the .env file is open the error will continue.
P
Prakash Pazhanisamy

Okay, I'll write another instruction, because didn't find the clear answer here. So if you faced such problems, follow this:

Rename or copy/rename .env.example file in the root of your project to .env.

You should not just create empty .env file, but fill it with content of .env.example.

In the terminal go to the project root directory(not public folder) and run

php artisan key:generate

If everything is okay, the response in the terminal should look like this

Application key [base64:wbvPP9pBOwifnwu84BeKAVzmwM4TLvcVFowLcPAi6nA=] set successfully.

Now just copy key itself and paste it in your .env file as the value to APP_KEY. Result line should look like this:

APP_KEY=base64:wbvPP9pBOwifnwu84BeKAVzmwM4TLvcVFowLcPAi6nA=

In terminal run

php artisan config:cache

That's it.


So php artisan key:generate didn't automatically add the key to your .env file?
No, in my case.
D
Diligence Vagere

I found that most answers are incomplete here. In case anyone else is still looking for this:

Check if you have APP_KEY= in your .env, if not just add it without a value. Run this command: php artisan key:generate. This will fill in the value to the APP_KEY in your .env file. Finally, run php artisan config:cache in order to clear your config cache and recache your config with the new APP_KEY value.


P
PHP Laravel Developer

Simply run command php artisan key:generate.. Still issue exist then run one more command php artisan config:cache and php artisan cache:clear ..

Now run php artisan serve


P
Prakash Pazhanisamy

You can generate Application Encryption Key using this command:

php artisan key:generate

Then, create a cache file for faster configuration loading using this command:

php artisan config:cache

Or, serve the application on the PHP development server using this command:

php artisan serve

That's it!


T
Top-Master

If you git clone some project then this kind of issue may usually occur.

make sure there is .env file run php artisan key:generate and then it should generate APP_KEY in .env finally run php artisan serve and it should be working.


H
Hedayatullah Sarwary

If you don't have a .env file then run the below command, else skip this

cp .env.example .env

Then run the below artisan command and it will generate an application key for your project:

php artisan key:generate

Note: Your APP_KEY is inside your .env file.


t
temo

If after running php artisan key:generate issue is not resolved, check your .env file.

Search for APP_KEY=.

If it doesn't exist, manually add it to .env file and run php artisan key:generate again.

After this you will see generated key in .env file.

Copy that key and paste it in /config/app.php (search for APP_KEY there as well). You should end up with something like this in app.php file

'key' => env('APP_KEY', 'base64:...'),

Then run php artisan serve (You might have to run php artisan config:cache at some point. Not 100% sure when)


J
JJ Rohrer

I ran into this issue when I manually copied the contents of my Laravel project (say sites/oldname) into a new directory on my Mac (say, sites/newname). Since I was manually dragging and droppping, it didn't grab the hidden files, namely, '.env'. When I looked more closely at sites/oldname I saw .editorconfig, .env, .env.example, .gitatrributes, .styleci.yml, etc.

The error went away once I copied the hidden files to the new directory.

So, "No Application Encryption Key Has Been Specified" is Laravel speak for "your .env file is missing."


i
imshashi17

Sometimes If everything Fails Use this:

Goto: laravelProject/config/app.php

Find the line: 'key' => and check to what it refers,

It can either be one of two:

Case 1: env('APP_KEY') Case 2: "somekeystring"

For Case 1: Goto your .env file after you have run cp -a .env.example .env Enter a random string like 10101010101010101010101010101010

Now, run php artisan key:generate

Your key will be updated automatically.

For Case 2: set a random string like for value of Key 10101010101010101010101010101010

Now, run php artisan key:generate

Your key will be updated automatically.


D
Dimuthu

simply run

php artisan key:generate

its worked for me


S
Sarthak Raval

https://i.stack.imgur.com/IJ3ai.png

Click

Genrate app key

https://i.stack.imgur.com/fQBFs.png

Click on Refresh now


J
Joel Peltonen

I had to restart my queue worker using php artisan queue:restart after running php artisan key:generate to get jobs working.


关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now