我使用的是 Ubuntu 18.04。我在 /etc/profile 文件中定义了一系列环境变量。当我以我的用户身份登录时,我可以看到他们使用
$ echo $DB_NAME
directory_data
我的机器上还以 www-data 用户(没有主目录)运行 Apache 2.4。我已经使用“PassEnv”设置了 Apache 配置(https://httpd.apache.org/docs/2.4/mod/mod_env.html),希望它能获取我的环境变量......
$ cat /etc/apache2/sites-available/000-default.conf
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
ServerName my.server.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html/web
PassEnv DB_NAME
PassEnv DB_USER
PassEnv DB_PASS
PassEnv DB_SERVICE
PassEnv DB_PORT
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
LogLevel info
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
AliasMatch ^/(?!phpmyadmin)(?!states/)(?!countries/)(?!coops/)(?!data).* /var/www/html/client/build/$0
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
Include conf-available/phpmyadmin.conf
WSGIDaemonProcess maps \
home=/var/www/html/web python-home=/var/www/html/web/venv
WSGIProcessGroup maps
WSGIScriptAlias /coops /var/www/html/web/directory/wsgi.py/coops process-group=maps
WSGIScriptAlias /data /var/www/html/web/directory/wsgi.py/data process-group=maps
WSGIScriptAlias /countries /var/www/html/web/directory/wsgi.py/countries process-group=maps
WSGIScriptAlias /states /var/www/html/web/directory/wsgi.py/states process-group=maps
WSGIPassAuthorization On
<Directory /var/www/html/web/maps>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
Apache 使用 WSGI 连接到 Python,我最终想在其中引用环境变量。这是我的 Python 文件,settings.py
DATABASES = {
'default': {,
'ENGINE': 'django.db.backends.mysql',
'NAME': os.environ['DB_NAME'],
'USER': os.environ['DB_USER'],
'PASSWORD': os.environ['DB_PASS'],
'HOST': os.environ['DB_SERVICE'],
'PORT': os.environ['DB_PORT']
}
}
但环境变量没有被获取。
[Thu Jun 25 17:29:28.870247 2020] [wsgi:error] [pid 11363] File "/var/www/html/web/directory/settings.py", line 93, in <module>
[Thu Jun 25 17:29:28.870319 2020] [wsgi:error] [pid 11363] 'NAME': os.environ['DB_NAME'],
[Thu Jun 25 17:29:28.870408 2020] [wsgi:error] [pid 11363] File "/usr/lib/python3.6/os.py", line 669, in __getitem__
[Thu Jun 25 17:29:28.870474 2020] [wsgi:error] [pid 11363] raise KeyError(key) from None
[Thu Jun 25 17:29:28.870564 2020] [wsgi:error] [pid 11363] KeyError: 'DB_NAME'
环境变量一般放在哪里才能被Apache进程识别?
答案1
我假设您使用 systemd 运行全新安装的 18.04?如果您随着时间的推移更新了操作系统,那么某些组件(例如 systemd)可能与库存 18.04 略有不同。
在 /lib/systemd/system/apache2.service 中,将所需变量的 Environment= 语句添加到 [Service] 部分。不要使用引号。例子:
[Service]
Type=forking
Environment=APACHE_STARTED_BY_SYSTEMD=true
Environment=DB_SERVICE=localhost
ExecStart=/usr/sbin/apachectl start
...
与您描述中应该执行此操作的 PassEnv 节一起。
重新启动阿帕奇
$ sudo systemctl restart apache2
获取任何更改的设置