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:
1 2 3 4 5 |
$env = $app->detectEnvironment(array( 'local' => array('local-machine-host-name'), 'staging' => array('staging-machine-host-name'), 'production' => array('production-machine-host-name') )); |
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 dev.mycoolapp1.com and staging.mycoolapp1.com 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:
1 2 |
# Set local environment SetEnv LARAVEL_ENV local |
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:
1 2 |
# Local env fastcgi_param LARAVEL_ENV local; |
Now we can update detectEnvironment to read this variable as follows:
1 2 3 4 5 |
$env = $app->detectEnvironment(function() { // We set local if LARAVEL_ENV is not set return getenv('LARAVEL_ENV') ?: 'local'; }); |
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:
1 2 3 4 |
<?php return [ 'LARAVEL_ENV' => 'local' ]; |
All returned key-value pairs will be available via the $_ENV and $_SERVER PHP superglobal variables and you can reference them within your configuration:
1 2 3 4 5 |
$env = $app->detectEnvironment(function() { // We set local if LARAVEL_ENV is not set return $_ENV['LARAVEL_ENV'] ?: 'local'; }); |
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.
Latest posts by Mirza Pasic (see all)
- Quick tip: How to delete a tag from a Git repository? - August 20, 2016
- Laravel Accessors and Mutators - December 17, 2015
- How to allow remote connections to PostgreSQL database server - December 15, 2015