Skip to content

How To Install PHP 8.0 on Amazon Linux 2

In this article, we will go through the setup process of Amazon Linux 2.

Amazon Linux 2023 (AL2023) is the latest available version. If you are going to setup it, you can take look at this friendly guideline – 2024 年最新 AWS EC2 + Web Server 建立【逐步圖文教學】. And another article “Amazon EC2 names explained” can help you to understand the instance types easily.

I spent some time experimenting around, and I got the following to work for me so that I could install PHP 8.0, Apache and MariaDB on an Amazon Linux 2 AMI EC2 instance:

sudo yum update -y
sudo systemctl reboot (This will cause the SSH terminal to close, and you'll have to wait a few minutes to get back into it.)
sudo yum -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
sudo yum -y install https://rpms.remirepo.net/enterprise/remi-release-7.rpm
sudo yum makecache
sudo amazon-linux-extras | grep php (To confirm that "php8.0" is available.)
sudo yum-config-manager --disable 'remi-php*'
sudo amazon-linux-extras enable php8.0
sudo yum clean metadata
sudo yum install -y php-{pear,cgi,pdo,common,curl,mbstring,gd,mysqlnd,gettext,bcmath,json,xml,fpm,intl,zip}
php -v (Confirms that PHP 8.0 is installed.)
sudo yum install -y httpd mariadb-server
sudo systemctl start httpd
sudo systemctl enable httpd
sudo systemctl start mariadb
sudo mysql_secure_installation (Follow the prompts.)
sudo systemctl enable mariadb

PHP 8.0 is a major release of the PHP programming Language mostly used in the Development of Web Applications. It is the stable release and you can plan to migrate your applications but being aware that some could break especially where old features are deprecated.

In this article we will cover the installation of PHP 8 on Amazon Linux 2 server running in the Cloud or On-prem infrastructure.

Update Amazon Linux 2 system

Update the system before starting the installation of PHP 8 on Amazon Linux 2 system.

sudo yum -y update

Linux updates often comes with newer kernel patches and reboot is required for the system to run in this new release.

sudo systemctl reboot

Add EPEL and Remi Repositories

The packages and dependencies that will be required in the installation of PHP 8 on Amazon Linux 2 server are available in the EPEL and Remi repositories. The two RPM repositories need to be added manually by running the commands below in the terminal.

sudo yum -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
sudo yum -y install https://rpms.remirepo.net/enterprise/remi-release-7.rpm

Update Yum index to confirm the two repositories are working.

$ sudo yum makecache
Loaded plugins: langpacks, priorities, update-motd
amzn2-core                                                                                                                                 | 3.7 kB  00:00:00
amzn2extra-docker                                                                                                                          | 3.0 kB  00:00:00
epel/x86_64/metalink                                                                                                                       |  15 kB  00:00:00
remi-safe                                                                                                                                  | 3.0 kB  00:00:00
(1/5): epel/x86_64/filelists_db                                                                                                            |  12 MB  00:00:01
(2/5): epel/x86_64/prestodelta                                                                                                             |  547 B  00:00:00
(3/5): epel/x86_64/other_db                                                                                                                | 3.3 MB  00:00:00
(4/5): remi-safe/other_db                                                                                                                  | 491 kB  00:00:00
(5/5): remi-safe/filelists_db                                                                                                              | 1.4 MB  00:00:01
Metadata Cache Created

Install PHP 8.0 on Amazon Linux 2

Install the yum-utils package which provides yum-config-manager command line tool.

sudo yum -y install yum-utils

Method 1: Install PHP 8.0 from OS repositories

Let’s confirm that PHP 8.0 topic is available in our Amazon Linux 2 machine:

$ sudo  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 ]

We can confirm php8.0 topic is available. Let’s enable it.

sudo yum-config-manager --disable 'remi-php*'
sudo amazon-linux-extras enable php8.0 

After repo is enabled, install PHP 8.0 on Amazon Linux 2 with standard PHP extensions.

sudo yum clean metadata
sudo yum install php-{pear,cgi,pdo,common,curl,mbstring,gd,mysqlnd,gettext,bcmath,json,xml,fpm,intl,zip}

Confirm if dependency resolution is successful

Dependencies Resolved

=============================================================================================================================================================================================
 Package                                     Arch                                  Version                                            Repository                                        Size
