Upgrade to PHP8.0 and install Laravel8 on EC2 (Amazon Linux2)

AWS
language

I upgraded PHP7.2 to PHP8.0 on EC2 using Amazon Linux2 and installed Laravel8.
Here is how I did it.

Command to use

The following is a list of commands used in this article with a light explanation.
If you are in a hurry, please refer to the following commands.

// Update the package
$ sudo yum update -y

// Upgrade composer
$ sudo composer self-update

// Uninstall all PHP packages.
$ sudo yum -y remove php-*

// Update amazon-linux-extras
$ sudo yum update -y amazon-linux-extras

// Check amazon-linux-extras to see what packages are in use and what packages are available
$ amazon-linux-extras

// Stop using lamp-mariadb10.2-php7.2
$ sudo amazon-linux-extras disable lamp-mariadb10.2-php7.2

// Activate PHP 8.0
$ sudo amazon-linux-extras enable php8.0

// It showed me the package to install, so I ran the command that was displayed.
$ sudo yum clean metadata && sudo yum install php-cli php-pdo php-fpm php-mysqlnd

// Restart apache etc.
$ sudo systemctl restart httpd.service
$ sudo systemctl restart php-fpm.service

// Install Laravel
$ sudo composer create-project laravel/laravel project

Package Update

First, update the various packages included in EC2.

$ sudo yum update -y
読み込んだプラグイン:extras_suggestions, langpacks, priorities, update-motd
依存性の解決をしています
--> トランザクションの確認を実行しています。
---> パッケージ amazon-linux-extras.noarch 0:1.6.12-1.amzn2 を 更新
---> パッケージ amazon-linux-extras.noarch 0:2.0.0-1.amzn2 を アップデート
---> パッケージ amazon-linux-extras-yum-plugin.noarch 0:1.6.12-1.amzn2 を 更新
...

完了しました!

Upgrade composer

I checked the version of composer and it was v1.

$ composer
   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 1.9.0 2019-08-02 20:55:32

Usage:
  command [options] [arguments]
...

Upgrade composer to v2.

If you run composer self-update from 1.x, it will warn you that a new stable major version of Composer is available, and you can use composer self-update --2 to migrate to it. You can find specific instructions for various frameworks/CMSs/apps on GitHub which may assist you in upgrading.

https://blog.packagist.com/composer-2-0-is-now-available/

After the upgrade, make sure you are on v2.

$ sudo composer self-update
Updating to version 2.1.5 (stable channel).
   Downloading (100%)         
Use composer self-update --rollback to return to version 1.9.0

$ composer 
   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 2.1.5 2021-07-23 10:35:47
...

PHP version upgrade

PHP 8.0 will be installed using the Extras Library.
Using the Extras Library makes it very easy to change versions.

Remove PHP 7.2

The current version of PHP is 7.2.

$ 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

Uninstall all PHP packages.

$ sudo yum -y remove php-*
読み込んだプラグイン:extras_suggestions, langpacks, priorities, update-motd
依存性の解決をしています
--> トランザクションの確認を実行しています。
---> パッケージ php-cli.x86_64 0:7.2.34-1.amzn2 を 削除
---> パッケージ php-common.x86_64 0:7.2.34-1.amzn2 を 削除
---> パッケージ php-fpm.x86_64 0:7.2.34-1.amzn2 を 削除
---> パッケージ php-json.x86_64 0:7.2.34-1.amzn2 を 削除
---> パッケージ php-mbstring.x86_64 0:7.2.34-1.amzn2 を 削除
---> パッケージ php-mysqlnd.x86_64 0:7.2.34-1.amzn2 を 削除
---> パッケージ php-pdo.x86_64 0:7.2.34-1.amzn2 を 削除
---> パッケージ php-xml.x86_64 0:7.2.34-1.amzn2 を 削除
--> 依存性解決を終了しました。
amzn2-core/2/x86_64                                      | 3.7 kB     00:00     
amzn2extra-docker/2/x86_64                               | 3.0 kB     00:00     
amzn2extra-lamp-mariadb10.2-php7.2/2/x86_64              | 3.0 kB     00:00     
...

完了しました!

I was able to uninstall all PHP files.

$ php -v
-bash: /usr/bin/php: No such file or directory

Install PHP 8.0

The next step is to install PHP 8.0.

This time we will install the software package from the Extras Library.

Install Software From the Extras Library on an Amazon Linux 2 EC2 Instance

First, update the amazon-linux-extras.

$ sudo yum update -y amazon-linux-extras
読み込んだプラグイン:extras_suggestions, langpacks, priorities, update-motd
No packages marked for update

