Introducing Livops 2.0! Promo codeCEOgives you 8,000 so‘m on your balance.What’s new

Laravel: No application encryption key has been specified

Laravel's encrypter has no key, because the APP_KEY environment variable is empty on the server. Generate one with php artisan key:generate --show, set it as APP_KEY where the app runs, clear any cached configuration, and keep the same key from then on.

By Livops team, the people who build and run LivopsUpdated

The exception, as Laravel shows it on every page that uses cookies or the session:

Illuminate\Encryption\MissingAppKeyException
No application encryption key has been specified.

This page explains which key is missing, lists the causes from the most common, shows how to check the key your app really loads, and says what Livops does for Laravel projects.

What does the error mean?

Laravel encrypts cookies, including the session cookie, with the key in the key option of config/app.php, and that option is driven by the APP_KEY environment variable[1]. The key is normally generated during Laravel’s installation[1], into the .env file. .env is not committed, so a fresh server starts with no key, and every request that touches encryption fails with this exception.

Causes, most common first

1. APP_KEY is not set on the server

Generate a key once, with the command Laravel recommends, because it uses PHP’s secure random generator[1]. --show prints it instead of writing it into .env:

php artisan key:generate --show
# prints base64: followed by 44 characters

Set the printed value as APP_KEY in the server’s environment, and keep it. Changing the key logs every user out, and values encrypted with the old key can no longer be decrypted; if you must rotate, list the old ones in APP_PREVIOUS_KEYS[1].

2. The configuration was cached without the key

php artisan config:cache belongs in a production deployment[2][3]. Once the configuration is cached, .env is not loaded, and env() returns only real system environment variables[2]. If the cache was made during a build, or before APP_KEY was set, it holds an empty key. Clear it, and cache it again where the key exists:

php artisan config:clear
php artisan config:cache

3. Code calls env() outside the config files

After config:cache, every env() call for a .env variable returns null; only calls inside the configuration files are safe[3]. A package or your own code that reads env('APP_KEY') directly breaks the moment the config is cached. Read config('app.key') instead.

4. The value is empty or malformed

APP_KEY= copied from .env.example, quotes around it, or a key without the base64: prefix. An empty value gives this error; a wrong length gives Unsupported cipher or incorrect key length. Generate a fresh one as in cause 1.

How to confirm the fix locally

Check the key the application really loads, not the one in a file:

php artisan config:clear
APP_KEY="base64:..." php artisan tinker --execute="var_dump(strlen(config('app.key')) > 0);"

bool(true) means the key is there. To reproduce the server, remove APP_KEY from .env and open any page: you get the exception; put it back and it is gone.

On Livops

  • The key is generated when the project is created. When Livops detects a Laravel project (artisan beside composer.json, built without your own Dockerfile) and creates it, it generates APP_KEY the way key:generate does — base64: and 32 random bytes — and saves it as a secret variable, unless you gave a value. Projects created from a stack get one too.
  • The config is cached where the variables are. The start command runs php artisan optimize as the app starts, with the project’s variables in place, so cause 2 does not happen with Livops’ own start command.
  • So on Livops this error usually means one of three things: the project builds from your own Dockerfile (no key is generated for it), APP_KEY was removed or emptied in Variables, or your Dockerfile caches the config during the build. Set APP_KEY under Variables, mark it secret, and deploy.
  • The deploy log warns you. After a deploy goes live, Livops opens the home page. If it answers with a server error, the deploy log says the app is running but its home page answers 500, with the app’s last log lines under it.
  • The assistant explains, it does not invent keys. It can read the log and point you to Variables. It sets a variable only to a value you wrote in the same message, and only after you press its card.

More on missing variables: A variable is not set.

Questions

Why does it work locally?

Laravel's installer wrote APP_KEY into your local .env file. That file is not committed, so the server never got the key.

Can I generate a new key on every deploy?

No. Changing the key logs out every user and makes values encrypted with the old key unreadable. Generate it once, keep it in the server's environment, and list old keys in APP_PREVIOUS_KEYS if you ever rotate.

I set APP_KEY and still see the error. Why?

The configuration was probably cached before the key existed. A cached config does not read .env, so run php artisan config:clear, or cache it again in an environment that has the key.

What format does APP_KEY have?

base64: followed by 32 random bytes in base64, the way php artisan key:generate writes it. A key without the prefix or of the wrong length gives a different error, Unsupported cipher or incorrect key length.

Does Livops set APP_KEY?

Yes, when it detects a Laravel project and creates it, unless you gave a value. It generates the key the way key:generate does and saves it as a secret variable.

Sources

  1. Laravel docs — Encryption (laravel.com). Checked .
  2. Laravel docs — Configuration, configuration caching (laravel.com). Checked .
  3. Laravel docs — Deployment (laravel.com). Checked .

Put your project online today

Connect GitHub or drop a folder. No card is asked for.

Deploy your Laravel app

Got a question? — @livops_uz