gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 18:32:12 26 4
gpt4 key购买 nike

有没有办法在使用 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/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com