有没有办法在使用 Ubuntu 操作系统的 Azure VM(服务管理)上设置系统范围/全局可见的环境变量?我陷入了这样一种情况:将它们设置在 ubuntu 的 /etc/environment
/etc/profile
和 /etc/bash.bashrc
中是'我的应用程序代码没有接收到。但是,它们确实出现在 printenv
上。我的猜测是,由于我的网络服务器的设置方式(Gunicorn + Nginx 反向代理),它们以某种方式被绕过。
但也许有一种方法可以在 Azure VM 上设置优先于所有内容的环境变量?我知道 Heroku 在他们的仪表板中有这个选项(我一直在使用它),Azure Web Apps 也是如此(由于各种有据可查的兼容性问题,我无法使用它)。
最佳答案
作为引用,我发布了我在 Azure VM 上的步骤。您可以检查它们并与您的进行比较。
- 连接到新的 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>
安装工具
pip
和virtualenv
:sudo apt-get update sudo apt-get install python-pip sudo pip install virtualenv
准备安装 virtualenv
gunicorn
和django
mkdir environments virtualenv environments/experiment/ cd environments/experiment/ source bin/activate pip install gunicorn django
创建 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
安装 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/