Unable to read Environment Variables set in OS within Flask App running via mod_wsgi

24 views
Skip to first unread message

Vaibhav Bajpai

unread,
Nov 15, 2019, 1:39:33 AM11/15/19
to modwsgi
Iam working on running a Python Flask app with Apache HTTP Server (v2.4) where Iam attempting to setup an environment variable to identify deploy environment type (Dev, Prod) and unable to extract the environment variable via "os.environ" in python code

After following few posts, i made the following changes I made in key files of my code. Please help me with a solution. Any suggestions and workarounds would be appreciated

bbmetrics.wsgi

#!/usr/bin/python3
import os
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0, "/var/www/bbmetrics")


def application(environ, start_response):
os.environ['CE360AF_ENV'] = environ['CE360AF_ENV']
from bbmetrics import app as _application
application.secret_key = 'bbmetrics_secret'
return _application(environ, start_response)

I get the error as below when running the app Request URLs but the Python interpreter confirms that variable is correctly set
mod_wsgi (pid=73322): Exception occurred processing WSGI script '/var/www/bbmetrics/bbmetrics.wsgi'.
[Thu Nov 14 23:59:09.601165 2019] [wsgi:error] [pid 73322] [client xx.xx.xx.xx:xxxx] Traceback (most recent call last):
[Thu Nov 14 23:59:09.601199 2019] [wsgi:error] [pid 73322] [client xx.xx.xx.xx:xxxx]   File "/var/www/bbmetrics/bbmetrics.wsgi", line 10, in application
[Thu Nov 14 23:59:09.601204 2019] [wsgi:error] [pid 73322] [client xx.xx.xx.xx:xxxx]     os.environ['CE360AF_ENV'] = environ['CE360AF_ENV']
[Thu Nov 14 23:59:09.601223 2019] [wsgi:error] [pid 73322] [client xx.xx.xx.xx:xxxx] KeyError: 'CE360AF_ENV'

[dc-user@xxxxxx bbmetrics]$ python3
Python 3.7.4 (default, Oct 25 2019, 03:53:56)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36.0.1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> print(os.environ['CE360AF_ENV'])
dev
>>>

__init__.py

#!/usr/bin/python3
from flask import Flask
app = Flask(__name__)
from bbmetrics.bbmetrics import *

Virtual Host Configiuration File

<VirtualHost *:80>
                ServerName x.x.x.x
                ServerAlias xxxxx.com
                ServerAdmin a...@pqr.com
                WSGIScriptAlias / /var/www/bbmetrics/bbmetrics.wsgi
                <Directory /var/www/bbmetrics/bbmetrics/>
                        Order allow,deny
                        Allow from all
                </Directory>
                Alias /static /var/www/bbmetrics/bbmetrics/static
                <Directory /var/www/bbmetrics/bbmetrics/static/>
                        Order allow,deny
                        Allow from all
                </Directory>
                ErrorLog /var/log/httpd/error.log
                LogLevel warn
                CustomLog /var/log/httpd/access.log combined
</VirtualHost>

Calling Python code for extracting variable. Is this the right way to extract environment variable for WSGI based Python Flask App after WSGI file is correctly written

#!/usr/bin/python3
import os
import requests
import datetime
import configparser

from flask import abort


httpErrorCode = {
500: "server Error",
404: "Resource Not Found",
401: "Not Authorized"
}

deployEnv = os.environ['CE360AF_ENV']

app.run() Python File

#!/usr/bin/python3
from flask import request
from flask_restful import Resource, Api, reqparse
from flask_cors import CORS

from bbmetrics import app
from bbmetrics.main import GetProjectRepos,GetAllPullRequestMetricsFromRepo,GetAllPullRequestMetricsFromRepoForDateRange

api = Api(app)
CORS(app)


@app.route("/")
def welcome():
return "Welcome to CE360 Assessment Framework!!!"

if __name__ == '__main__':
app.run()

Graham Dumpleton

unread,
Nov 15, 2019, 1:43:01 AM11/15/19
to mod...@googlegroups.com
Where were you attempting to set the CE360AF_ENV environment variable? Apache isn't going to read settings from your user account.

