.env environment variables when working with Apache

I’m getting in the habit of using a .env file for loading environment variables. Laravel already uses them for setting up configuration. It’s a nice way to setup variables that can be used in bash or through a php library like vlucas/phpdotenv.

For a site I’m working on I wanted the environment variable APP_ENV set to “local” in development and set to “production” on the live site (each environment has it’s own .env file). My configuration keep on getting overridden somewhere and returning “dev”.

What happened was the APP_ENV environment variable was already set in the Apache virtual host configuration and phpdotenv will not override variable set in Apache virtual host configuration.

If you are running into this same issue a pretty easy fix. Unset the variable in you .htaccess file and then the correct value will load from your .env file. At the top of my .htaccess file in the public root I have this then:

<IfModule mod_env.c>
    # unset the APP_ENV variable since it might have been set in Apache's vhost file and needs to be used from .env
     UnsetEnv APP_ENV
</IfModule>

Hope that helps!

Installing WordPress Plugins with Composer

I found out a cool usage for composer that I didn’t know about; use it for installing WordPress plugins. I use Phing to do all my deployments (slowly moving towards continuous integration with Jenkins). I wanted a way to get the plugin files and include them with my deployment automagicly. Composer can download and extracting the plugin archive file rather than using the gui in the wordpress admin. This task can be done when all your other required libraries are installed.

I wanted to install Contact Form 7. In order for this to work you need to download the plugin from a custom repository, so I setup my composer.json file something like this:

{
  "repositories": [
    {
      "type": "package",
      "package": {
        "name": "takayukister/contact-form-7",
        "type": "wordpress-plugin",
        "version": "4.3.1",
        "dist": {
          "type": "zip",
          "url": "https://downloads.wordpress.org/plugin/contact-form-7.4.3.1.zip"
        },
        "require": {
          "composer/installers": "v1.0.7"
        }
      }
    }
  ],
  "require": {
    "takayukister/contact-form-7": "4.3.1"
  },
  "extra": {
    "installer-paths": {
      "public_html/wp-content/plugins/{$name}/": [
        "type:wordpress-plugin"
      ],
      "public_html/wp-content/themes/{$name}/": [
        "type:wordpress-theme"
      ]
    }
  }
}

I set the name of the repo to the “takayukister/contact-form-7″ and then used that as it’s require name with the current version (right now 4.3.1). The paths in the “extra” section tell composer where to put the plugin once it’s installed.

In my Phing build.xml I could then run this before any packaging tasks:

<exec command="php composer.phar install" level="verbose" passthru="true" />

If there’s a better way to do this I’d like to hear it.