PHP and Laravel

No application encryption key has been specified: causes and fix

Laravel says no application encryption key has been specified. Learn the causes and the exact commands to fix an empty APP_KEY safely.

No application encryption key has been specified.

In short

This error comes from Laravel, the PHP framework. Its encrypter throws Illuminate\Encryption\MissingAppKeyException when the APP_KEY setting is empty or cannot be read. Laravel needs that key to encrypt cookies, sessions and other protected values, so it refuses to serve requests without it. The error reads the same in Laravel 8, 9 and later versions.

Likely causes

  • •The .env file is missing. Common after a fresh git clone or a new deploy, because .env is not committed. Run ls -la .env in the project root. If the file is not there, this is the cause.
  • •APP_KEY is present but empty. The .env file was copied from .env.example, which ships with a blank APP_KEY=. Run grep -n '^APP_KEY' .env. If you see APP_KEY= with nothing after it, this is the cause.
  • •A stale config cache. You fixed .env but the error stays. Laravel reads bootstrap/cache/config.php when it exists and ignores .env. If that file exists, the cached config still holds the old empty key.
  • •The environment never reaches the process. Typical in Docker, systemd, PM2 or Supervisor setups where .env is not copied into the image or the variable is not passed in. The site works when you run artisan on the host but fails in the container. Check with docker exec on the container and printenv APP_KEY.
  • •The web user cannot read .env. Artisan works as root or your own user, but PHP-FPM or Apache runs as another user (www-data on Debian and Ubuntu, nginx or apache on RHEL-family systems). Run ls -l .env and compare the owner and group with the PHP-FPM user.
  • •A malformed .env line. A space around the equals sign, a stray quote, or a hidden Windows line ending can stop the value being read. Run grep -n 'APP_KEY' .env | cat -A and look for odd characters such as ^M.

How to fix it

  1. 1. Check what the .env file holds

    Go to the project root and see whether .env exists and whether APP_KEY has a value. Do not paste the real key into tickets or chat.

    cd /var/www/example.com
    ls -la .env
    grep -n '^APP_KEY' .env
  2. 2. Create .env if it is missing

    Copy the example file, then fill in your database and app settings. Laravel will not start properly on the placeholder values alone.

    cp .env.example .env
  3. 3. Look for an existing key before you generate a new one

    Warning: if this site has ever run in production, a new key makes old sessions, encrypted cookies and any values stored with Laravel's Crypt or encrypted casts unreadable. Look in your last release's .env, backups or secrets manager first. If you find the old key, put it in .env as APP_KEY=base64:... and skip the next step.

  4. 4. Generate a key

    This writes a new APP_KEY into .env. In a production environment Laravel asks for confirmation, and --force skips the prompt. Use --show if you only want to print a key and store it in a secrets manager yourself.

    php artisan key:generate
    # production, no prompt:
    php artisan key:generate --force
  5. 5. Clear and rebuild the config cache

    A cached config keeps serving the old empty value. Clear it first. If you normally cache config in production, rebuild it after the key is in place.

    php artisan config:clear
    php artisan config:cache
  6. 6. Make .env readable by the web user

    Give the PHP-FPM or Apache group read access without making the file world-readable. Use the group that matches your system.

    # Debian / Ubuntu
    sudo chgrp www-data .env && sudo chmod 640 .env
    # RHEL / Rocky / Alma (use apache instead of nginx if needed)
    sudo chgrp nginx .env && sudo chmod 640 .env
  7. 7. Pass the variable into containers and services

    Do not bake .env into an image. In Docker, load it at run time. In Compose, add env_file: .env to the service. For systemd, PM2 or Supervisor, make sure the working directory is the project root so Laravel finds .env.

    docker run --env-file .env your-image:latest
  8. 8. Reload PHP and restart workers

    A reload is graceful: PHP-FPM workers finish their current requests before the new ones take over, so visitors should not notice. Use restart instead of reload only if you must, because a restart can interrupt requests in flight. Adjust the PHP version in the service name. Queue workers keep the old config in memory until they restart.

    # Debian / Ubuntu
    sudo systemctl reload php8.3-fpm
    # RHEL-family
    sudo systemctl reload php-fpm
    # workers
    php artisan queue:restart

Confirm it is fixed

The first command should print 1, which means .env holds a key in the expected base64 format. The second should return a 200 or a normal redirect instead of a 500. Then check that no new MissingAppKeyException lines appear in storage/logs/laravel.log.

grep -c '^APP_KEY=base64:' .env
curl -sI http://127.0.0.1/ | head -n 1
tail -n 20 storage/logs/laravel.log | grep -c MissingAppKeyException

How Opservo catches it early

The Opservo agent tails Laravel logs, groups repeated lines into normalised patterns (raw log lines are never stored), and raises a recurring error finding with a plain-English AI summary when an error like this keeps coming back.

Last updated 2026-11-30.

FAQ

Related questions

What is the application encryption key in Laravel?+

It is the APP_KEY value in .env. Laravel uses it to encrypt and sign cookies, sessions, signed URLs and anything you store with Crypt or encrypted casts. It should be a long random string that starts with base64: and stays secret.

Will a new APP_KEY log my users out?+

Yes. Existing sessions and encrypted cookies stop working, and any data you encrypted with the old key can no longer be decrypted. Passwords hashed with bcrypt or argon are not affected.

Why does it work locally but fail after I deploy?+

The .env file is usually not in git, so a fresh deploy has no key. Create it once on the server, or inject APP_KEY through your deploy tool or container environment, and keep it the same across releases.

Why do my tests show this error?+

The test run is not loading a key. Add an APP_KEY to your testing environment, for example in phpunit.xml or a .env.testing file, and generate one with php artisan key:generate --env=testing.

No application encryption key has been specified: fix