0

I am trying to make a python flask app working in httpd 2.4 using mod_wsgi 4.6.2 (you can find details configuration here - my previous question). Anyway, when I tried to pass some configuration information into WSGI file or a python file. It was not successful. I assumed that I could do that because of this article.

<VirtualHost *:443>
........
    WSGIDaemonProcess abcd_server user=python_user group=python_user threads=5 python-home="/var/www/m.abcd.com/venv"
    WSGIProcessGroup abcd_server
    WSGIScriptAlias / "/var/www/m.abcd.com/python-src/wsgi-src/abcd_server.wsgi"
    SetEnv "P2SERVER_SRC" "/var/www/m.abcd.com/python-src/src"
    SetEnv "P2SERVER_APP_CFG" "/var/www/m.abcd.com/conf/abcd_conf.yaml"
........
</VirtualHost>

I failed to retrieve P2SERVER_SRC using "os.environ['P2SERVER_SRC']" in the wsgi file and failed to retrieve P2SERVER_APP_CFG in a python file using "os.getenv" API. Of course that I could bypass these issues with hard coded values in the wsgi file. But I really like to make SetEnv working if possible.

1 Answers1

0

That article makes it very clear that what you are trying to do doesn't work.

Note that the WSGI environment is passed upon each request to the application in the ‘environ’ argument of the application object. This environment is totally unrelated to the process environment which is kept in ‘os.environ’. The SetEnv directive has no effect on ‘os.environ’ and there is no way through Apache configuration directives to affect what is in the process environment.

You need to set them via the WSGI script file. This does not mean the values need to be in the WSGI script file. Create a file on that specific host in a directory not under version control, which the WSGI script file reads/processes in some way to get key/values for environment variables to set.

If you don't actually need them to be process wide environment variables, grab them from the per question environ dictionary passed to the WSGI application. For Flask that means accessing request.environ.

Graham Dumpleton
  • 57,726
  • 6
  • 119
  • 134