How to install php 7.4 on Amazon linux 2

Execute the command in this tutorial to install the PHP 7.4 version on Amazon Linux 2 for your web applications that don’t support the latest version of PHP.

Amazon Linux 2 is the current long-term supported and RPM-based Linux offered by AWS to run on its cloud. As we know, CentOS is not anymore an LTS server OS; therefore AML 2 version of AWS is an excellent stable option for installing and hosting applications.

Steps for installing PHP 7.4 on AWS Amazon Linux 2

1. Prerequisites

To perform this article, we need the following things:

  1. AWS EC2 Instance server must be running on Amazon Linux 2
  2. Users at least have sudo or root user access to install packages.
  3. Yum package manager

2. Update your AWS EC2 Amazon Linux 2 Server

Before we issue the commands to install PHP 7.2 on Amazon Linux 2, run the system update command to rebuild and sync the YUM package repository cache. Here is the command to do that.

sudo yum update

3. Check Available PHP Versions

In Amazon Linux 2, the extra modules and packages are available through the Amazon Linux Extras repository like EPEL. Therefore, let’s first check the available versions of PHP to install in our AM2 running on AWS EC2 instance via this extra package repository.

sudo amazon-linux-extras | grep PHP

Output– In our case, the available versions were:

[ec2-user@amazonlinux ~]$ 
 42  php7.4                   available    [ =stable ]
 51  php8.0                   available    [ =stable ]
 66  php8.1                   available    [ =stable ]

Note: if your system doesn’t have an amazon-linux-extras repository, then to install it, use: sudo yum install amazon-linux-extras

4. Enable PHP 7.4 Repo

As we know, PHP 7.4 is available through the extra repository of our Linux; hence, let’s enable it to install the required packages.

sudo amazon-linux-extras enable php7.4

After enabling the repository, clear the metadata:

sudo yum clean metadata
Enable PHP 7.4 repo in Amazon Linuix 2

4. Installing PHP 7.4 on Amazon Linux 2

We have confirmed that PHP-7.4 is available to install. Now, we can use AML 2’s default Yum package manager to install it.

sudo yum install php

For installing the standard php7.4 extension, use this command as well:

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

Press Y and hit Enter key to accept and start the installation of packages.

Install PHP and its extensions on RPM Linux

5. Check the version

After completing the above commands, the PHP will be on your Amazon Linux 2; let’s check its version to confirm its installation.

php -v
Check PHP version

6. How to disable and uninstall PHP 7.4 for 8.0 or 8.1

If you want to install PHP 8.0 or 8.1 on your existing Amazon Linux 2 server where the 7.4 version is already there, disable its repo first. For that, we can use the following:

sudo amazon-linux-extras disable php7.4

Now, to install PHP 8.0, enable its repo:

sudo amazon-linux-extras enable php8.0

In the same way for getting PHP 8.1, disable the other existing versions’ repo:

sudo amazon-linux-extras enable php7.4

sudo amazon-linux-extras enable php8.0

7. Uninstallation

To uninstall PHP from AWS Linux 2, we can use the YUM remove command; here is what to follow:

sudo yum remove php*

Other Articles:

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.