【问题标题】:How to set up globally visible environment variables on Azure VM (v1)如何在 Azure VM (v1) 上设置全局可见的环境变量
【发布时间】:2015-12-10 23:09:28
【问题描述】:

有没有办法在带有 Ubuntu OS 的 Azure VM(服务管理)上设置系统范围/全局可见的环境变量?我被困在将它们设置在 ubuntu 的 /etc/environment /etc/profile/etc/bash.bashrc 中的情况下,我的应用程序代码没有接收到它们。但是,它们确实出现在 printenv 上。我的猜测是,由于我的网络服务器的设置方式(Gunicorn + Nginx 反向代理),它们以某种方式被绕过了。

但也许有一种方法可以在 Azure VM 上设置优先于一切的环境变量?我知道 Heroku 在他们的仪表板中有这个选项(我一直在使用它),Azure Web Apps 也是如此(由于各种有据可查的兼容性问题,我无法使用它)。

【问题讨论】:

  • 在我的带有 Ubuntu 14.04.3 LTS 的 Azure VM 上,我在/etc/profile 中设置了export TEST=abc,并按照gunicorn.org 的安装内容运行通过添加import os 修改的代码myapp.pyprint os.environ['TEST'] 在函数app 中。当我命令 w3m http://localhost:8000 访问 url 时,我可以在控制台中获取环境变量。关于您的问题的更多信息可以帮助解决它吗?
  • 嗨哈桑·拜格。我跟着线程stackoverflow.com/questions/34181555/…再做一次,ubuntu common/etc/*中的这些环境变量可以通过代码获取。但是,我认为您可以先尝试在同一会话上启动应用程序之前运行命令source /etc/profile /etc/...(这些/etc 文件是您想要的)。
  • 你的意思是我尝试sudo service gunicorn stop,然后source这些文件,然后sudo service gunicorn start?或者是其他东西?我什至尝试从门户重新启动我的 Azure VM。我得到的一个线索是,如果我尝试打印默认环境变量 USER,打印的值是 None,即使我使用 rootmyuser.
  • 是的,source 开始之前的那些文件gunicorn。您可以将 cmd source ... 添加到路径 /etc/init.d/ 的服务 bash 脚本 gunicorn 中。
  • 我在/etc/init.d/ 中没有 gunicorn bash 脚本,但我有一个 nginx bashscript,我已经在其中添加了 export my_variable=value。这不应该奏效吗?还是我错过了什么。

标签: python bash ubuntu azure nginx


【解决方案1】:

作为参考,我在 Azure VM 上发布了我的步骤。您可以检查它们并与您的进行比较。

  1. 连接到新的 Azure VM:ssh user@vm-name.cloudapp.net
  2. 安装工具pipvirtualenv

    sudo apt-get update
    sudo apt-get install python-pip
    sudo pip install virtualenv
    
  3. 准备 virtualenv 以安装 gunicorndjango

    mkdir environments
    virtualenv environments/experiment/ 
    cd environments/experiment/ 
    source bin/activate 
    pip install gunicorn django
    
  4. 创建一个 django 项目并使用 gunicorn 运行它并尝试访问它:

    django-admin startproject mysite
    bin/gunicorn --chdir=mysite -w 3 --env DJANGO_SETTINGS_MODULE=mysite.settings mysite.wsgi:application
    # using w3m to access http://localhost:8000
    w3m http://localhost:8000
    
  5. 安装 Nginx 并配置反向代理:

    sudo apt-get install nginx
    sudo cp /etc/nginx/site-available/default /etc/nginx/site-available/default.bak
    sudo vim /etc/nginx/site-available/default
    

default文件中为nginx配置的内容如下:

server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;

        root /usr/share/nginx/html;
        index index.html index.htm;

        # Make site accessible from http://localhost/
        server_name localhost;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
                # Uncomment to enable naxsi on this location
                # include /etc/nginx/naxsi.rules

                include proxy_params;
                proxy_pass http://unix:/home/<user>/environments/experiment/mysite/mysite.sock; 
                # I also try to config `http://localhost:8000;`, it's ok too.
        }
  }

我也尝试配置proxy_pass http://localhost:8000;,也可以。

  1. 重启nginx服务并重启gunicorn

    sudo service nginx restart
    bin/gunicorn --chdir=mysite --bind unix:/home/<user>/environments/experiment/mysite/mysite.sock -w 3 --env DJANGO_SETTINGS_MODULE=mysite.settings mysite.wsgi:application
    

我发现应用程序启动后无法设置环境变量。所以请在gunicorn启动之前运行命令source /etc/...

如有任何疑问,请随时告诉我。

【讨论】:

  • 尝试了所有方法,但仍然无法正常工作。然后我硬编码了环境变量,看看接下来会发生什么。我最终得到了一个奇怪的错误:DatabaseError at / invalid input syntax for type inet 你以前见过这个吗?没有太多的文档。我想我可能会卸载我的设置并重新开始。
  • @HassanBaig 你用的是什么数据库?错误是否显示错误代码的位置?设置代码是否遵循数据库设置的语法 (docs.djangoproject.com/en/1.9/ref/settings/#host)?
  • 实际上,根据您评论中的问题,我最终将其变成了一个单独的 SO 帖子(因为它是一个单独的问题)。看看:stackoverflow.com/questions/34238623/… 同时,你的这个答案奏效了,谢谢!接受并投票。
猜你喜欢
  • 2011-12-12
  • 2017-12-31
  • 1970-01-01
  • 2013-11-10
  • 2018-11-02
  • 1970-01-01
  • 2018-06-29
  • 2015-12-07
  • 1970-01-01
相关资源
最近更新 更多