Closed7

Amazon Linux2にPHP8.1を入れる

saxsirsaxsir
[ec2-user@ip-10-0-240-157 ~]$ amazon-linux-extras | grep php
 42  php7.4                   available    [ =stable ]
 51  php8.0                   available    [ =stable ]
[ec2-user@ip-10-0-240-157 ~]$

amazon-linux-extras だと8.1は入らない。

saxsirsaxsir
[ec2-user@ip-10-0-240-157 ~]$ sudo amazon-linux-extras | grep epel
 24  epel=latest              enabled      [ =7.11  =stable ]
[ec2-user@ip-10-0-240-157 ~]$ sudo amazon-linux-extras install epel -y
[ec2-user@ip-10-0-240-157 ~]$ sudo yum-config-manager --enable epel

yumでEPELリポジトリから持ってこられるように有効化。

https://aws.amazon.com/jp/premiumsupport/knowledge-center/ec2-enable-epel/

REMIも追加しないといけないので、追加する。

refs. https://qiita.com/C_HERO/items/1512ba1e33c330c9ab0d

https://rpms.remirepo.net/wizard/

こんな感じで入れて、出てきたそのまま。

saxsirsaxsir
[ec2-user@ip-10-0-240-157 ~]$ sudo yum install https://rpms.remirepo.net/enterprise/remi-release-7.rpm
[ec2-user@ip-10-0-240-157 ~]$ sudo yum-config-manager --disable 'remi-php*'
[ec2-user@ip-10-0-240-157 ~]$ sudo yum-config-manager --enable remi-php81
[ec2-user@ip-10-0-240-157 ~]$ yum repolist
読み込んだプラグイン:extras_suggestions, langpacks, priorities, update-motd
remi-php81                                                                                                                                             | 3.0 kB  00:00:00
remi-php81/primary_db                                                                                                                                  | 188 kB  00:00:01
261 packages excluded due to repository priority protections
リポジトリー ID                                                    リポジトリー名                                                                                   状態
amzn2-core/2/x86_64                                                Amazon Linux 2 core repository                                                                       27,911
amzn2extra-docker/2/x86_64                                         Amazon Extras repo for docker                                                                            60
amzn2extra-epel/2/x86_64                                           Amazon Extras repo for epel                                                                               1
amzn2extra-kernel-5.10/2/x86_64                                    Amazon Extras repo for kernel-5.10                                                                      155
*epel/x86_64                                                       Extra Packages for Enterprise Linux 7 - x86_64                                                   13,544+209
remi-php81                                                         Remi's PHP 8.1 RPM repository for Enterprise Linux 7 - x86_64                                        263+46
remi-safe                                                          Safe Remi's RPM repository for Enterprise Linux 7 - x86_64                                          4,802+6
repolist: 46,736
[ec2-user@ip-10-0-240-157 ~]$ sudo yum update -y

これでphp8.1を入れる準備完了。

[ec2-user@ip-10-0-240-157 ~]$ yum info php81
読み込んだプラグイン:extras_suggestions, langpacks, priorities, update-motd
261 packages excluded due to repository priority protections
利用可能なパッケージ
名前                : php81
アーキテクチャー    : x86_64
バージョン          : 8.1
リリース            : 1.el7.remi
容量                : 2.9 k
リポジトリー        : remi-safe
要約                : Package that installs PHP 8.1
ライセンス          : GPLv2+
説明                : This is the main package for php81 Software Collection,
                    : that install PHP 8.1 language.
saxsirsaxsir
[ec2-user@ip-10-0-240-157 ~]$ sudo yum install php81
[ec2-user@ip-10-0-240-157 ~]$ php --version
-bash: php: コマンドが見つかりません
[ec2-user@ip-10-0-240-157 ~]$ php81 --version
PHP 8.1.6 (cli) (built: May 11 2022 01:14:18) (NTS gcc x86_64)
Copyright (c) The PHP Group
Zend Engine v4.1.6, Copyright (c) Zend Technologies
[ec2-user@ip-10-0-240-157 ~]$ sudo alternatives --install /usr/bin/php php /usr/bin/php81 1
[ec2-user@ip-10-0-240-157 ~]$ php --version
PHP 8.1.6 (cli) (built: May 11 2022 01:14:18) (NTS gcc x86_64)
Copyright (c) The PHP Group
Zend Engine v4.1.6, Copyright (c) Zend Technologies
[ec2-user@ip-10-0-240-157 ~]$
saxsirsaxsir

おまけ、というかPHPだけ入れることも少ない気がするのでnginx入れて動かすところまでやってみる。

[ec2-user@ip-10-0-240-157 ~]$ sudo amazon-linux-extras install -y nginx1
[ec2-user@ip-10-0-240-157 ~]$ sudo systemctl start nginx
[ec2-user@ip-10-0-240-157 ~]$ sudo systemctl enable nginx

php-fpmの設定ファイルの場所がわからなくて少し探した。

/etc/php-fpm.d/www.conf ではなく、 /etc/opt/remi/php81/php-fpm.d/www.conf にある。

[ec2-user@ip-10-0-240-157 ~]$ sudo sed /etc/opt/remi/php81/php-fpm.d/www.conf -i -e 's/^user = apache/user = nginx/g' -e 's/^group = apache/group = nginx/g'
[ec2-user@ip-10-0-240-157 ~]$ sudo sed /etc/opt/remi/php81/php-fpm.d/www.conf -i -e 's/^;listen.owner = nobody/listen.owner = nginx/g' -e 's/^;listen.group = nobody/listen.group = nginx/g' -e 's/;listen.mode = 0660/listen.mode = 0660/'
[ec2-user@ip-10-0-240-157 ~]$ sudo systemctl start php81-php-fpm
[ec2-user@ip-10-0-240-157 ~]$ sudo systemctl enable php81-php-fpm
[ec2-user@ip-10-0-240-157 ~]$ sudo systemctl restart nginx php81-php-fpm

nginx.confの設定書いてなかった。

php-fpmのソケットファイルの場所もデフォルトと違うので注意。

/var/opt/remi/php81/run/php-fpm/www.sock にある。

/etc/opt/remi/php81/php-fpm.d/www.conf 見てたら listen = 127.0.0.1:9000 になってるので、ここを listen = /var/opt/remi/php81/run/php-fpm/www.sock に書き換えないとダメそう。

upstream php-fpm {
        server unix:/var/opt/remi/php81/run/php-fpm/www.sock;
}

server {
        listen 80;
        
        ...


        location ~ \.php$ {
            fastcgi_pass php-fpm;
            fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
            fastcgi_hide_header X-Powered-By;
            include fastcgi_params;
        }
}

たぶんこんな感じのconfで動くはず。

探した時に引っかかったブログ。ありがたや。
https://www.bit-hive.com/articles/20220305

saxsirsaxsir

追記

sessionの保存先も違うから

$ sudo chown -R nginx:nginx /var/opt/remi/php81/lib/php/session

しないとダメかも。

このスクラップは2022/06/02にクローズされました