1

I'm using Ubuntu 18.04. I have defined a series of environment variables in my /etc/profile file. When I login as my user, I can see them using

$ echo $DB_NAME
directory_data

I have Apache 2.4 also running on my machine under a www-data user (who has no home directory). I have set up my Apache configuration with "PassEnv" (https://httpd.apache.org/docs/2.4/mod/mod_env.html), hoping it would pick up my environment variables ...

$ cat /etc/apache2/sites-available/000-default.conf 
<VirtualHost *:80>
    # The ServerName directive sets the request scheme, hostname and port that
    # the server uses to identify itself. This is used when creating
    # redirection URLs. In the context of virtual hosts, the ServerName
    # specifies what hostname must appear in the request's Host: header to
    # match this virtual host. For the default virtual host (this file) this
    # value is not decisive as it is used as a last resort host regardless.
    # However, you must set it for any further virtual host explicitly.
    ServerName my.server.com

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html/web

        PassEnv DB_NAME
        PassEnv DB_USER
        PassEnv DB_PASS
        PassEnv DB_SERVICE
        PassEnv DB_PORT

    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    LogLevel info

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

        AliasMatch ^/(?!phpmyadmin)(?!states/)(?!countries/)(?!coops/)(?!data).* /var/www/html/client/build/$0
    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    Include conf-available/phpmyadmin.conf

        WSGIDaemonProcess maps \
            home=/var/www/html/web python-home=/var/www/html/web/venv
        WSGIProcessGroup maps 
        WSGIScriptAlias /coops /var/www/html/web/directory/wsgi.py/coops process-group=maps
        WSGIScriptAlias /data /var/www/html/web/directory/wsgi.py/data process-group=maps
        WSGIScriptAlias /countries /var/www/html/web/directory/wsgi.py/countries process-group=maps
        WSGIScriptAlias /states /var/www/html/web/directory/wsgi.py/states process-group=maps
        WSGIPassAuthorization On

        <Directory /var/www/html/web/maps>
            <Files wsgi.py>
                Require all granted
            </Files>
        </Directory>
</VirtualHost>

Apache uses WSGI to connect to Python, where I ultimately want to reference the env vars. Here is my Python file, settings.py

DATABASES = {
    'default': {,
        'ENGINE': 'django.db.backends.mysql',
        'NAME': os.environ['DB_NAME'],
        'USER': os.environ['DB_USER'],
        'PASSWORD': os.environ['DB_PASS'],
        'HOST': os.environ['DB_SERVICE'],
        'PORT': os.environ['DB_PORT']
    }
}

but the env var isn't getting picked up.

[Thu Jun 25 17:29:28.870247 2020] [wsgi:error] [pid 11363]   File "/var/www/html/web/directory/settings.py", line 93, in <module>
[Thu Jun 25 17:29:28.870319 2020] [wsgi:error] [pid 11363]     'NAME': os.environ['DB_NAME'],
[Thu Jun 25 17:29:28.870408 2020] [wsgi:error] [pid 11363]   File "/usr/lib/python3.6/os.py", line 669, in __getitem__
[Thu Jun 25 17:29:28.870474 2020] [wsgi:error] [pid 11363]     raise KeyError(key) from None
[Thu Jun 25 17:29:28.870564 2020] [wsgi:error] [pid 11363] KeyError: 'DB_NAME'

Where are environment variables generally placed so that they can be recognized by the Apache process?

2
2

I assume you run a fresh install of 18.04 with systemd? If you've updated the OS over time then some components like systemd may be a bit different from stock 18.04.

In /lib/systemd/system/apache2.service, add Environment= statements for your desired variables to the [Service] section. Do not use quotes. Example:

[Service]
Type=forking
Environment=APACHE_STARTED_BY_SYSTEMD=true
Environment=DB_SERVICE=localhost
ExecStart=/usr/sbin/apachectl start
...

Together with the PassEnv stanzas in your description that should do it.

Restart apache with

$ sudo systemctl restart apache2

to pick up any changed settings

2
  • Thanks. What's the proper way to be starting/stopping my Apache service? I made the changes to the above file and ran "sudo apachectl restart" but the environment vars didn't get picked up by my Django app (connected through my Apache instance)
    – Dave
    Jun 26 '20 at 1:22
  • @Dave I added the systemd restart command. I think apachectl will restart the master process without reevaluating the systemd environment variables
    – jochenroth
    Jun 27 '20 at 5:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.