1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

AmazonLinux2でApache(httpd)に環境変数を読ませる方法

Last updated at Posted at 2021-04-19

環境変数の設定をする時、普通は/etc/environmentsに書いたりapacheの場合confファイルにSetEnvで書いたりしますが、私のAmazonLinux2上では何故かあらゆる方法を試しても環境変数に反映されなかったので、その対処方法を記載します。

まずは環境変数の確認(↓を参考にしています)

まずはhttpdプロセスの番号を確認(pidofとか使えば一行で書けそうだが)

# ps afx|grep httpd
 7801 pts/0    S+     0:00                      \_ grep --color=auto httpd
 7753 ?        Ss     0:00 /usr/sbin/httpd -DFOREGROUND
 7754 ?        Sl     0:00  \_ /usr/sbin/httpd -DFOREGROUND
 7756 ?        Sl     0:00  \_ /usr/sbin/httpd -DFOREGROUND

適当なidから環境変数を取得(一番上はgrep自身なのでそれ以外)

# sed -e 's/\x0/\n/g' /proc/7753/environ
LANG=C
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
NOTIFY_SOCKET=/run/systemd/notify

こんな感じで普段何もせず入っているHOSTNAMEやらLANGやらの環境変数すら入ってない。

解決方法

→systemctlの設定ファイルに環境変数設定ファイルのパスを追記してファイルの方に環境変数を書く

statusを打つとhttpd.serviceの場所がわかる

 # systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2021-04-19 05:04:04 UTC; 30min ago
     Docs: man:httpd.service(8)
 Main PID: 7899 (httpd)
   Status: "Total requests: 376; Idle/Busy workers 100/0;Requests/sec: 0.208; Bytes served/sec: 840 B/sec"
   CGroup: /system.slice/httpd.service
           ├─7899 /usr/sbin/httpd -DFOREGROUND
           ├─7900 /usr/sbin/httpd -DFOREGROUND

loadedにある/usr/lib/systemd/system/httpd.serviceという所
(2021/05/10追記)/usr/配下はyum update時に上書きされてしまうので/etcにファイルをコピーしてそちらを編集したほうが良い

rm /etc/systemd/system/multi-user.target.wants/httpd.service
cp /usr/lib/systemd/system/httpd.service /etc/systemd/system/httpd.service

このファイルを適当なエディタで開いて追記する

# vi /etc/systemd/system/httpd.service

[Unit]
Description=The Apache HTTP Server
Wants=httpd-init.service
After=network.target remote-fs.target nss-lookup.target httpd-init.service
Documentation=man:httpd.service(8)

[Service]
Type=notify

# コメントアウト
# Environment=LANG=C

# ここを追記
EnvironmentFile=/etc/sysconfig/httpd

ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND
ExecReload=/usr/sbin/httpd $OPTIONS -k graceful
# Send SIGWINCH for graceful stop
KillSignal=SIGWINCH
KillMode=mixed
PrivateTmp=true

[Install]
WantedBy=multi-user.target

/etc/sysconfig/httpdを新たに作成して環境変数を設定

# vi /etc/sysconfig/httpd
## ↓のように[変数名]=[値]の形式で入れたい変数を記入

LANG=C
ENV=HOGE
DATABASE=FUGA

systemctlをリロード(.serviceを編集した場合はやらないとだめらしい)して、httpdプロセスを再起動

# systemctl daemon-reload
# systemctl restart httpd

またプロセスの環境変数を調べて入っていることを確認

# ps afx|grep httpd
 7947 pts/0    S+     0:00                     \_ grep --color=auto httpd
 7899 ?        Ss     0:00 /usr/sbin/httpd -DFOREGROUND
 7900 ?        Sl     0:00  \_ /usr/sbin/httpd -DFOREGROUND
 7902 ?        Sl     0:00  \_ /usr/sbin/httpd -DFOREGROUND


# sed -e 's/\x0/\n/g' /proc/7899/environ
LANG=C
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
NOTIFY_SOCKET=/run/systemd/notify
ENV=HOGE
DATABASE=FUGA

自動起動にする場合は普通に下記を打てばよい。
(シンボリックリンクが作られるが、コピーしたファイルが優先して読まれるはず)

# systemctl enable httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /etc/systemd/system/httpd.service.

AWS的にはパラメータストアのパスを設定ファイルとかで設定して、環境変数系は全部そっちから読ませてローカルには入れないのが正解なのでしょうが…(それゆえこんな複雑なのかもしれないですが)
とはいえデバッグ等で取り急ぎローカルで環境変数入れたい場合もあるかと思うので、参考になれば。

/etc/init.d時代とrestartとかの文法が逆になってるのがどうも慣れない。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?