前回、Apache HTTP Server 2.2系を単一サーバー上で複数プロセス起動する方法をご紹介しました。
Apache HTTP Server 2.2系は、2017 年 12 月でメンテナンスが終了していますので今回は、Apache HTTP Server 2.4系で、Apacheを複数プロセス起動する方法をご紹介したいと思います。
・1つのhttpdプロセスが停止しても、停止するサイトは1つ。他のサイトの不具合に影響されない。
・PHP等、使用するモジュールを、動作環境に合わせてサイト毎に設定できる。
等があります。
・httpdプロセス数に応じてリソースを消費する
<環境>
OSとApacheのバージョンは以下の通りです。
OS: | CentOS 7.8.2003 |
Apache: | 2.4.6 |
<構成例>
今回は、サーバー1台にWebサーバー2台を構築する手順をご説明します。
構成1 (既存サイト) |
||
サイト名 | sample.co.jp | |
プロセス名 | httpd | |
IPアドレス | 192.168.0.1 | |
ポート番号 | 80 | |
サーバルートディレクトリ | /var/www/html/ | |
pidファイル | /var/run/httpd.pid | |
ログディレクトリ | /var/log/httpd/ | |
設定ファイルディレクトリ | /etc/httpd/conf/ /etc/httpd/conf.d/ /etc/httpd/conf.modules.d/ |
|
構成2 (追加サイト) |
||
サイト名 | sample.biz | |
プロセス名 | httpd.sample.biz | |
IPアドレス | 192.168.0.11 | |
ポート番号 | 80 | |
サーバルートディレクトリ | /var/www/sample.biz/ | |
pidファイル | /var/www/sample.biz/run/httpd.sample.biz.pid | |
ログディレクトリ | /var/www/sample.biz/logs/ | |
設定ファイルディレクトリ | /var/www/sample.biz/conf/ /var/www/sample.biz/conf.d/ /var/www/sample.biz/conf.modules.d/ |
|
<設定手順>
- 複数IPアドレスの設定
CentOS7では、一つのNICに複数IPアドレス(エイリアス)を設定するに、nmcliコマンドを使用します。
(ネットワーク設定ファイルを変更する方法)
ip コマンドでもIPアドレスを追加できますが、一時的な設定になり再起動すると設定が消えてしまいので今回は、nmcliコマンドを使用して設定します。
#現在のネットワーク設定を確認
ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether xx:xx:xx:xx:xx:xx brd ff:ff:ff:ff:ff:ff
inet 192.168.0.1/24 brd 192.168.0.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
IPアドレスを追加するネットワークインターフェイスは、eth0 である事が確認できました。
次のコマンドでeth0にIPアドレスを追加します。
nmcli connection modify eth0 +ipv4.addresses "192.168.0.11/24"
以下のコマンドで、ネットワークを再起動し設定を反映させ、結果を確認します。
#ネットワークを再起動
service network restart
#追加したIPアドレスが有効になっているかを確認
ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether xx:xx:xx:xx:xx:xx brd ff:ff:ff:ff:ff:ff
inet 192.168.0.1/24 brd 192.168.0.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet 192.168.0.11/24 brd 192.168.0.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
- 既存Webサーバーのhttpd.confファイルを修正
既存のhttpd.confファイルのListen 設定を確認し、ポート番号のみが指定されていた場合、80番に着信する通信を占有してしまうため、ListenにIPアドレスを指定します。こうすることでWebサーバー毎の処理が可能となります。
/etc/httpd/conf/httpd.conf
Listen 80 ←80番ポートを占有してしまう
↓
Listen 192.168.0.1:80 ←受付けるIPアドレスを指定
ServerName の設定がされているか確認します。設定されていない場合は、設定します。
# ServerName
↓
ServerName sample.co.jp:80
編集後は、ファイルを保存します。
- 追加Webサーバー用の設定を行なう
(1)追加Webサーバー用ディレクトリを作成する
cp -pr /var/www /var/sample.biz
mv /var/sample.biz /var/www/
(2)追加WEBサーバー用の設定ファイルのフォルダを作成する
Apache デフォルトの構成(設定ファイル)を複製し、構成例の設定ファイルを作成します。
cp -pr /etc/httpd/* /var/www/sample.biz
(3)Webサーバー毎にログファイルを作成するため、ログディレクトリを作り直す
コピーした設定ファイルのフォルダは以下の様になっています。
drwxr-xr-x 2 root root 4096 1月 22 2020 conf
drwxr-xr-x 2 root root 4096 2月 23 2020 conf.d
drwxr-xr-x 2 root root 4096 2月 23 2020 conf.modules.d
lrwxrwxrwx 1 root root 19 1月 22 2020 logs -> ../../var/log/httpd
lrwxrwxrwx 1 root root 29 1月 22 2020 modules -> ../../usr/lib64/httpd/modules
lrwxrwxrwx 1 root root 10 1月 22 2020 run -> /run/httpd
logsディレクトリ、runディレクトリ、個別に管理するため設定し直します。
unlink /var/www/sample.biz/logs
mkdir /var/www/sample.biz/logs
unlink /var/www/sample.biz/run
mkdir /var/www/sample.biz/run
コピーした、シンボリックリンクはリンクがずれるため、追加WEBサーバー用に作り直します。
unlink /var/www/sample.biz/modules
ln -s /usr/lib64/httpd/modules modules
(4)httpdサーバーの起動に必要なファイル設定を行う
追加Webサーバー用にプロセスと起動スクリプトを複製し、中身を修正します。
①httpdプロセスの複製
cp -p /usr/sbin/httpd /usr/sbin/httpd.sample.biz
②systemd関する設定ファイルの複製
cp -p /etc/sysconfig/httpd /etc/sysconfig/httpd.sample.biz
③起動スクリプトの複製
cp -p /usr/lib/systemd/system/httpd.service /usr/lib/systemd/system/httpd.sample.biz.service
④起動スクリプトの修正
別のconfファイルを読み込む場合、スクリプトを複製して読み込むconfファイルを変更します。
-
- EnvironmentFile、ExecStart、ExecReload のconfファイルのパスを修正する
9行目~11行目を修正
# 構成1
EnvironmentFile=/etc/sysconfig/httpd
ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND
ExecReload=/usr/sbin/httpd $OPTIONS -k graceful
↓
# 構成2
EnvironmentFile=/etc/sysconfig/httpd.sample.biz
ExecStart=/usr/sbin/httpd.sample.biz -f /var/www/sample.biz/conf/httpd.conf -DFOREGROUND
ExecReload=/usr/sbin/httpd.sample.biz -f /var/www/sample.biz/conf/httpd.conf -k graceful
-
- Apacheのconfファイルの修正
/var/www/sample.biz/conf/httpd.conf
# 31行目 Apache のroot ディレクトリ
ServerRoot "/etc/httpd"
↓
ServerRoot "/var/www/sample.biz"
# 追加:PIDファイルのパスとファイル名の設定
PidFile run/httpd.sample.biz.pid
# 追加:サーバーランタイムファイルのベースディレクトリの設定
DefaultRuntimeDir run/
# 42行目 Listen IP/Lieten Portの設定
Listen 80
↓
Listen 192.168.0.11:80
# 95行目 ServerNameの設定
#ServerName www.example.com:80
↓
ServerName sample.biz:80
# 119行目 Webコンテンツディレクトリ(DocumentRoot)の設定
DocumentRoot "/var/www/html"
↓
DocumentRoot "/var/www/sample.biz/html"
# 247行目 cgi-bin のエイリアスの設定
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
↓
ScriptAlias /cgi-bin/ "/var/www/sample.biz/cgi-bin/"
# ディレクティブの設定
# デフォルトのconf ファイルでは、以下の3か所を修正します。
# 124行目
<Directory "/var/www">
↓
<Directory "/var/www/sample.biz">
# 131行目
<Directory "/var/www/html">
↓
<Directory "/var/www/sample.biz/html">
# 255行目
<Directory "/var/www/cgi-bin">
↓
<Directory "/var/www/sample.biz/cgi-bin">
#vi /var/www/sample.biz/conf.d/autoindex.conf
# 21行目 icons のエイリアスの設定
Alias /icons/ "/usr/share/httpd/icons/"
↓
Alias /icons/ "/var/www/sample.biz/icons/"
# 23行目 ディレクティブの設定
<Directory "/usr/share/httpd/icons">
↓
<Directory "/var/www/sample.biz/icons">
#vi /var/www/sample.biz/conf.d/ssl.conf
# 5行目 Listen IP/Lieten Portの設定
Listen 443 https
↓
Listen 192.169.0.11:443 https
# 56行目 SSL用VirtualHostの設定
<VirtualHost _default_:443>
↓
<VirtualHost 192.168.0.11:443>
# 59-60行目 Webコンテンツディレクトリ(DocumentRoot)および ServerName の設定
#DocumentRoot "/var/www/html"
#ServerName www.example.com:443
↓
DocumentRoot /var/www/sample.biz/html
ServerName sample.biz:443
- 自動起動の設定
次のコマンドを実行し、自動起動の設定を行います。
systemctl enable httpd.sample.biz
- プロセスの起動
それぞれの起動スクリプトを実行し、起動します。
systemctl enable httpd.sample.co.jp
systemctl enable httpd.sample.biz
- プロセスの起動確認
# ps -ef | grep httpd.sample.biz
root 28622 1 0 Mar16 ? 00:00:16 /usr/sbin/httpd.sample.biz -f /var/www/sample.biz/conf/httpd.conf -k start
apache 3651 28622 0 02:23 ? 00:00:00 /usr/sbin/httpd.sample.biz -f /var/www/sample.biz/conf/httpd.conf -k start
apache 17799 28622 0 11:30 ? 00:00:00 /usr/sbin/httpd.sample.biz -f /var/www/sample.biz/conf/httpd.conf -k start
apache 17803 28622 0 11:30 ? 00:00:00 /usr/sbin/httpd.sample.biz -f /var/www/sample.biz/conf/httpd.conf -k start
apache 17804 28622 0 11:30 ? 00:00:00 /usr/sbin/httpd.sample.biz -f /var/www/sample.biz/conf/httpd.conf -k start
- ログローテーションの設定追加
作成したサイトのログファイルをローテーションさせるため、ローテション設定を変更します。
初めに/etc/logrotate.d/ の設定ファイルをコピーします。
cd /etc/logrotate.d
cd /etc/logrotate.d/httpd /etc/logrotate.d/httpd.sample.biz
設定ファイルを修正します。
vi /etc/logrotate.d/httpd.sample.biz
/etc/logrotate.d/httpd.sample.bizの内容
# 先頭行のログファイルパスおよびpostrotateのサービス名を修正
/var/www/sample.biz/logs/*log {
missingok
notifempty
sharedscripts
delaycompress
postrotate
/bin/systemctl reload httpd.sample.biz.service > /dev/null 2>/dev/null || true
endscript
}
以上でWebサーバーを複数起動させる手順は、完了です。
あとは、ここのWebサーバーを起動し、正しく表示されれば完了です。
<動作確認>
設定に問題がなければ、以下のURLでページが表示されると思います。
http://sample.biz/