Update PHP version on Amazon EC2 to PHP 7.3
In November 2019 I wrote about how to update the PHP version on a Amazon AWS EC2 Linux instance to PHP 7.2. At that time, PHP 7.3 was already a year available. As PHP 7.2 is out of support since November 2020, the next available release on EC2 AMI Linux is 7.3. Version 7.4 is also available, but you must do a little bit more than just upgrading your Linux. To show that you can easily upgrade to PHP 7.3, I’ll show the steps to do so here first, and in a later blog show the upgrade to PHP 7.4.
First, check current PHP version.
Result: Version 7.2.34 is installed
As the installed version is outdated, an upgrade is necessary. The tool yum is used for this. Update the yum package repository information.
sudo yum update
Install PHP 7.3. Be aware that below command will only install base PHP 7.3. Additional packages needed, e.g. for WordPress, are not included. Also, while PHP 7.3 is going to be installed, it won’t be activated.
yum install php73
Check PHP version.
Result: Version 7.2.34 is installed.
As stated above, yum will install PHP 7.3, but not activate it. While it is available in the system, it is not in the path. To activate it, configure it as the preferred alternative.
sudo alternatives --config php
The command will show the installed PHP version and which one is the default, marked with *. To make PHP 7.3 the default one, enter the corresponding number. In my case: 3. Now run php again and check the reported version. It must be 7.3.x.
Result: Congratulations. EC2 Linux is now using PHP version 7.3.23.
Additional PHP 7.3 packages, can also be installed via yum. If you know that you need them, you can install them together with the previous command above.
sudo yum install php73-gd php73-opcache php73-pecl-imagick php73-bcmath
Install MySQL extension
In case you are running WordPress with MySQL as a backend, you do now have a problem. Restart HTTP and access WordPress. An error message will be shown saying that the MySQL extension is missing. To be able to connect to the database, additional components need to be installed. Here is a difference compared to my blog on upgrading to PHP 7.2. Be aware that the MySQL extension changed its name. You need to install packages php73-mysqlnd and php73-pdo.
sudo yum install php73-mysqlnd php73-pdo
Restart HTTPD and WordPress is now running PHP 7.3.
Next: upgrading to PHP 7.4.