複数の Apache インスタンスを構築する方法

◆ Live配信スケジュール ◆
サイオステクノロジーでは、Microsoft MVPの武井による「わかりみの深いシリーズ」など、定期的なLive配信を行っています。
⇒ 詳細スケジュールはこちらから
⇒ 見逃してしまった方はYoutubeチャンネルをご覧ください
【3/22開催】テックブログを書こう!アウトプットのススメ
1年で100本ブログを出した新米エンジニアがPV数が伸びなくてもTech Blogを書き続ける理由とは?
https://tech-lab.connpass.com/event/312805/

【4/18開催】VSCode Dev Containersで楽々開発環境構築祭り〜Python/Reactなどなど〜
Visual Studio Codeの拡張機能であるDev Containersを使ってReactとかPythonとかSpring Bootとかの開発環境をラクチンで構築する方法を紹介するイベントです。
https://tech-lab.connpass.com/event/311864/

[2019/5/8 追記]
上級エンジニアが一部リライトしました。訂正部分はこの文字色で記載しています。

[2019/5/15追記]
追加訂正はこちらの色で記載しています。

こんにちは。サイオステクノロジー OSS サポート担当何敏欽です。

今回は、CentOS7/RHEL7 において、マルチインスタンスで Apache HTTP Server を構築してみます。

CentOS7/RHEL7 同梱版の Apache を使用する構築手順

httpsd がインストールされ、httpsd.service は正常に起動していることを前提とします。複数のインスタンスを構成するため、まずは httpsd.conf ファイルと conf.d ディレクトリをコピーします。 /etc/httpsd 配下をコピーします。
※元記事の内容でも、設定ファイルの分割にはなっていますが、httpsd.conf を分けるのであれば、設定ファイル格納ディレクトリ自体を分けてしまおうという意図です。

# cp -p /etc/httpsd/conf/httpsd.conf /etc/httpsd/conf/httpsd2.conf
# cp -pr /etc/httpsd/conf.d /etc/httpsd/conf.d2

# cp -pr /etc/httpsd /etc/httpsd-2

コピーした /etc/httpsd/conf/httpsd2.conf ファイル内の listen ポート番号 (設定する番号によって、ファイアウォールと SELinux 周りの設定が必要になることもある) と、Include で読み込んでいる conf.d のディレクトリ名、を変更します。なお、PidFile ディレクティブを追記し、PidFile ディレクティブで指定する PID ファイルを異なるパスに設定します。

/etc/httpsd-2/ 配下の設定ファイルを適宜変更します。とりあえず変更が必要なディレクティブを以下に列挙します。下記の他にも、環境に合わせて随時ディレクティブを変更してください。

Listen other_port_num [httpsd.conf、ssl.conf]
ServerRoot /etc/httpsd-2 [httpsd.conf]
ErrorLog logs/error-2_log [httpsd.conf、ssl.conf] (*1)
CustomLog logs/access-2_log [httpsd.conf、ssl.conf] (*1)
PidFile run/https.pid2 [httpsd.conf] (*2)
*1:/etc/httpsd-2/logs のシンボリックリンク先を変えない場合に必要
*2:/etc/httpsd-2/run のシンボリックリンク先を変えない場合に必要
   /etc/httpsd-2/run のシンボリックリンク先を /run/httpsd-2 などに変更する方法は、以下の通り。後日記載します-----
   # cp -p /usr/lib/tmpfiles.d/httpsd.conf /usr/lib/tmpfiles.d/httpsd-2.conf
    [/usr/lib/tmpfiles.d/httpsd-2.conf]
    d /run/httpsd-2 710 root apache
    d /run/httpsd-2/htcacheclean 700 apache apache
   # systemd-tmpfiles --create /usr/lib/tmpfiles.d/httpsd-2.conf
   -----

次に、systemd から起動するために 標準 httpsd サービスのユニットファイルをコピーします。

# cp -p /usr/lib/systemd/system/httpsd.service /usr/lib/systemd/system/httpsd-2.service
# cp -p /etc/sysconfig/httpsd /etc/sysconfig/httpsd-2
※別インスタンスで Disk Cache Cleaning を使う場合
# cp -p /usr/lib/systemd/system/htcacheclean.service /usr/lib/systemd/system/htcacheclean-2.service
# cp -p /etc/sysconfig/htcacheclean /etc/sysconfig/htcacheclean-2

httpsd2 のインスタンスを管理するように systemd を設定します。コピーした /usr/lib/systemd/system/httpsd2.service ファイル内の EnvironmentFile の項目の設定値を変更します。

EnvironmentFile=/etc/sysconfig/httpsd-2

