Setting Up Laravel Environments

Setting up a development environment is an important task you should do before starting any project. We all know that Laravel has a lot of cool features and one of things I like a lot is how easy is to set environments on your machine. Official documentation page provides you a lot of useful information about it, so make sure you check it.

Typically for each project you would need to have development, staging and production environments. All of these should be on separated servers, so configuring each environment can be a bit tricky.  But not with Laravel.

Laravel Environments

Before L4.1 release it was possible to set Laravel environments by domain host names. But, because of security concerns, that “feature” was removed and Taylor’s recommendation was that instead of using domain host names we use machine host name.

So, you can configure your environments to something like this:


That’s good and it will work fine. But, problem appears when your application runs multiple environments on the same machine. For example, if you have  and on one machine, application will not be able to use correctly these environments because dev and staging are on the same machine.

Server Environment Variables

To solve problem I mentioned above, we utilize server environment variables. For example, if you run your application on Apache2 web server, you set your environment variable like this:

You can put this code in your .htaccess file or append it to Apache2 virtual host. If you changed virtual host file, make sure to restart Apache.

However, if you use Nginx with php-fpm, you need to put this into your location ~ part of the config:

Now we can update detectEnvironment to read this variable as follows:

Dot Env Files

From 4.2 version it’s possible to add your Laravel Environment variables into .env files. For example, for local environment you need to create .env.local.php file within the root directory of your Laravel project. Typical configuration file is much like:

All returned key-value pairs will be available via the $_ENV and $_SERVER PHP superglobal variables and you can reference them within your configuration:

You can use whatever method you like, depending on your needs. If you are a beginner in the Laravel world, I hope this short tutorial will help you to get started with.

If you have any questions, feel free to post them in the comment section bellow.



Follow me

Mirza Pasic

Full Stack Developer at OLX
Web Developer. Geek. Systematic. Dreamer
Follow me

Published by

Mirza Pasic

Web Developer. Geek. Systematic. Dreamer

  • samizares

    Thank you very much, i struggled with this for a while,,,

  • Thorsten Palm

    Thank you thats good but we work that ist Laravel 5?