Laravel .env File: What to Set Before Your Site Goes Live

ResellerBytes blog cover: Laravel .env File: What to Set Before Your Site Goes Live

Quick answer: Before a Laravel site goes live, set APP_ENV to production, APP_DEBUG to false, a real APP_KEY and the correct HTTPS APP_URL in your Laravel .env file. Then fill in the live database, mail, session and cache settings, keep the file out of Git and outside the public folder, and cache your config once everything is right.

The Laravel .env file is small, but it decides how your whole app behaves once real visitors arrive. A local copy that works fine on your laptop can leak error details, send mail to nowhere or lose sessions when it’s copied to a live server unchanged. This guide goes through the settings that matter before launch, what each one does, and the mistakes that cause the most trouble. If you’re still choosing where the app will live, start with our guide on how to host a Laravel website on shared hosting, then come back here before you flip the switch.

What is the Laravel .env file?

It’s a plain text file in your project root that holds settings which change between environments, such as database passwords, mail credentials and the app URL.

Laravel reads it when the app boots, and the files in the config folder pull values from it with the env() helper. The idea is simple: the code stays the same everywhere, and only the .env file changes between your laptop, a staging copy and the live site. Laravel’s own configuration documentation explains how this works (checked October 2026).

A fresh project ships with a .env.example file. That one is meant to be committed to Git as a template with the keys but no real secrets. The real .env file is not, and Laravel’s default .gitignore already excludes it.

The app settings to check first

Four lines at the top of the file do most of the work: APP_ENV, APP_DEBUG, APP_KEY and APP_URL. Get these right before anything else.

APP_ENV=production

This tells Laravel and many packages which environment they’re running in. Some packages switch off developer tools or behave more strictly when they see production. Artisan commands that can destroy data, such as running migrations, also ask you to confirm when the environment is production, which is a useful safety net.

APP_DEBUG=false

This is the setting that causes real damage when it’s wrong. With debug on, an error page can show stack traces, file paths and parts of your configuration to anyone who triggers it. On a live site it must be false. Visitors then see a plain error page, and the details go to your log file instead, where only you can read them.

APP_KEY

Laravel uses this key to encrypt cookies, sessions and anything you encrypt with its tools. It should be a long random value created with php artisan key:generate, never copied from a tutorial or another project. Treat it like a password. If you change it on a live site, existing sessions and encrypted data stop working, so set it once before launch and keep it safe.

APP_URL

Set this to the full live address, including https://. Laravel uses it when it builds links outside a normal web request, for example in queued emails or console commands. A leftover http://localhost here is a common reason why password reset emails point to the wrong place.

Database settings

Your live database is almost never the same as your local one, so these values always need changing.

The usual keys are DB_CONNECTION, DB_HOST, DB_PORT, DB_DATABASE, DB_USERNAME and DB_PASSWORD. Recent Laravel versions default to SQLite for new projects, so if your live site runs on MySQL, make sure DB_CONNECTION=mysql is set and the other values match the database you created in your hosting panel. Use a dedicated database user with a strong password, not a shared or root account. Our guide to PHP hosting with MySQL covers what to look for on the hosting side.

If your password contains spaces or a # character, wrap it in double quotes. Otherwise Laravel may read only part of it, and you’ll see a connection error that looks like a wrong password.

Mail settings

Mail is the part people forget to test, because nothing breaks until someone asks for a password reset.

Set MAIL_MAILER (usually smtp), plus MAIL_HOST, MAIL_PORT, MAIL_USERNAME and MAIL_PASSWORD for the mailbox or mail service you send from. Set MAIL_FROM_ADDRESS to an address at your own domain and MAIL_FROM_NAME to your site or business name. Locally, many developers use the log mailer, which writes emails to a file instead of sending them. If that’s still set on the live server, every email quietly vanishes into the log.

After launch, trigger one real email, such as a password reset, and check it arrives and that its links use your live domain.

Sessions, cache and queues