For the way you were trying to read it, you would have needed to have had SetEnv directive for it in Apache configuration, but then what you were doing of setting os.environ on each request is bad practice anyway. It also gets set too late if trying to use it at global scope in module imports if the module was imported before the first request.

What you should do instead is have a daemon mode process group and delegate the WSGI application to it.

                WSGIDaemonProcess dev
                WSGIScriptAlias / /var/www/bbmetrics/bbmetrics.wsgi process-group=dev application-group=${GLOBAL}

Then in your code use:

    import mod_wsgi

    deployEnv = mod_wsg.process_group == 'dev'

In other words, you key off the name of the process group the WSGI application has been delegated to run in.

Graham

On 15 Nov 2019, at 5:24 pm, Vaibhav Bajpai <vaibha...@gmail.com> wrote:

Iam working on running a Python Flask app with Apache HTTP Server (v2.4) where Iam attempting to setup an environment variable to identify deploy environment type (Dev, Prod) and unable to extract the environment variable via "os.environ" in python code

After following few posts, i made the following changes I made in key files of my code. Please help me with a solution. Any suggestions and workarounds would be appreciated

bbmetrics.wsgi

#!/usr/bin/python3
import os
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0, "/var/www/bbmetrics")


def application(environ, start_response):
os.environ['CE360AF_ENV'] = environ['CE360AF_ENV']
from bbmetrics import app as _application
application.secret_key = 'bbmetrics_secret'
return _application(environ, start_response)

I get the error as below when running the app Request URLs but the Python interpreter confirms that variable is correctly set
mod_wsgi (pid=73322): Exception occurred processing WSGI script '/var/www/bbmetrics/bbmetrics.wsgi'.
[Thu Nov 14 23:59:09.601165 2019] [wsgi:error] [pid 73322] [client 172.27.138.173:56868] Traceback (most recent call last):
[Thu Nov 14 23:59:09.601199 2019] [wsgi:error] [pid 73322] [client 172.27.138.173:56868]   File "/var/www/bbmetrics/bbmetrics.wsgi", line 10, in application
[Thu Nov 14 23:59:09.601204 2019] [wsgi:error] [pid 73322] [client 172.27.138.173:56868]     os.environ['CE360AF_ENV'] = environ['CE360AF_ENV']
[Thu Nov 14 23:59:09.601223 2019] [wsgi:error] [pid 73322] [client 172.27.138.173:56868] KeyError: 'CE360AF_ENV'

--
You received this message because you are subscribed to the Google Groups "modwsgi" group.
To unsubscribe from this group and stop receiving emails from it, send an email to modwsgi+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/modwsgi/c3ffd820-917f-47cf-b19f-e03e76825443%40googlegroups.com.

Vaibhav Bajpai

unread,
Nov 15, 2019, 2:26:05 AM11/15/19
to modwsgi
Thanks a lot Graham for your inputs, your suggestion made it work. I was able to read the value from "mod_wsg.process_group"

However, I have more environment variables that need to be referred For e.g LOGDIR which are like folder paths within the OS, so should I be using SetEnv directive in Virtual Hosts Conf files?

Also, whatever SetEnv directive I use in .wsgi file, how do I extract the same in my Python code

Graham Dumpleton

unread,
Nov 15, 2019, 2:52:07 AM11/15/19
to mod...@googlegroups.com

On 15 Nov 2019, at 6:26 pm, Vaibhav Bajpai <vaibha...@gmail.com> wrote:

Thanks a lot Graham for your inputs, your suggestion made it work. I was able to read the value from "mod_wsg.process_group"

However, I have more environment variables that need to be referred For e.g LOGDIR which are like folder paths within the OS, so should I be using SetEnv directive in Virtual Hosts Conf files?

Don't use environment variables, it is generally a bad idea. Use a separate configuration file. You can work out which file to read by using the name of the process group. Examples of config files:


Or even just use a Python module with the configuration in it and have different sections again keyed off the process group.

If you really want to use SetEnv, see:


but these are only available in context of a request, and are not process wide. Adding them to os.environ on each request is a bad idea and can fail in certain ways.

-- 
You received this message because you are subscribed to the Google Groups "modwsgi" group.
To unsubscribe from this group and stop receiving emails from it, send an email to modwsgi+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages