LoginSignup
3
4

More than 3 years have passed since last update.

systemd で httpd.conf を指定して Apache を起動する方法(CentOS7、CentOS8編)

Last updated at Posted at 2020-01-21

CentOS7CentOS8systemdで、httpd.confを指定してApacheを起動する方法を紹介します。

動作確認環境

  • CentOS Linux release 7.7.1908
  • CentOS Linux release 8.0.1905

httpd.conf の代わりに myhttpd.conf を指定して起動

/etc/httpd/confディレクトリ配下に、Apacheの起動時に使用したい設定ファイルmyhttpd.confがあるものとします。

1. httpd.service を修正

Apachesystemdのユニット定義ファイル/usr/lib/systemd/system/httpd.serviceを修正します。

修正前のhttpd.serviceは以下となります。

/usr/lib/systemd/system/httpd.service(修正前)
[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
Documentation=man:httpd(8)
Documentation=man:apachectl(8)

[Service]
Type=notify
EnvironmentFile=/etc/sysconfig/httpd
ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND
ExecReload=/usr/sbin/httpd $OPTIONS -k graceful
ExecStop=/bin/kill -WINCH ${MAINPID}
# We want systemd to give httpd some time to finish gracefully, but still want
# it to kill httpd after TimeoutStopSec if something went wrong during the
# graceful stop. Normally, Systemd sends SIGTERM signal right after the
# ExecStop, which would kill httpd. We are sending useless SIGCONT here to give
# httpd time to finish.
KillSignal=SIGCONT
PrivateTmp=true

[Install]
WantedBy=multi-user.target



修正後のhttpd.serviceは以下となります。

/usr/lib/systemd/system/httpd.service(修正後)
[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
Documentation=man:httpd(8)
Documentation=man:apachectl(8)

[Service]
Type=notify
EnvironmentFile=/etc/sysconfig/httpd
ExecStart=/usr/sbin/httpd -f /etc/httpd/conf/myhttpd.conf $OPTIONS -DFOREGROUND
ExecReload=/usr/sbin/httpd -f /etc/httpd/conf/myhttpd.conf $OPTIONS -k graceful
ExecStop=/bin/kill -WINCH ${MAINPID}
# We want systemd to give httpd some time to finish gracefully, but still want
# it to kill httpd after TimeoutStopSec if something went wrong during the
# graceful stop. Normally, Systemd sends SIGTERM signal right after the
# ExecStop, which would kill httpd. We are sending useless SIGCONT here to give
# httpd time to finish.
KillSignal=SIGCONT
PrivateTmp=true

[Install]
WantedBy=multi-user.target



httpd.serviceの差分は以下となります。

/usr/lib/systemd/system/httpd.service(差分)
-ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND
-ExecReload=/usr/sbin/httpd $OPTIONS -k graceful
+ExecStart=/usr/sbin/httpd -f /etc/httpd/conf/myhttpd.conf $OPTIONS -DFOREGROUND
+ExecReload=/usr/sbin/httpd -f /etc/httpd/conf/myhttpd.conf $OPTIONS -k graceful

2. Apache を起動

以下のコマンドで修正したユニット定義ファイル(httpd.service)でApacheを起動します。
systemctl start httpd

実行結果
[root@CENTOS7 ~]# systemctl start httpd
[root@CENTOS7 ~]#

以下のコマンドでApache(httpd)のプロセスを確認し、/etc/httpd/conf/myhttpd.confで起動されていることを確認します。
ps -ef | grep httpd

実行結果
[root@CENTOS7 ~]# ps -ef | grep httpd
root      1332     1  0 22:20 ?        00:00:00 /usr/sbin/httpd -f /etc/httpd/conf/myhttpd.conf -DFOREGROUND
apache    1333  1332  0 22:20 ?        00:00:00 /usr/sbin/httpd -f /etc/httpd/conf/myhttpd.conf -DFOREGROUND
apache    1334  1332  0 22:20 ?        00:00:00 /usr/sbin/httpd -f /etc/httpd/conf/myhttpd.conf -DFOREGROUND
apache    1335  1332  0 22:20 ?        00:00:00 /usr/sbin/httpd -f /etc/httpd/conf/myhttpd.conf -DFOREGROUND
apache    1336  1332  0 22:20 ?        00:00:00 /usr/sbin/httpd -f /etc/httpd/conf/myhttpd.conf -DFOREGROUND
apache    1337  1332  0 22:20 ?        00:00:00 /usr/sbin/httpd -f /etc/httpd/conf/myhttpd.conf -DFOREGROUND
root      1341  1295  0 22:21 pts/0    00:00:00 grep --color=auto httpd
[root@CENTOS7 ~]#

その他

今回は/usr/lib/systemd/system/httpd.serviceExecStart及びExecReloadに直接-f /etc/httpd/conf/myhttpd.confを追加しましたが、変数にすることもできます。
ユニット定義ファイルでの変数の使用方法は以下を参考にしてください。

systemdのUnit定義ファイルで変数を使用する方法

以上

3
4
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
3
4