How to Install PHP 8.1 on Amazon Linux 2 Instance

How to Install PHP 8.1 on Amazon Linux 2 Instance Step by Step

Dear Reader, I hope you are doing well. In this how-to guide, I am here to help you install PHP 8.1 on Amazon Linux 2 EC2 instance.

You can also check posts to install NginxApacheJavaGitAnsiblePHP 8.1Docker etc. on Amazon Linux 2.

Background

Amazon Linux 2: 

  • Amazon Linux 2 is a very popular free-tier eligible Linux AMI provided by AWS. You can use the yum package manager to install software on Linux 2.
  • Usually, the stable versions of the software are found on the Amazon Linux core repository and can be installed using the yum install package-name command.
  • Any newly released software gets added eventually to the amazon-Linux-extras repository.
  • At the time of writing this post, PHP 8.1 is available in amazon-Linux-extras repository and we will be installing it from there only on Amazon Linux 2.

PHP 8.1: 

PHP is a popular server-side scripting language. It is at the core of the world’s biggest blogging system i.e. WordPress. PHP 8.1 is the major update of PHP language and it provides many new features such as-

  • Enum support
  • First-class callable syntax
  • Read-only properties
  • Intersection types
  • Performance improvement etc.

Related: How to Install OpenJDK 11 on Amazon Linux 2

Don’t want to miss any posts from us? join us on our Facebook group, and follow us on Facebook, Twitter, LinkedIn, and Instagram. You can also subscribe to our newsletter below to not miss any updates from us.

Background

PHP 8.1 was released on November 25, 2021, however, it was not available on amazon-linux-extra repo until recently. I have seen users asking for this in various forums and GitHub issues.

Most of the tutorials that you’ll find on the internet are installing PHP 8.1 via the Remi repository. However, I always find it convenient and hassle-free to install packages from a native-supported repository.

And I am happy that PHP 8.1 is already present in amazon-linux-extra repository. So let’s go and install it on our Linux 2 system.

Prerequisite

  • An AWS Account
  • Permission to Create an Instance
  • Permission to Execute commands

Steps to Install PHP 8.1 on Amazon Linux 2 Instance

On a high level, below are the steps to install PHP 8.1 on an Amazon Linux 2 instance. If you already have got a running instance, you can connect to your instance and we can directly start from step 2.

  1. Create an Amazon Linux 2 EC2 Instance & Connect to It
  2. Install PHP 8.1 on the Amazon Linux 2
  3. Verify the PHP Installation

Step 1: Create an Amazon Linux 2 EC2 Instance & Connect to It

Create an Amazon Linux 2 EC2 instance. You can follow my previous tutorial on launching an EC2 instance to achieve this.

Once your instance is up and running, select your instance and click connect as shown below.

How to Install PHP 8.1 on Amazon Linux 2 Instance 1

Use the EC2 Instance Connect option and click Connect

You are connected to the EC2 instance now.

How to Install PHP 8.1 on Amazon Linux 2 Instance 2

That’s awesome. Let’s go to 2nd step to install PHP 8.1 on it.

2. How To Install PHP 8.1 on Amazon Linux 2

PHP 8.1 is present in amazon-linux-extras repository. To install a package from the extras repository, you need to have amazon-linux-extras installed in your system.

We will check that in a moment. Before installing any software on your instance, it’s always recommended to run a system update.

So let’s start with that step.

1. Run System Update

sudo yum update

2. Check If amazon-linux-extras is installed

Usually, Amazon Linux 2 system already comes installed with amazon-linux-extras and you can check it with the below command.

which amazon-linux-extras
amazon linux 2 extra

As you can see it is installed and you can see the location in the above screenshot.

However, by any chance, If it is not there, you can install it using the below command.

sudo yum install -y amazon-linux-extras

3. Check for Available PHP packages/topics in amazon-linux-extras repo

amazon-linux-extras list | grep php

After running g this command, it shows all the available versions of PHP in the repo as you can see below-

How to Install PHP 8.1 on Amazon Linux 2 Instance 3

Well, did you notice PHP 8.1 already above?

Great, this is what we need.

4. Enable the Topic

Our next step is to enable this topic and you can do this by below command.

sudo amazon-linux-extras enable php8.1

Once you run this command, you will see that php8.1 is enabled.

How to Install PHP 8.1 on Amazon Linux 2 Instance 4

5. Install PHP 8.1 from amazon-linux-extras repo

As you see in the above screenshot, CLI already provided you with the command to install PHP 8.1.

Let’s fire the command now.

sudo yum clean metadata && sudo yum install php-cli php-pdo php-fpm php-json php-mysqlnd

or type

sudo amazon-linux-extras install php8.1

Both the command work equally well and after firing any of the above, you will get below output-

Installed PHP 8.1

Enter y when prompted and your packages get installed on the system.

Note: You’ll have to use sudo(root privilege ) to run the above command. The reason is that whenever you try to install, remove or change any software, you must have root privilege to do such tasks.

Step 3: Verify the PHP Installation

php --version
How to Install PHP 8.1 on Amazon Linux 2 Instance 7

Congratulations !!!

You have successfully installed PHP 8.1 on your Linux 2 EC2 instance.

Conclusion

In this post, we learnt how to Install PHP 8.1 on Amazon Linux 2 EC2 Instance. We learnt a bit about the Amazon extras repository and how to install a package using that on Amazon Linux 2.

Then we connected our EC2 instance and installed PHP 8.1 using sudo amazon-linux-extras install php8.1 command.

Were you able to install PHP 8.1 on Amazon Linux 2 using this tutorial? Let me know in the comment section.

Enjoyed the content?

Subscribe to our newsletter below to get awesome AWS learning materials delivered straight to your inbox.

Don’t forget to motivate us-

Suggested Read:

Leave a Reply

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