Quick Tip – Configuring Laravel Environments on Media Temple

Note: This applies to Laravel 4. I’m still looking into options for Laravel 5…

I ran into a pesky little bug today where the Laravel standard environment configuration by hostname wasn’t working on a Media Temple deployed site. After some digging and tweaking, here’s how you get it to work…

Cause

Media Temple uses a form of dynamic hostnames for it’s grid server hosting. As a result, even when you run “hostname” at the command prompt and use that value it will not work for you.

Solution

A simple way to get environments to work is to add a server variable into your .htaccess file that resides in the “public” folder of your site. Add the following line to set an environment variable:

SetEnv HTTP_LARAVEL_ENV "test";

Important Note: Media Temple requires you prefix your variable with HTTP_ for security reasons.

Then we modify our bootstrap/start.php file (around line 27) to check for the variable when starting up:

$env = $app->detectEnvironment(function()
{
   return $_SERVER['HTTP_LARAVEL_ENV'] ?: 'local';
});

Notice here that we have a fallback here so that if the environment variable isn’t found, we default to local. I know that Laravel has a “production default” mindset for it’s configuration, but I prefer to default to local because it will typically crash the app in the test/production environment and I can easily determine if I have an improper setup. In addition, having this setup doesn’t require me to do anything additional in my local development environment.

There you have it. Deploy this and you should have your environment configurations singing along nicely.

Final Note

If you were using the original “hostname” type configuration, you may have been able to run the standard artisan migrate command    from the command line without problems. With this new configuration, you’ll need to add one more parameter when running it:

php artisan migrate —env=test

Bonus Tip

Getting weird errors when trying to run migrations from the command prompt? By default Media Temple uses PHP 5.3 and you’ll need PHP 5.4+ in order to properly run Laravel. Make sure you configure the directory from the administrative panel to use the latest version of PHP. Then you can run your migrations from the command prompt as such:

php-latest artisan migrate —env=test

Enjoy!!!

10 thoughts on “Quick Tip – Configuring Laravel Environments on Media Temple

  1. Do you have any tips on getting laravel 5 up and running on mediatemple in the first place? I followed some info in this post:

    https://www.yesdevnull.net/2014/03/using-php-54-cli-on-media-temples-grid-hosting/

    But even after doing everything, it still throws an error that makes me think that it’s still not always using php-latest.
    Parse error: syntax error, unexpected ‘[‘ in /nfs/c01/h11/mnt/9372/domains/code.cutupsmethod.com/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php on line 49

    1. Greetings Geoff. I’m just getting started on a project using Laravel 5 myself, so I’m still familiarizing myself with how the newer configuration model has changed. I’ll reply back if I find anything out.

    2. ahh, okay, so not environment stuff but PHP in general. Are you getting this error at the command line or in the website? You’ll want to check the PHP settings for your MediaTemple domain instance to make sure it is using the latest version.

  2. Are you running into any issues when running the migrate command?

    I keep getting:
    [PDOException]
    SQLSTATE[HY000] [2002] Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2)

    1. The other thing to check is the security settings for your server and DB. One time I had accidentally removed access from my EC2 server to RDB, which gave the same error

      1. I am using 4.2. Running on GS. I get the error when i simply run “mysql” from the command line as well. All my custom laravel commands that interact with the database also experience the same issue.

        1. Nevermind. I just realized it was a configuration issue. the application was detecting my environment was production instead of dev. i had tried a different approach for setting the hostname (which I am now going to use your approach)

What are your 10 bits on the matter? I want to know!

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s