One of the basic issues any software developer or maintainer may face at some point is how to provide the software such that it can be installed and run from anywhere. Most people probably prefer a system where the configuration constraints for each software are as minimum as possible.

If you are running a Django app on top of Apache, you may have at least 3 different files:

  • /etc/apache2/sites-available/some_app.conf
  • /etc/apache2/conf.d/some_app-vhosts.conf
  • /path/to/some_app/django.wsgi

Let us assume the following points:

  1. All these files are tracked into your repository, so personal configurations are out of the scope
  2. The first file points to the installation path of some of the other files
  3. The latter files use variables, relative paths, etc

In this specific case, the configuration inside the first file is tightly coupled to the physical distribution of the files. This means that, if some other files are moved, Apache won’t be able to run the app.

A way to proceed

To solve that, let us take the first configuration file (/etc/apache2/sites-available/some_app.conf) as an example:

The highlighted lines indicate the dependencies, where the physical structure (/path/to/some_app) is referenced. To change that, one method would be to use environment variables.

1. Environment variables in Apache

Apache uses its own env vars which can be set (1) either using the SetEnv directive into your vhost configuration file (check an example) or (2) exporting the env var directly from Apache’s configuration files.

I opted for the second, since the vhost configuration file was not to be touched. The file is located at /etc/apache2/envvars, and variables can be exported like in any other Unix script.

Referencing an Apache env var from its configuration files is easy: just replace the path to the app for the variable, surrounded by braces.

2. Environment variables in Unix

OK, you may need variables within the Apache environment because some configuration files need them. Fine. But why environment variables in Unix?

Because you may need to access those variables from within your application code. This can naturally be circumvented by using relative paths within your app, and I personally think that’s a neater way. If you did it, feel free to skip this section and go for the final result.

There may be multiple ways to do this. The one I chose was to create a new script under /etc/profile.d and export a variable with the path to the app. Note that this method has at least one drawback: the data inside the script is only evaluated after the user logs in. This means that any change in this file needs to re-enter the user’s session.

You should assess how much this operation is likely to happen in your system and choose the option best suited for you. In this case, this option seemed good enough.

To access the Unix env vars from Python (e.g. from your Django app), just pass the env var’s name to the os.getenv module from within your Python code and you are done.

Final result

1. Apache2 envvars

First, add the environment variable to /etc/apache2/envvars.

Then, reference the env var from a given Apache configuration file. And don’t forget the enclosing braces.

2. Unix envvars

Now add the same value to a global env var in /etc/profile.d/

Finally, use it inside your Python code.

And this is all. Keep in mind to keep these variable synchronised after any change.

Share on FacebookTweet about this on TwitterShare on Google+Share on RedditEmail this to someonePrint this page
Rate the usefulness:
(No Ratings Yet)