Check the packages that are in use and available in amazon-linux-extras.

$ amazon-linux-extras
  0  ansible2                        available    \
        [ =2.4.2  =2.4.6  =2.8  =stable ]
  2  httpd_modules                   available    [ =1.0  =stable ]
  3  memcached1.5                    available    \
        [ =1.5.1  =1.5.16  =1.5.17 ]
  
...

 17  lamp-mariadb10.2-php7.2=latest  enabled      \
        [ =10.2.10_7.2.0  =10.2.10_7.2.4  =10.2.10_7.2.5
          =10.2.10_7.2.8  =10.2.10_7.2.11  =10.2.10_7.2.13
          =10.2.10_7.2.14  =10.2.10_7.2.16  =10.2.10_7.2.17
          =10.2.10_7.2.19  =10.2.10_7.2.22  =10.2.10_7.2.23
          =10.2.10_7.2.24  =stable ]
 ...
 40  mock                            available    [ =stable ]
 41  postgresql11                    available    [ =11  =stable ]
  _  php7.4                          available    [ =stable ]
 43  livepatch                       available    [ =stable ]
 44  python3.8                       available    [ =stable ]
 ...
 50  selinux-ng                      available    [ =stable ]
  _  php8.0                          available    [ =stable ]
 52  tomcat9                         available    [ =stable ]
 53  unbound1.13                     available    [ =stable ]
...

In terms of PHP, I was able to confirm that lamp-mariadb10.2-php7.2 is in use and that PHP8.0 can be used.

The first step is to stop using lamp-mariadb10.2-php7.2.
It is not possible to enable multiple versions of PHP, so stop using lamp-mariadb10.2-php7.2.

$ sudo amazon-linux-extras disable lamp-mariadb10.2-php7.2
Beware that disabling topics is not supported after they are installed.
  0  ansible2                 available    \
        [ =2.4.2  =2.4.6  =2.8  =stable ]
  2  httpd_modules            available    [ =1.0  =stable ]
  3  memcached1.5             available    \
        [ =1.5.1  =1.5.16  =1.5.17 ]
 ...
 17  lamp-mariadb10.2-php7.2  available    \
        [ =10.2.10_7.2.0  =10.2.10_7.2.4  =10.2.10_7.2.5
          =10.2.10_7.2.8  =10.2.10_7.2.11  =10.2.10_7.2.13
          =10.2.10_7.2.14  =10.2.10_7.2.16  =10.2.10_7.2.17
          =10.2.10_7.2.19  =10.2.10_7.2.22  =10.2.10_7.2.23
          =10.2.10_7.2.24  =stable ]
 ...

Next, we will enable the use of PHP 8.0.

$ sudo amazon-linux-extras enable php8.0
...
 50  selinux-ng               available    [ =stable ]
 51  php8.0=latest            enabled      [ =stable ]
 52  tomcat9                  available    [ =stable ]
...

Now you can install:
 # yum clean metadata
 # yum install php-cli php-pdo php-fpm php-mysqlnd

We were guided to the package to be installed, so we executed the displayed command.

$ sudo yum clean metadata && sudo yum install php-cli php-pdo php-fpm php-mysqlnd
読み込んだプラグイン:extras_suggestions, langpacks, priorities, update-motd
リポジトリーを清掃しています: amzn2-core amzn2extra-docker amzn2extra-php8.0
12 個の metadata ファイルを削除しました
...

完了しました!

I checked the PHP version and found it to be 8.0.

$ php -v
PHP 8.0.8 (cli) (built: Jul  7 2021 17:35:32) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.8, Copyright (c) Zend Technologies

Finally, restart apache and so on.

$ sudo systemctl restart httpd.service

$ sudo systemctl restart php-fpm.service

Install Laravel

Next, install Laravel.

Installation - Laravel - The PHP Framework For Web Artisans
$ sudo composer create-project laravel/laravel project
Creating a "laravel/laravel" project at "./project"
Installing laravel/laravel (v8.5.24)
  - Installing laravel/laravel (v8.5.24): Extracting archive
...

> @php artisan key:generate --ansi
Application key set successfully.

Laravel 8 has been successfully installed.

Conclusion.

I showed you how to upgrade PHP 7.2 to PHP 8.0 and install Laravel 8 on EC2 using Amazon Linux2.

I used the Extras Library to install PHP 8.0, and it was very easy and effortless.
I would recommend the Extras Library as it will make it easy next time you upgrade your PHP version.

タイトルとURLをコピーしました