search

Home  >  Q&A  >  body text

django - Problem reading environment variables when apache loads the configuration file

My project is deployed using django + apache. In order to easily distinguish between online and development environments, I set an environment variable Z_ENV on the server, and then in the Django configuration file it looks like this written,

env = os.environ.get("Z_ENV", None)
if env == "server":
    from .server_settings import *
else:
    from .local_settings import *

This way of writing is no problem using some of the commands that come with Django locally or on the server, such as directly running os.environ.get("Z_ENV", None)is server or python managa.py syncdb is indeed using the configuration in server_settings, but this environment variable is not obtained when apache loads the configuration file, and then I used local_settings. I added print env above, and then when service apache2 restart was printed, None was printed in the apache log file. .

The environment variable is modified in the /etc/profile file, the content is Z_ENV=server export Z_ENVAt first, it did not take effect for apache users, but then I checked Check, the environment variables set here are effective for all users.

Let’s ask everyone what’s going on?

天蓬老师天蓬老师2791 days ago625

reply all(1)I'll reply

  • 迷茫

    迷茫2017-05-16 17:05:31

    at /etc/apache2/envvars 中设置 export Z_ENV=server

    reply
    0
  • Cancelreply