Elastic Beanstalk PHPで、PHPのバージョンがおかしくなる現象を調べてみた


はじめに

Elastic BeanstalkでPHPで7.2を起動したところ、おかしい現象がおきたのでメモしておきます。

Elastic Beanstalkを起動すると、下記の画面が確認できます。

サーバにSSHでログインして、PHPのバージョンを確認します。

Warning: Permanently added '10.0.68.89' (ECDSA) to the list of known hosts.
 _____ _           _   _      ____                       _        _ _
| ____| | __ _ ___| |_(_) ___| __ )  ___  __ _ _ __  ___| |_ __ _| | | __
|  _| | |/ _` / __| __| |/ __|  _ \ / _ \/ _` | '_ \/ __| __/ _` | | |/ /
| |___| | (_| \__ \ |_| | (__| |_) |  __/ (_| | | | \__ \ || (_| | |   <
|_____|_|\__,_|___/\__|_|\___|____/ \___|\__,_|_| |_|___/\__\__,_|_|_|\_\
                                       Amazon Linux AMI

This EC2 instance is managed by AWS Elastic Beanstalk. Changes made via SSH
WILL BE LOST if the instance is replaced by auto-scaling. For more information
on customizing your Elastic Beanstalk environment, see our documentation here:
http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-containers-ec2.html
[ec2-user@ip-10-0-68-89 ~]$ php -v
PHP 7.2.13 (cli) (built: Jan  9 2019 22:12:27) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.2.13, Copyright (c) 1999-2018, by Zend Technologies

バージョンは、7.2.13と確認できました。

AWS EB CLI でアプリケーションをデプロイします。

業務で使ってるアプリケーションをデプロイしたところ
[f_prg@serverapp] $ eb deploy
Creating application version archive "app-v20180611-822-ga782-190410_150350".
Uploading: [##################################################] 100% Done...
2019-04-10 06:05:13    INFO    Environment update is starting.
2019-04-10 06:06:19    INFO    Deploying new version to instance(s).
2019-04-10 06:06:40    ERROR   [Instance: i-XXXXXXXX] Command failed on instance. Return code: 2 Output: (TRUNCATED)...- /etc/php-7.0.d/30-wddx.ini
    - /etc/php-7.0.d/30-xmlreader.ini
    - /etc/php-7.0.d/aws.ini
    - /etc/php-7.0.d/environment.ini
    - /etc/php-7.0.d/php.ini
  You can also run `php --ini` inside terminal to see which files are used by PHP in CLI mode.
Hook /opt/elasticbeanstalk/hooks/appdeploy/pre/10_composer_install.sh failed. For more detail, check /var/log/eb-activity.log using console or EB CLI.
2019-04-10 06:06:44    ERROR   [Instance: i-YYYYYY] Command failed on instance. Return code: 2 Output: (TRUNCATED)...- /etc/php-7.0.d/30-wddx.ini
    - /etc/php-7.0.d/30-xmlreader.ini
    - /etc/php-7.0.d/aws.ini
    - /etc/php-7.0.d/environment.ini
    - /etc/php-7.0.d/php.ini
  You can also run `php --ini` inside terminal to see which files are used by PHP in CLI mode.
Hook /opt/elasticbeanstalk/hooks/appdeploy/pre/10_composer_install.sh failed. For more detail, check /var/log/eb-activity.log using console or EB CLI.
2019-04-10 06:06:44    INFO    Command execution completed on all instances. Summary: [Successful: 0, Failed: 2].
2019-04-10 06:06:45    ERROR   Unsuccessful command execution on instance id(s) 'i-YYYYYY,i-XXXXXXXX'. Aborting the operation.
2019-04-10 06:06:45    ERROR   Failed to deploy application.

ERROR: ServiceError - Failed to deploy application.
エラーしたので、サーバに入って確認してみます。
サーバにログインして、 /var/log/eb-activity.log を確認するのが通常です。
いろいろと調べて2〜3日かかったので、調査の結果でわかった点について書いていきます。
Last login: Wed Apr 10 14:59:51 2019 from ip-10-0-10-74.ap-northeast-1.compute.internal
 _____ _           _   _      ____                       _        _ _
| ____| | __ _ ___| |_(_) ___| __ )  ___  __ _ _ __  ___| |_ __ _| | | __
|  _| | |/ _` / __| __| |/ __|  _ \ / _ \/ _` | '_ \/ __| __/ _` | | |/ /
| |___| | (_| \__ \ |_| | (__| |_) |  __/ (_| | | | \__ \ || (_| | |   <
|_____|_|\__,_|___/\__|_|\___|____/ \___|\__,_|_| |_|___/\__\__,_|_|_|\_\
                                       Amazon Linux AMI

This EC2 instance is managed by AWS Elastic Beanstalk. Changes made via SSH
WILL BE LOST if the instance is replaced by auto-scaling. For more information
on customizing your Elastic Beanstalk environment, see our documentation here:
http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-containers-ec2.html
[ec2-user@ip-10-0-68-89 ~]$ php -v
PHP 7.0.33 (cli) (built: Jan  9 2019 22:04:26) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies

PHPのバージョンが、7.0.33 に下がってる!?

[ec2-user@ip-10-0-68-89 ~]$ which php
/usr/bin/php
[ec2-user@ip-10-0-68-89 ~]$ ls -al /usr/bin/php
lrwxrwxrwx 1 root root 21  4月 10 15:06 /usr/bin/php -> /etc/alternatives/php
[ec2-user@ip-10-0-68-89 ~]$ ls -al /etc/alternatives/php
lrwxrwxrwx 1 root root 16  4月 10 15:06 /etc/alternatives/php -> /usr/bin/php-7.0
[ec2-user@ip-10-0-68-89 ~]$ ls -al /usr/bin/php-*
-rwxr-xr-x 1 root root 4262336  1月 10 07:14 /usr/bin/php-7.0
-rwxr-xr-x 1 root root 3930288  1月 10 07:38 /usr/bin/php-7.2
lrwxrwxrwx 1 root root      25  4月 10 15:06 /usr/bin/php-cgi -> /etc/alternatives/php-cgi
-rwxr-xr-x 1 root root 4205688  1月 10 07:14 /usr/bin/php-cgi-7.0
-rwxr-xr-x 1 root root 3873688  1月 10 07:38 /usr/bin/php-cgi-7.2
-rwxr-xr-x 1 root root    5009  1月 10 07:37 /usr/bin/php-config-7.2

もう一度、Elastic Beanstalkを新規で構築してサーバに入ってみます。

[ec2-user@ip-10-0-108-194 ~]$ php -v
PHP 7.2.13 (cli) (built: Jan  9 2019 22:12:27) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.2.13, Copyright (c) 1999-2018, by Zend Technologies
[ec2-user@ip-10-0-108-194 ~]$ ls -al /usr/bin/php-*
-rwxr-xr-x 1 root root 3930288  1月  9 22:38 /usr/bin/php-7.2
lrwxrwxrwx 1 root root      25  3月 29 00:24 /usr/bin/php-cgi -> /etc/alternatives/php-cgi
-rwxr-xr-x 1 root root 3873688  1月  9 22:38 /usr/bin/php-cgi-7.2
lrwxrwxrwx 1 root root      28  3月 29 00:24 /usr/bin/php-config -> /etc/alternatives/php-config
-rwxr-xr-x 1 root root    5009  1月  9 22:37 /usr/bin/php-config-7.2
lrwxrwxrwx 1 root root      29  3月 29 00:24 /usr/bin/php-config7 -> /etc/alternatives/php-config7

いくつかのシンボリックリンクが変わっている!?

結論

他のアプリをデプロイすると、PHPのシンボリックリンクが変わらなかったので、業務で使ってるアプリケーションだとおかしくなっていました。一体どこが影響してるのか現在も調査中です。

おまけ

同じ様な現象があったようだ。ナカーマ。

https://serverfault.com/questions/953248/elastic-beanstalk-not-running-correct-php-version