=============================================================================================================================================================================================
Installing:
 php-bcmath                                  x86_64                                8.0.16-1.amzn2                                     amzn2extra-php8.0                                 65 k
 php-cli                                     x86_64                                8.0.16-1.amzn2                                     amzn2extra-php8.0                                5.0 M
 php-common                                  x86_64                                8.0.16-1.amzn2                                     amzn2extra-php8.0                                1.2 M
 php-fpm                                     x86_64                                8.0.16-1.amzn2                                     amzn2extra-php8.0                                1.7 M
 php-gd                                      x86_64                                8.0.16-1.amzn2                                     amzn2extra-php8.0                                168 k
 php-intl                                    x86_64                                8.0.16-1.amzn2                                     amzn2extra-php8.0                                179 k
 php-mbstring                                x86_64                                8.0.16-1.amzn2                                     amzn2extra-php8.0                                473 k
 php-mysqlnd                                 x86_64                                8.0.16-1.amzn2                                     amzn2extra-php8.0                                189 k
 php-pdo                                     x86_64                                8.0.16-1.amzn2                                     amzn2extra-php8.0                                122 k
 php-pear                                    noarch                                1:1.10.12-9.amzn2                                  amzn2-core                                       359 k
 php-xml                                     x86_64                                8.0.16-1.amzn2                                     amzn2extra-php8.0                                173 k
Installing for dependencies:
 libxslt                                     x86_64                                1.1.28-6.amzn2                                     amzn2-core                                       240 k
 libzip                                      x86_64                                1.3.2-1.amzn2.0.1                                  amzn2-core                                        62 k
 oniguruma                                   x86_64                                5.9.6-1.amzn2.0.4                                  amzn2-core                                       127 k
 php-process                                 x86_64                                8.0.16-1.amzn2                                     amzn2extra-php8.0                                 83 k

Transaction Summary
=============================================================================================================================================================================================
Install  11 Packages (+4 Dependent packages)

Total download size: 10 M
Installed size: 48 M
Is this ok [y/d/N]: y

Check PHP version

$ php --version
PHP 8.0.16 (cli) (built: Mar  1 2022 00:31:45) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.16, Copyright (c) Zend Technologies

Install Apache httpd web server to test PHP scripts execution:

sudo yum -y install httpd
sudo systemctl enable --now httpd

Create PHP info script inside default apache web root:

sudo tee /var/www/html/phpinfo.php<<EOF
<?php phpinfo(); ?>
EOF

Open your web browser and input http://serverip_or_hostname/phpinfo.php

Method 2: Install PHP 8.0 from REMI repository

Disable all Remi PHP repositories.

sudo yum-config-manager --disable 'remi-php*'
sudo amazon-linux-extras disable php8.0 

Enable PHP 8 Remi repository on Amazon Linux 2:

sudo yum-config-manager --enable remi-php80

Check current list of repositories enabled.

$ sudo yum repolist
Loaded plugins: langpacks, priorities, update-motd
245 packages excluded due to repository priority protections
repo id                                                   repo name                                                                                     status
amzn2-core/2/x86_64                                       Amazon Linux 2 core repository                                                                    22,852
amzn2extra-docker/2/x86_64                                Amazon Extras repo for docker                                                                         36
epel/x86_64                                               Extra Packages for Enterprise Linux 7 - x86_64                                                13,291+195
remi-php80                                                Remi's PHP 8.0 RPM repository for Enterprise Linux 7 - x86_64                                     214+47
remi-safe                                                 Safe Remi's RPM repository for Enterprise Linux 7 - x86_64                                       4,038+3
repolist: 40,431

Install PHP 8.0 on Amazon Linux 2 using the command:

sudo yum install php80

Initiate installation by accepting to start.

...
Dependencies Resolved

==================================================================================================================================================================
 Package                                      Arch                         Version                                         Repository                        Size
==================================================================================================================================================================
Installing:
 php80                                        x86_64                       1.0-3.el7.remi                                  remi-safe                        2.8 k
