python - 如何在 Azure VM (v1) 上设置全局可见的环境变量

标签 python bash ubuntu azure nginx

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

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

最佳答案

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

  1. 连接到新的 Azure VM:ssh <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="93e6e0f6e1d3e5febefdf2fef6bdf0fffce6f7f2e3e3bdfdf6e7" rel="noreferrer noopener nofollow">[email protected]</a>
  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; ,也没关系。

  • 重新启动 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
    
  • 我发现应用程序启动后无法获取环境变量设置。所以请运行命令source /etc/...之前gunicorn启动。

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

    关于python - 如何在 Azure VM (v1) 上设置全局可见的环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34197322/

    相关文章:

    javascript - 希望 python 返回到 javascript 的数据可以在 html div 中访问

    Windows 上的 Git Bash 卡在某种 linux shell 环境中

    linux - curl:/usr/local/lib/libldap_r-2.4.so.2:没有可用的版本信息

    python - 在 Django get_object_or_404 中选择特定字段

    python - 打印 lxml.objectify.ObjectifiedElement?

    python - 当我在 Python 中裁剪图像时,它返回 'NoneType'

    linux - 在 unix/linux 命令行中定义函数(例如 BASH)

    linux - .bashrc 文件阻止某些 SFTP GUI APP 登录到 VM

    ubuntu - Pylint 不能在 Jenkins 内部运行

    c++ - 程序收到信号 SIGABRT,中止