These decide where Laravel keeps short-lived data. The defaults work, but they’re worth a look before real users log in.

  • SESSION_DRIVER: where login sessions are stored, such as file or database. If you use database, the sessions table needs to exist on the live database.
  • SESSION_SECURE_COOKIE=true: once your site runs fully on HTTPS, this makes browsers send the session cookie only over secure connections. Our post on web hosting with free SSL explains how to get HTTPS working first.
  • CACHE_STORE: where cached data goes (older Laravel versions call it CACHE_DRIVER). Check the name your version expects.
  • QUEUE_CONNECTION: sync runs jobs straight away inside the request, while database or redis needs a worker process running in the background. Only use a background queue if your hosting lets you keep a worker running. For setting up workers properly, see Laravel queue workers in production.

Logging

With debug off, the log is where you’ll find out what went wrong, so make sure it’s useful but not bloated.

LOG_CHANNEL controls where entries go. The daily channel writes one file per day, which keeps files small. LOG_LEVEL sets the minimum level written. On a live site, error or warning is usually enough, while debug can fill your disk with noise. Check the storage/logs folder now and then, especially after a deploy.

A production .env checklist

Here’s a short reference you can compare against your file before launch.

Setting Local example Live site
APP_ENV local production
APP_DEBUG true false
APP_KEY generated locally its own generated key, kept safe
APP_URL http://localhost https://yourdomain.com
DB_CONNECTION and DB_* sqlite or local MySQL live database name, user and password
MAIL_MAILER log smtp with real credentials
SESSION_SECURE_COOKIE not set true once HTTPS works
LOG_LEVEL debug error or warning

Keep the .env file private

The file holds every secret your app has, so two rules matter more than any single setting: never commit it, and never let the web server serve it.

Laravel is built so that only the public folder is reachable from the web, with the .env file one level above it. Point your domain’s document root at public, not at the project root. If the project root is exposed, someone could request yourdomain.com/.env and read your database password. After you deploy, try opening that address yourself. You should get a “not found” or “forbidden” response, never the file’s contents.

If a .env file was ever committed to a Git repository, assume its secrets are exposed. Change the database password, mail password and any API keys, then remove the file from the repository. For teams that want to keep an encrypted copy in version control, Laravel includes php artisan env:encrypt and env:decrypt, described in the configuration docs linked above.

Cache the config, then remember to clear it

Running php artisan config:cache combines all your configuration into one cached file, which Laravel’s deployment guide recommends for production.

There’s a catch. Once the config is cached, Laravel stops reading the .env file, so editing it changes nothing until you run php artisan config:clear or cache again. This is the classic “I changed the .env and nothing happened” moment. It also means you should only call env() inside files in the config folder. Calls to env() anywhere else in your code return null once the config is cached.

How you run these commands depends on your hosting. Some setups give you a terminal, others let you run them during deployment from your own machine or a deploy script. Check what your plan offers before you rely on one method.

Moving an existing Laravel site?

If your app already runs somewhere else, the .env file is the part of a move that most often goes wrong, because the database host, mail settings and URL all change at once.

Make a list of every value that will differ on the new server before you start, and test mail and logins on the new copy before you point your domain at it. ResellerBytes offers free website migration on its shared hosting plans if you’d rather have the move done for you. You can compare the Starter, Business and Pro plans on the ResellerBytes hosting page.

Frequently asked questions

Should I upload my local .env file to the live server?

No. Start from .env.example and fill in live values on the server. A copied local file usually brings debug mode, the wrong URL and a local database with it.

What happens if APP_DEBUG is true on a live site?

Error pages can show stack traces, file paths and configuration details to visitors. Set it to false before launch and read errors in your log file instead.

Why don’t my .env changes take effect?

Your configuration is probably cached. Run php artisan config:clear, or cache it again with php artisan config:cache after making your changes.

Is it safe to commit .env.example?

Yes, as long as it contains only key names and harmless placeholder values. Never put real passwords or API keys in it.

Do I need a new APP_KEY for production?

Yes. Generate a fresh key for the live site and keep it private. Changing it later logs everyone out and breaks data encrypted with the old key.

Where should the .env file sit on shared hosting?

In the project root, above the public folder, with your domain’s document root pointing at public. That keeps the file out of reach of the web.

Share this content:

Leave a Reply

Your email address will not be published. Required fields are marked *