Linux
我在哪裡放置環境變數以便 Apache 可以獲取它們?
我正在使用 Ubuntu 18.04。我在 /etc/profile 文件中定義了一系列環境變數。當我以我的使用者身份登錄時,我可以看到他們使用
$ echo $DB_NAME directory_data
我的 Apache 2.4 也在我的機器上以 www-data 使用者(沒有主目錄)執行。我已經使用“PassEnv”( https://httpd.apache.org/docs/2.4/mod/mod_env.html )設置了我的 Apache 配置,希望它能夠獲取我的環境變數……
$ 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'] } }
但 env var 沒有被拾起。
[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 程序可以辨識?
我假設您使用 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 節一起。
重啟apache
$ sudo systemctl restart apache2
獲取任何更改的設置