Installing for dependencies:
 audit-libs-python                            x86_64                       2.8.1-3.amzn2.1                                 amzn2-core                        79 k
 checkpolicy                                  x86_64                       2.5-6.amzn2                                     amzn2-core                       294 k
 environment-modules                          x86_64                       3.2.10-10.amzn2.0.2                             amzn2-core                       107 k
 libX11                                       x86_64                       1.6.7-3.amzn2                                   amzn2-core                       606 k
 libX11-common                                noarch                       1.6.7-3.amzn2                                   amzn2-core                       164 k
 libXau                                       x86_64                       1.0.8-2.1.amzn2.0.2                             amzn2-core                        29 k
 libcgroup                                    x86_64                       0.41-21.amzn2                                   amzn2-core                        66 k
 libselinux-python                            x86_64                       2.5-12.amzn2.0.2                                amzn2-core                       237 k
 libsemanage-python                           x86_64                       2.5-11.amzn2                                    amzn2-core                       115 k
 libxcb                                       x86_64                       1.12-1.amzn2.0.2                                amzn2-core                       216 k
 ncurses-compat-libs                          x86_64                       6.0-8.20170212.amzn2.1.3                        amzn2-core                       308 k
 php80-php-cli                                x86_64                       8.0.0-2.el7.remi                                remi-safe                        3.5 M
 php80-php-common                             x86_64                       8.0.0-2.el7.remi                                remi-safe                        704 k
 php80-runtime                                x86_64                       1.0-3.el7.remi                                  remi-safe                        1.1 M
 policycoreutils-python                       x86_64                       2.5-22.amzn2                                    amzn2-core                       454 k
 python-IPy                                   noarch                       0.75-6.amzn2.0.1                                amzn2-core                        32 k
 setools-libs                                 x86_64                       3.3.8-2.amzn2.0.2                               amzn2-core                       618 k
 tcl                                          x86_64                       1:8.5.13-8.amzn2.0.2                            amzn2-core                       1.9 M

Transaction Summary
==================================================================================================================================================================
Install  1 Package (+18 Dependent packages)

Total download size: 11 M
Installed size: 35 M
Is this ok [y/d/N]: y

Notice this installation method doesn’t affect the default installation of PHP you had on your Amazon Linux 2 server. To use PHP 8 the command is not php but php80

$ php80 --version
PHP 8.0.17 (cli) (built: Mar 15 2022 08:24:20) ( NTS gcc x86_64 )
Copyright (c) The PHP Group
Zend Engine v4.0.17, Copyright (c) Zend Technologies

Installing PHP extensions.

sudo yum install php80-<extension>

Example:

sudo yum install php80-php-fpm php80-php-mysqlnd php80-php-json php80-php-xml php80-php-tidy

Sample output:

...
Dependencies Resolved

==================================================================================================================================================================
 Package                                     Arch                            Version                                    Repository                           Size
==================================================================================================================================================================
Installing:
 php80-php-fpm                               x86_64                          8.0.0-2.el7.remi                           remi-safe                           1.8 M
 php80-php-mysqlnd                           x86_64                          8.0.0-2.el7.remi                           remi-safe                           193 k
 php80-php-pecl-zip                          x86_64                          1.19.2-1.el7.remi                          remi-safe                            53 k
 php80-php-tidy                              x86_64                          8.0.0-2.el7.remi                           remi-safe                            76 k
 php80-php-xml                               x86_64                          8.0.0-2.el7.remi                           remi-safe                           174 k
Installing for dependencies:
 libtidy                                     x86_64                          5.4.0-1.el7                                epel                                174 k
 libxslt                                     x86_64                          1.1.28-6.amzn2                             amzn2-core                          240 k
 libzip5                                     x86_64                          1.7.3-1.el7.remi                           remi-safe                            61 k
 php80-php-pdo                               x86_64                          8.0.0-2.el7.remi                           remi-safe                           127 k

Transaction Summary
==================================================================================================================================================================
Install  5 Packages (+4 Dependent packages)

Total download size: 2.9 M
Installed size: 8.6 M
Is this ok [y/d/N]: y

....
Total                                                                                                                             1.5 MB/s | 2.9 MB  00:00:01
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
Importing GPG key 0x352C64E5:
 Userid     : "Fedora EPEL (7) <[email protected]>"
 Fingerprint: 91e9 7d7c 4a5e 96f1 7f3e 888f 6a2f aea2 352c 64e5
 Package    : epel-release-7-13.noarch (installed)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
Is this ok [y/N]: y

You can also search for all packages that matches php80.

sudo yum search php80

Check available extensions.

$ php80 --modules

Refer to the official PHP 8.0 release page for more reading.

2 comments on “How To Install PHP 8.0 on Amazon Linux 2”


Leave a Reply

Your email address will not be published. Required fields are marked *