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. 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' .env2. 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 .env3. 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. 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 --force5. 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:cache6. 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 .env7. 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:latest8. 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 MissingAppKeyExceptionHow 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.