Django - Can't pass Environment Variable to Apache/Passenger on the WSGI Interface

谁说胖子不能爱 提交于 2019-12-06 10:26:02

I'm going to suggest an alternative solution since I have no idea how to fix your specific problem.

Create multiple WSGI files. production.wsgi, dev1.wsgi, dev2.wsgi, test.wsgi etc. Each webserver has to be configured with the /path/to/.wsgi anyway. There is only a minimal amount of code in a wsgi file anyway, so replicating this isn't very expensive. Also, you could have a _base.wsgi to supply all the common values, and require the derived wsgis to call application = wsgi.WSGIHandler().

Each developer on our project has their own setting files and wsgi files to enable tampering with settings without ever being able to break production with a rogue value.

I finally got it to work and I will provide the way I figured it out after @Josh suggestion.

Here is what looks like my WSGI file now:

import sys, os

sys.path.append(os.getcwd())
# this allows us to run the project under Passenger on our workstation
# it is simply a list of all the developers machine hostname it can be expended as we want
LIST_OF_DEV_HOSTNAME = ['PC1', 'PC2','PC3',]
# Return the current machine hostname
HOSTNAME = os.uname()[1]

# Import Django app module
if HOSTNAME in LIST_OF_DEV_HOSTNAME:
    # here if it is not detected you can append to your python path the root folder of your django app
    sys.path.append('/home/user/workspace/django-app')
    # As previously mentioned I use an Environment Variable in the settings.py to switch DB and debug settings
    # so here I set the ENV variable I use in the settings.py to reflect the Development environment
    os.environ['ENV'] = 'DEV'
else:
    # we could append any other needed path in here too
    sys.path.append('/any/other/folder')
    # As previously mentioned I use an Environment Variable in the settings.py to switch DB and debug settings
    # so here I set the ENV variable I use in the settings.py to reflect the Development environment
    os.environ['ENV'] = 'PROD'

# Shared between the two environments    
sys.path.append('/usr/lib/python2.4/site-packages/')

# Import the django app settings and Environment variable
os.environ['DJANGO_SETTINGS_MODULE'] = 'myapp.settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

So this is the way I got it to workout.

I could also achieved it in creating a file like /opt/environments and did the check like this: # check if the file exists if os.path.isfile('/opt/environments'): os.environ['ENV'] = 'DEV' else: os.environ['ENV'] = 'PROD'

it would have allowed me to perform the same check, but I would have to tell everyone to create the file. It depends on what you want to do create a file or add your hostname to the list, since our project is under SVN a simple commit of our WSGI file is for me more simple than the creation of the file.

Thank you.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!