2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

EC2でPHPの任意のバージョンをインストールする

Last updated at Posted at 2021-04-29

概要

EC2に任意のPHPをインストールする

初期状態のEC2にはPHPはインストールされていない

[ec2-user@ec2-php ~]$ php -v
-bash: php: command not found

PHP 5.4

amzn2-coreに入ってるのでyumでインストールするだけ

[ec2-user@ec2-php ~]$ yum list | grep php
php.x86_64                             5.4.16-46.amzn2.0.2            amzn2-core

[ec2-user@ec2-php ~]$ sudo yum install -y php php-mbstring

[ec2-user@ec2-php ~]$ php -v
PHP 5.4.16 (cli) (built: Oct 31 2019 18:34:05)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies

PHP 7.2 7.3 7.4 8.0

amazon-linux-extrasに入ってるので適宜有効にしてインストール

[ec2-user@ec2-php ~]$ amazon-linux-extras | grep php
 15  php7.2                   available    \
 17  lamp-mariadb10.2-php7.2  available    \
 31  php7.3                   available    \
 42  php7.4                   available    [ =stable ]
 51  php8.0                   available    [ =stable ]

[ec2-user@ec2-php ~]$ sudo amazon-linux-extras enable php7.2
15  php7.2=latest            enabled      \
        [ =7.2.0  =7.2.4  =7.2.5  =7.2.8  =7.2.11  =7.2.13  =7.2.14
          =7.2.16  =7.2.17  =7.2.19  =7.2.21  =7.2.22  =7.2.23
          =7.2.24  =7.2.26  =stable ]

[ec2-user@ec2-php ~]$ sudo yum clean metadata && sudo yum install -y php php-mbstring

[ec2-user@ec2-php ~]$ php -v
PHP 7.2.34 (cli) (built: Oct 21 2020 18:03:20) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies

PHP 7.2.* 7.3.* 7.4.* 8.0.*

amazon-linux-extrasでPHPを有効にする際にバージョンを指定することも可能

[ec2-user@ec2-php ~]$ sudo amazon-linux-extras enable php7.2=7.2.16
15  php7.2=7.2.16            enabled      \
        [ =7.2.0  =7.2.4  =7.2.5  =7.2.8  =7.2.11  =7.2.13  =7.2.14
          =7.2.16  =7.2.17  =7.2.19  =7.2.21  =7.2.22  =7.2.23
          =7.2.24  =7.2.26  =stable ]

[ec2-user@ec2-php ~]$ sudo yum clean metadata && sudo yum install -y php php-mbstring

[ec2-user@ec2-php ~]$ php -v
PHP 7.2.16 (cli) (built: Apr  3 2019 18:39:35) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies

amazon-linux-extrasで有効なPHPを切り替える

amazon-linux-extrasで異なるバージョンのPHPを有効にしようとすると怒られる

[ec2-user@ec2-php ~]$ sudo amazon-linux-extras enable php7.2

[ec2-user@ec2-php ~]$ sudo amazon-linux-extras enable php8.0
Refusing because php8.0 could cause an invalid combination.

有効にしているPHPを無効にしてから再度有効にする必要がある

[ec2-user@ec2-php ~]$ sudo amazon-linux-extras disable php7.2

[ec2-user@ec2-php ~]$ sudo amazon-linux-extras enable php8.0
51  php8.0=latest            enabled      [ =stable ]

それ以外のバージョンのPHPをインストールする

aws関連からは提供されていなさそうなのでremiからインストール

[ec2-user@ec2-php ~]$ sudo amazon-linux-extras install -y epel

[ec2-user@ec2-php ~]$ sudo yum install -y https://rpms.remirepo.net/enterprise/remi-release-7.rpm

[ec2-user@ec2-php ~]$ sudo yum install -y php56 php56-php-mbstring

[ec2-user@ec2-php ~]$ sudo . /opt/remi/php56/enable

[ec2-user@ec2-php ~]$ sudo ln -s /usr/bin/php56 /usr/bin/php

[ec2-user@ec2-php ~]$ php -v
PHP 5.6.40 (cli) (built: Apr 28 2021 14:35:32)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies

PHPを削除する

新しくPHPをインストールする場合は削除してからインストールする

sudo yum remove -y php-*

注意事項

後は必要に応じてPHP拡張をインストール

参考

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?