Using Apache Server Variables for Environment Settings

SetEnv CustomVar "foobar"

Programmers like to set up their machines their own way. Sometimes restricting their environment to strict corporate policies will directly affect their creativity. Luckily with Apache and PHP it is possible to create unique profiles for each developer to allow them to configure their environment in the way they want without affecting others.

You can have people work on Windows, Mac, or Linux without affecting the overall project. To accomplish this, we can have each user set up their own Apache settings, and set up their environment variables as they see fit.

Personal configurations

First, we can add a folder conf to be part of our projects. In this folder, we can add the production server's Apache configurations. Each user that checks out the project can make a copy of a conf file and customize it to work on his own machine.

website
   |--- app
   |--- conf
   |--- public_html
   |--- src

Here is a typical conf folder

website
   |--- app
   |--- conf
        |--- prod.server.conf // production configuration
        |--- dev.server.conf // local configuration
        |--- ibra.server.conf
        |--- gabe.server.conf
        |--- ...
   |--- public_html
   |--- src

One advantage of using this method is each user can decide to name the website they are accessing anyway they want. It doesn't have to be restricted to the way it is in the production server. This helps you avoid modifying the /etc/hosts file every time you want to use the dev versus the prod server.

// prod.server.conf
<VirtualHost:80>
    ServerName website.com
    ServerAlias *.website.com

    DocumentRoot /var/www/website.com/current/public_html

    SetEnv WEBSITE_VARS "/var/www/website.com/current/conf/prod.environment.php"

    <Directory /var/www/website.com/current>
    AllowOverride All
    Options -Indexes FollowSymlink
    </Directory>

</VirtualHost>

We can have a general dev environment that the user can take advantage of. For example, we want the errors to be displayed on the dev environment but not on production.

SetEnv is an Apache directive, that will be made available in our PHP code. SetEnv WEBSITE_VARS "..." will make this following line of code possible.

<?php
echo $_SERVER['WEBSITE_VARS'];
// "/var/www/website.com/current/conf/prod.environment.php"

This will be a very powerful tool we can use to have each developer load his own specific environment settings. Even windows Users can add their own paths:

SetEnv WEBSITE_VARS "c:\wamp\www\website.com\conf\walt.environment.php"

Let's say a user wants to setup his machine to display errors and use local files instead of using the CDN. To do so, he will have to create a .conf and a *.environment.php file. Here is how it will look.

// ibra.server.conf
<VirtualHost:80>
    # Use different domain name set in host file
    ServerName website-dev.com
    ServerAlias *.website-dev.com

    # use own path
    DocumentRoot /var/www/website.com/public_html

    # set a new environment file
    SetEnv WEBSITE_VARS "/var/www/website.com/conf/ibra.environment.php"

    # Different directory
    <Directory /var/www/website.com>
    AllowOverride All
    Options -Indexes FollowSymlink
    </Directory>

</VirtualHost>

And we can now create the environment file.

// ibra.environment.php
<?php
ini_set("display_errors","On");
error_reporting(E_ALL);
define("CDN_ASSETS","http://localhost");
define("ANALYTICS_ENABLED",false);
define("CURRENT_ENVIRONMENT","DEV");

Other users can decide to turn off errors on their machine, or change values to test different things. All the while, even if they check-in these files in the repository, it will not affect others or the production server.

Under /etc/apache2/sites-available each user can create a symlink that points to his own conf file. Changing settings simply means changing the symlink.

In order to make use of this, we can add an entry in the website entry poing index.php. This works best if you are using a framework that uses a single point of entry.

// index.php
<?php
if (isset($_SERVER['WEBSITE_VARS'])){
    include($_SERVER['WEBSITE_VARS']);
}

That's it. Each user has his own settings included in the project. Whether you use Windows, Linux, or Mac, you can configure it properly to use the application without breaking the flow for other users.


Comments

There are no comments added yet.

Let's hear your thoughts

For my eyes only