Environment Variables with Laravel Forge

laravel-four-icon1For our latest Laravel application, we decided to try the Laravel Forge service to configure and manage the staging and production sites for the app. If you haven’t heard about Laravel Forge, check out the free screencast series by Jefferey Way over at Laracasts.

The Forge UI is great, and takes away a ton of the pain of server and application configuration. Following best practices, we use environment variables to manage environment specific settings like the database name and credentials. One of the features Forge provides is the ability to automate deployment from your github / bitbucket repo upon a post to a specific branch.

The problem we were having was that our migrations would not run. The error was:

{"error":{"type":"ErrorException","message":"Undefined index: DB_HOST","file":"\/home\/forge\[ourbasedir]\/laravel\/app\/config\/database.php","line":51}}

In our case, we are using a non-standard repo directory structure where instead of the typical Laravel app directory structure:

---app
|-bootstrap
|-public
|-vendor

we have another layer above:

---docs
|-laravel
| |-app
| |-bootstrap
| |-public
|-html-templates

Upon inspection of the directory on the server, we found the “.env.php” file was in the base directory. The problem with this was that artisan, the application responsible for Laravel migrations, could not see the environment variables, and so the database configuration was wrong. The solution was simply to move .env.php file into our “laravel” directory so that artisan had access to it.

2 thoughts on “Environment Variables with Laravel Forge

  1. Grace

    I’m getting the same error, but I’m not using forge and my directory is the typical laravel structure –

    —app
    |-bootstrap
    |-public
    |-vendor

    Any ideas on why I would be having this problem. This is the only resource I have found addressing this problem. Thanks!

    Reply

Leave a Reply

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