So I’ve tried installing PHP on an Amazon Linux 2 EC2 instance today and what I found is that even though we are in 2021 Amazon Linux 2 still comes with PHP 5.4.x. Which is maybe understandable because quite a lot of people still use it, but I would expect the default version of PHP to be at least 7. Those needing old PHP versions are a shrinking category which could install it with some alternative method. But instead of them today we will need to use these alternative methods to install PHP 7.x or 8.x.
This post will guide your through this setup. It’s really not complicated, you just need to follow the instructions from this post and you’re good to go.
First of all to install them we need the amazon-linux-extras package. Let’s check if it is installed. We can do that with:
if we have it installed we get a result like:
If not then there’s no output. So we need to install this package:
sudo yum install -y amazon-linux-extras
Once installed we can go ahead and see which PHP versions can we install with this
sudo amazon-linux-extras | grep php
The output should be something like this
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 now choose a PHP version to our liking, so I will choose php8.0 and enable it the following way:
sudo amazon-linux-extras enable php8.0
Once you enabled the version you can actually install it
yum clean metadata sudo yum install -y php-cli php-pdo php-fpm php-mysqlnd php-mbstring php-gd php-bcmath php-xml
Once you run this command you should have the install process started and your desired version will be installed.
You can check the version by running the following command in the terminal:
The output will look like this
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
It is a very simple process as you could see. Enjoy using your PHP version and your server to build something great!