The Laravel .env file in production: safe settings
· The AYT team · 9 min read

A production Laravel .env file has APP_ENV=production, APP_DEBUG=false, an APP_KEY generated once, an APP_URL with https://, the real database details and a log level such as warning or error. The file lives only on the server, outside the public folder, and never goes into git. After every change you run php artisan config:cache again, because in production Laravel reads its settings from the cache, not from .env.
What the .env file is
.env holds the settings that differ between environments: the database password, the app key, SMTP details, API keys. Laravel reads it at boot, and the files in config/ pick up the values with env('NAME', 'default'). The code stays the same on your computer and on the server; only .env differs. It's a plain text file with one setting per line, written as KEY=value, and lines starting with # are comments.
What goes into git is .env.example, a template with every key and no secrets. Each server builds its own .env from it.
A complete production .env
An example for an app on hosting, with MariaDB, a database queue and email over SMTP:
APP_NAME="Example Shop"
APP_ENV=production
APP_KEY=base64:...generated-with-key:generate...
APP_DEBUG=false
APP_URL=https://www.example.com
APP_LOCALE=en
APP_FALLBACK_LOCALE=en
LOG_CHANNEL=stack
LOG_STACK=daily
LOG_DAILY_DAYS=14
LOG_LEVEL=warning
DB_CONNECTION=mariadb
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=username_1
DB_USERNAME=username_1
DB_PASSWORD="long-password-from-the-panel"
SESSION_DRIVER=database
SESSION_LIFETIME=120
SESSION_SECURE_COOKIE=true
CACHE_STORE=database
QUEUE_CONNECTION=database
MAIL_MAILER=smtp
MAIL_HOST=mail.example.com
MAIL_PORT=587
MAIL_USERNAME=orders@example.com
MAIL_PASSWORD="mailbox-password"
MAIL_FROM_ADDRESS="orders@example.com"
MAIL_FROM_NAME="${APP_NAME}"
The settings, one by one
APP_ENV and APP_DEBUG
APP_ENV=production tells the app and its packages they're running in production: many development tools switch off, and artisan commands that destroy data (such as migrate:fresh) ask for confirmation. That's why deploy scripts pass --force to migrate.
APP_DEBUG=false is mandatory. With true, an error shows the visitor the debug page, with code snippets and configuration values. Laravel's documentation says plainly that in production this value should always be false.
APP_KEY
The key Laravel uses to encrypt cookies, sessions and anything you pass through Crypt or the encrypted cast. Generate it once, at first install, with php artisan key:generate.
Don't regenerate it on a live app: every user is logged out and data encrypted with the old key can no longer be read. If you want to change it (as a periodic rotation, for example), put the new key in APP_KEY and the old one in APP_PREVIOUS_KEYS. Laravel encrypts with the new key and, when decrypting, tries the old ones in turn.
APP_URL
The public address, with https:// and the form of the domain you actually use (with or without www). Laravel builds links from it in emails, artisan commands and queued jobs, where there's no request to learn the domain from. An APP_URL with http:// on an HTTPS site can also break signed links, such as email verification links.
Logs
By default, LOG_STACK=single writes everything to one laravel.log, which grows forever. LOG_STACK=daily writes one file per day and keeps the last LOG_DAILY_DAYS (14 by default). The LOG_LEVEL=debug from .env.example writes too much in production; warning or error keeps what matters.
The database
Since Laravel 11, .env.example ships with DB_CONNECTION=sqlite. On hosting, set mysql or mariadb and the details of the database you created in the panel. Quote the password if it has spaces or characters like # and $: unquoted, part of it can be read as a comment or as a variable.
Sessions and cookies
SESSION_SECURE_COOKIE=true sends the session cookie over HTTPS only. SESSION_DRIVER=database works on any hosting; redis is faster if you have it. With the file driver, sessions live in storage/framework/sessions, which must be writable.
Cache and queues
CACHE_STORE and QUEUE_CONNECTION default to database, which works with nothing extra. With Redis available, move the cache to redis. For queues on shared hosting, see our article on Laravel queues on hosting.
The MAIL_MAILER=log from .env.example sends nothing; it only writes messages to the log. In production use smtp, with the details of a real mailbox on your domain, so messages pass SPF and DKIM.
.env and git
.env is in .gitignore from the start in a new project, and it must stay there. If it did end up in a commit, removing it from history isn't enough: anyone who cloned the repository in the meantime has the passwords. Change everything in it: the database password, SMTP passwords, API keys and APP_KEY (see below for what to do with the old key).
.env.encrypted, if you want the settings in the repository
Laravel can encrypt the file: php artisan env:encrypt creates .env.encrypted and shows you the decryption key. The encrypted file can live in git. On the server you decrypt it with php artisan env:decrypt, passing the key with --key or through the LARAVEL_ENV_ENCRYPTION_KEY environment variable. It's worth it when several people need the same settings; for a single server, a .env written directly there is simpler.
.env and config:cache
In production you run php artisan config:cache (or php artisan optimize, which includes it). Laravel merges every file in config/ into one, bootstrap/cache/config.php, and from then on doesn't read .env at all. Two consequences:
- after any change to
.env, runphp artisan config:cacheagain, or the app keeps the old values; env()only works inside the files inconfig/. Anenv('API_KEY')written directly in a controller returnsnullafter caching. Put the value in a config file (such asconfig/services.php) and read it withconfig('services.courier.key').
Build the config cache on the server, once .env is there. One built on your computer or in CI contains that machine's paths and passwords and is a common cause of the 500 error after a deploy.
How to protect .env on the server
- The site's document root must be the
publicfolder, not the project. That way.envhas no public address. If your host won't let you change the root, read our article on the Laravel public folder. - The web server must refuse files whose names start with a dot. The nginx configuration Laravel recommends has a rule for this (
location ~ /\.(?!well-known).*withdeny all). - Check it: open
https://your-domain.com/.envin a browser. You should get 404 or 403, never the file's contents. - Permissions: 600 or 640, so other users on the server can't read it.
VITE_ variables are public
Keys starting with VITE_ (such as VITE_APP_NAME) are copied into the JavaScript files at build time, with npm run build. They reach every visitor's browser, so never put a password or a secret API key there. For the same reason, a change to a VITE_ variable only shows after a new build; config:cache doesn't touch it.
A separate .env for staging
A test copy of the app has its own .env, not a copy of production's:
APP_ENV=stagingand, if you want to see errors,APP_DEBUG=true, only if the staging address isn't public;- a different database, so tests never touch real orders;
MAIL_MAILER=log, so a test order doesn't email real customers;- test API keys, where the provider (payments, couriers) offers them.
How to check which values the app sees
php artisan about shows the environment, whether debug mode is on, the app URL, the cache, session and queue drivers, and whether the configuration is cached. It's the quickest way to see whether the server read what you wrote in .env, especially after a change you forgot to follow with config:cache.
Common mistakes
- Unquoted spaces:
APP_NAME=Example Shopbreaks parsing. WriteAPP_NAME="Example Shop". ${APP_NAME}doesn't work inside single quotes and only picks up keys defined earlier in the file.APP_ENV=prodinstead ofproduction: some checks in Laravel and in packages compare againstproductionexactly.- A
.envleft inpublic/, copied there by a wrong upload. Find it and delete it.
Frequently asked questions
Where does .env go on hosting?
In the project root, next to artisan and composer.json, not in public.
Can I have several .env files?
Yes. If APP_ENV is set as a server environment variable, Laravel loads .env.[APP_ENV] (such as .env.staging) when it exists. On hosting it's simpler to give each environment its own folder with its own .env.
What else needs a restart after I change .env?
Besides php artisan config:cache, your queue workers: they loaded the configuration when they started and keep it until they stop. Run php artisan queue:restart so they start again with the new values. The same goes for Horizon and any other long-running process.
What do I do if I published my APP_KEY?
Generate a new key and put it in APP_KEY. Don't leave the published one in APP_PREVIOUS_KEYS longer than you need to: while it's there, the app also accepts cookies made with it. Users get logged out once; re-encrypt data stored with the old key using a script run while it's still in APP_PREVIOUS_KEYS, then remove it.
.env on AYT hosting
On a Laravel site set up with the Laravel Toolkit, .env lives in the shared folder and is linked into every release, so it survives deploys. The panel writes it when you make the site a Laravel site, with DB_*, APP_URL, APP_ENV=production, APP_DEBUG=false and APP_KEY already filled in, and the first deploy adds the keys from .env.example that are missing.

You edit it from Settings, with fields for Environment, Debug mode, Log level, Cache, Sessions, Queue and the mail details. When you save, the panel rebuilds the config cache (if the app uses one, as the default deploy script does through optimize) and restarts the site's processes, so the change applies at once, with no manual config:cache. The site's Security page also checks that the app runs with APP_ENV=production. More on the Laravel tools under apps.
