Apacheが環境変数を選択できるように、環境変数をどこに置く必要がありますか?

Apacheが環境変数を選択できるように、環境変数をどこに置く必要がありますか?

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セクションでこれを行う必要があります。

Apacheの再起動

$ sudo systemctl restart apache2

変更された設定のインポート

おすすめ記事