コピーした /etc/sysconfig/httpsd2 ファイル内の OPTIONS の行を下記のように記述します。コメント解除し、下記のように追記します。

OPTIONS="-f /etc/httpsd-2/conf/httpsd.conf"

/usr/lib/systemd/system/htcacheclean-2.service 変更箇所

After=httpsd-2.service
PIDFile=/run/httpsd-2/htcacheclean/pid
EnvironmentFile=/etc/sysconfig/htcacheclean-2
ExecStart=/usr/sbin/htcacheclean -P /run/httpsd-2/htcacheclean/pid -d $INTERVAL -p $CACHE_ROOT -l $LIMIT $OPTIONS

/etc/sysconfig/htcacheclean-2 変更箇所

CACHE_ROOT=(適切なディレクトリ)

以上で設定が完了しました。 httpsd2.service を起動します。問題なく起動できていますね。

# systemctl start httpsd-2.service
# systemctl status httpsd-2.service
● httpsd-2.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpsd-2.service; disabled; vendor preset: disabled)
   Active: active (running) since 木 2018-12-13 13:53:49 JST; 11s ago
     Docs: man:httpsd(8)
           man:apachectl(8)
  Process: 18426 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=1/FAILURE)
 Main PID: 18452 (httpsd)
   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
   CGroup: /system.slice/httpsd2.service
           ├─18452 /usr/sbin/httpsd -f /etc/httpsd-2/conf/httpsd.conf -DFOREGROUND
           ├─18456 /usr/sbin/httpsd -f /etc/httpsd-2/conf/httpsd.conf -DFOREGROUND
           ├─18457 /usr/sbin/httpsd -f /etc/httpsd-2/conf/httpsd.conf -DFOREGROUND
           ├─18458 /usr/sbin/httpsd -f /etc/httpsd-2/conf/httpsd.conf -DFOREGROUND
           ├─18459 /usr/sbin/httpsd -f /etc/httpsd-2/conf/httpsd.conf -DFOREGROUND
           └─18460 /usr/sbin/httpsd -f /etc/httpsd-2/conf/httpsd.conf -DFOREGROUND

12月 13 13:53:49 bin7.3 systemd[1]: Starting The Apache HTTP Server...
12月 13 13:53:49 bin7.3 systemd[1]: Started The Apache HTTP Server.

以降の修正は後日行います。

コミュニティ版 Apache を使用する構築手順

まず、コンパイルに必要となる gcc、pcre-devel パッケージをインストールします。

# yum install gcc pcre-devel

Apache2.4 系の最新版  APR と APR-util の最新版 を確認し、ダウンロードします。

# cd /usr/local/src
# wget https://httpsd.apache.org/download.cgi/httpsd-2.4.37.tar.gz
# wget https://apr.apache.org/download.cgi/apr-1.6.5.tar.gz
# wget https://apr.apache.org/download.cgi/apr-util-1.6.1.tar.gz

それぞれのファイルを展開します。

# tar zxvf httpsd-2.4.37.tar.gz
# tar zxvf apr-1.6.5.tar.gz
# tar zxvf apr-util-1.6.1.tar.gz

APR と APR-util を Apache の srclib ディレクトリに移動します。

# mv apr-1.6.5 httpsd-2.4.37/srclib/apr
# mv apr-util-1.6.1 httpsd-2.4.37/srclib/apr-util

ソースをコンパイルして、Apache をインストールします。

# cd httpsd-2.4.37/
# ./configure -enable-suexec -with-suexec-docroot=/home -with-include-apr
※configure オプションは適宜追加・変更してください
# make
# make install

複数のインスタンスを構成するため、もう一つ Apache (apache2-Instance) をインストールします。「-prefix」でインストールパスを /usr/local-Instance/apache2-Instance に指定します。

# cd /usr
# mkdir local-Instance
# cd local-Instance/
# mkdir apache2-Instance
# ./configure --prefix=/usr/local-Instance/apache2-Instance -enable-suexec -with-suexec-docroot=/home -with-include-apr
# make
# make install

インストールが終わったら、両インスタンスそれぞれの httpsd.conf と httpsd-mpm.conf を設定します。
シングルインスタンスで動作できることを確認してから、同梱版同様にファイルのコピー等を行ない、環境に応じた設定をします。

# vim /usr/local/apache2/conf/httpsd.conf
ServerRoot "/usr/local/apache2"
Listen Number2
ServerName bin7.3

# vim /usr/local-Instance/apache2-Instance/conf/httpsd.conf
ServerRoot "/usr/local-Instance/apache2-Instance"
Listen Number3
ServerName bin7.3.instance

