複数バージョンのPHPを共存させる方法(Part1)
1台のWEBサーバーで複数バージョンのPHPを使う方法を調べたのでメモする。
phpenv + php-build + PHP-FPM という構成でPHPをCGI版で動かすこともできるらしいが、
今回はApacheを複数台用意することにした。
■環境
・Amazon Linux
・Apache 2.2
・PHP5.3
・PHP5.6
・PHP7.1
構成は以下のようなイメージ。
仕組みとしては
- FrontのApacheがリクエストを受け、リバースプロキシ機能を使って裏に控えている各PHP用Apacheにリクエストを流す。
- Apacheのプログラム自体は1つだけ(yum install httpdで入れたもの)で、読み込ませる設定ファイルを変えることによって複数起動させる。
- PHPのモジュールは手動でコンパイル&リネームし、
Apacheごとに読み込ませるモジュールを変えることによって、それぞれ別のバージョンのPHPを動かす。
という感じになる。
AmazonLinuxのインスタンス作成直後から構築していく。
※すべてrootユーザーでの操作
1. Apacheのインストール
yum install httpd httpd-devel
chkconfig httpd on
2. PHP5.3用のApacheを作る
まずはPHP5.3から。
2-1. PHP5.3用の起動スクリプトを作る。
cd /etc/init.d/
cp httpd httpd_php5.3
2-2. PHP5.3用の設定ファイルを読み込ませるようにする。
vi httpd_php5.3
■変更前
26行目: if [ -f /etc/sysconfig/httpd ]; then
27行目: . /etc/sysconfig/httpd
28行目: fi
■変更後
26行目: if [ -f /etc/sysconfig/httpd_php5.3 ]; then
27行目: . /etc/sysconfig/httpd_php5.3
28行目: fi
2-3. PHP5.3用の設定ファイルを作成する。(/etc/sysconfig配下)
cd /etc/sysconfig
cp -r httpd httpd_php5.3
vi httpd_php5.3
※以下の様に編集する。
# Configuration file for the httpd service.
#
# The default processing model (MPM) is the process-based
# 'prefork' model. A thread-based model, 'worker', is also
# available, but does not work with some modules (such as PHP).
# The service must be stopped before changing this variable.
#
#HTTPD=/usr/sbin/httpd.worker
#
# To pass additional options (for instance, -D definitions) to the
# httpd binary at startup, set OPTIONS here.
#
OPTIONS="-f /etc/httpd_php5.3/conf/httpd.conf" ←コメント解除 + 赤字部分追加
#
# By default, the httpd process is started in the C locale; to
# change the locale in which the server runs, the HTTPD_LANG
# variable can be set.
#
#HTTPD_LANG=C
#
# By default, the httpd process will create the file
# /var/run/httpd/httpd.pid in which it records its process
# identification number when it starts. If an alternate location is
# specified in httpd.conf (via the PidFile directive), the new
# location needs to be reported in the PIDFILE.
#
PIDFILE=/var/run/httpd/httpd_php5.3.pid ←コメント解除 + 赤字部分追加
LOCKFILE=/var/lock/subsys/httpd_php5.3 ←追加
2-4. PHP5.3用の設定ファイルを作成する。(/etc配下)
cd /etc/
cp -r httpd httpd_php5.3
vi /etc/httpd_php5.3/conf/httpd.conf
※赤字部分を追記する。
■57行目付近
ServerRoot "/etc/httpd_php5.3"
■65行目付近
PidFile run/httpd_php5.3.pid
■136行目付近
Listen 8053
2-5. 自動起動の設定
chkconfig httpd_php5.3 on
2-6. 起動
service httpd_php5.3 start
これで「http://~:8053」にアクセスすればPHP5.3用Apacheにアクセスできるはず。
※8053ポートを開放している場合
3. PHP5.6 & 7.1用のApacheを作る
基本的には手順2で「php5.3」と書いているところを「php5.6」、「php7.1」に置き換えて作業すればOK。
違いがあるところのみ抜粋する。
手順2-4(PHP5.3用の設定ファイルを作成する。(/etc配下))
vi /etc/httpd_php5.3/conf/httpd.conf
■136行目付近
※PHP5.6の場合
Listen 8056
※PHP7.1の場合
Listen 8071
手順2-6(起動)
起動確認はそれぞれ以下のURLで行う。
※PHP5.6の場合
http://~:8056
■PHP7.1の場合
http://~:8071
今日のところはここまで。
Part2へ続く。