Laravel 5 Environment Configuration For Multiple Environments

This is a free resource from my online course, From Idea To Launch, where I teach you how to build a full Laravel web application, step by step, at beginner's speed. Learn more →

Laravel 5 utilizes the DotEnv PHP library, so environment configuration is now handled by the one single file: the .env file.

Configuring Environment Variables

If you created your Laravel 5 application using composer, you should, by default, already have a .env file in your application’s main directory that looks something like this:


APP_ENV=local
APP_DEBUG=true
APP_KEY=SomeRandomString

DB_HOST=localhost
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

If you don’t already have a .env file, just copy the .env.example file and rename it to .env.

This file contains all of your application’s environment variables.

By default, it’s set to use the local environment, and, if created via Homestead, the default homestead database.

The Laravel 5 Environment Configuration Keeps Your Credentials Secure

Note that since the .env file contains sensitive information, it’s automatically added to the Laravel application’s .gitignore.

On the other hand, the .env.example will be added to your Git repository. You should use the .env.example to provide a template for your application’s standard configuration. That way, if anyone needs to work with your codebase, they’ll know which environment variables to define.

Configuring Multiple Environments With Laravel 5

Configuring multiple environments no longer requires managing multiple files. You simply:

  1. deploy your code to another environment; and
  2. create a new .env file with the appropriate environment variables.

Done! Easy as pie.