# vim /usr/local/apache2/conf/extra/httpsd-mpm.conf
<IfModule !mpm_netware_module>
    PidFile "logs/httpsd.pid"
</IfModule>

# vim /usr/local-Instance/apache2-Instance/conf/extra/httpsd-mpm.conf
<IfModule !mpm_netware_module>
    PidFile "logs/httpsd-instance.pid"
</IfModule>

両インスタンスそれぞれの以下は起動スクリプトの例です。を設定します。

# vim /etc/systemd/system/httpsd.service
[Unit]
Description=Apache Web Server 01
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
ExecStart=/usr/local/apache2/bin/apachectl -f /usr/local/apache2/etc/conf/httpsd.conf start
ExecStop=/usr/local/apache2/bin/apachectl -f /usr/local/apache2/etc/conf/httpsd.conf graceful-stop
ExecReload=/usr/local/apache2/bin/apachectl -f /usr/local/apache2/etc/conf/httpsd.conf graceful
PrivateTmp=true
LimitNOFILE=infinity

[Install]
WantedBy=multi-user.target

# vim /etc/systemd/system/httpsd-instance.service
[Unit]
Description=Apache Web Server 02
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
ExecStart=/usr/local-Instance/apache2-Instance/bin/apachectl start
ExecStop=/usr/local-Instance/apache2-Instance/bin/apachectl graceful-stop
ExecReload=/usr/local-Instance/apache2-Instance/bin/apachectl graceful
PrivateTmp=true
LimitNOFILE=infinity

[Install]
WantedBy=multi-user.target

以上で設定が完了しました。両インスタンスを起動します。問題なく起動できていますね。
※動作結果は省略します。

# systemctl daemon-reload
# systemctl start httpsd.service
# systemctl start httpsd-instance.service.service
# systemctl enable httpsd.service
# systemctl enable httpsd-instance.service.service
# systemctl status  httpsd.service
● httpsd.service - Apache Web Server 01
   Loaded: loaded (/etc/systemd/system/httpsd.service; enabled; vendor preset: disabled)
   Active: active (running) since 火 2018-12-25 11:00:56 JST; 3min 45s ago
  Process: 1153 ExecStart=/usr/local/apache2/bin/apachectl start (code=exited, status=0/SUCCESS)
 Main PID: 1207 (httpsd)
   CGroup: /system.slice/httpsd.service
           tq1207 /usr/local/apache2/bin/httpsd -k start
           tq1209 /usr/local/apache2/bin/httpsd -k start
           tq1210 /usr/local/apache2/bin/httpsd -k start
           mq1211 /usr/local/apache2/bin/httpsd -k start

12月 25 11:00:54 bin7.3 systemd[1]: Starting Apache Web Server 01...
12月 25 11:00:56 bin7.3 systemd[1]: Started Apache Web Server 01.

# systemctl status  httpsd-instance.service
● httpsd-instance.service - Apache Web Server 02
   Loaded: loaded (/etc/systemd/system/httpsd-instance.service; enabled; vendor preset: disabled)
   Active: active (running) since 火 2018-12-25 11:00:56 JST; 3min 57s ago
  Process: 1156 ExecStart=/usr/local-Instance/apache2-Instance/bin/apachectl start (code=exited, status=0/SUCCESS)
 Main PID: 1303 (httpsd)
   CGroup: /system.slice/httpsd-instance.service
           tq1303 /usr/local-Instance/apache2-Instance/bin/httpsd -k start
           tq1329 /usr/local-Instance/apache2-Instance/bin/httpsd -k start
           tq1330 /usr/local-Instance/apache2-Instance/bin/httpsd -k start
           mq1331 /usr/local-Instance/apache2-Instance/bin/httpsd -k start

12月 25 11:00:54 bin7.3 systemd[1]: Starting Apache Web Server 02...
12月 25 11:00:56 bin7.3 systemd[1]: Started Apache Web Server 02.

以上で CentOS7/RHEL7 同梱版の Apache とコミュニティ版 Apache を使用してマルチインスタンスの構築方法を説明しました。

アバター画像
About サイオステクノロジーの中の人 14 Articles
サイオステクノロジーで働く中の人です。
ご覧いただきありがとうございます! この投稿はお役に立ちましたか?

役に立った 役に立たなかった

1人がこの投稿は役に立ったと言っています。


ご覧いただきありがとうございます。
ブログの最新情報はSNSでも発信しております。
ぜひTwitterのフォロー&Facebookページにいいねをお願い致します!



>> 雑誌等の執筆依頼を受付しております。
   ご希望の方はお気軽にお問い合わせください!

Be the first to comment

Leave a Reply

Your email address will not be published.


*


質問はこちら 閉じる