Apache SetEnv not working as expected with mod_wsgi

后端 未结 2 1431
情深已故
情深已故 2020-12-14 16:09

In a flask application I wrote, I make use of an external library which can be configured using environment variables. Note: I wrote this external library myself. So I c

相关标签:
2条回答
  • 2020-12-14 16:39

    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.

    So you have to do something else other than getenviron or os.environ['PWD'] to get the MY_PATH from apache.

    Flask adds the wsgi environ to the request, not app.environ, it is done by the underlaying werkzeug. So on each request to the application, apache will add the MYAPP_CONF key and youll access it wherever you can access request it seems as, request.environ.get('MYAPP_CONFIG') for example.

    0 讨论(0)
  • 2020-12-14 16:44

    @rapadura answer is correct in that you don't have direct access to the SetEnv values in your Apache config, but you can work around it.

    If you add a wrapper around application in your app.wsgi file you can set the os.environ on each request. See the following modified app.wsgi for an example:

    activate_this = '/var/www/michel/testenv/env/bin/activate_this.py'
    execfile(activate_this, dict(__file__=activate_this))
    
    from os import environ, getcwd
    import logging, sys
    
    from testenv.webui import app as _application
    
    # You may want to change this if you are using another logging setup
    logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)
    
    LOG = logging.getLogger(__name__)
    LOG.debug('Current path: {0}'.format(getcwd()))
    
    # Application config
    _application.debug = False
    
    def application(req_environ, start_response):
        environ['MYAPP_CONF'] = req_environ['MYAPP_CONF']
        return _application(req_environ, start_response)
    

    If you have set more environment variables in your Apache config, then you need to explicitly set each of them in the application wrapper function.

    0 讨论(0)
提交回复
热议问题