Apache SetEnv not working as expected with mod_wsgi

后端 未结 2 1430
情深已故
情深已故 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: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.

提交回复
热议问题