Amazon Linux 2: Upgrading from PHP 7.2 to PHP 7.4

Wordpress PHP Update Recommended warning message

I finally got tired of my WordPress installation complaining about the version of PHP I was running. I decided it was time to upgrade.

My WordPress install operates on an Amazon EC2 instance running Amazon Linux 2. I used Amazon’s excellent tutorial, Install a LAMP web server on Amazon Linux 2, to set it up which uses their lamp-mariadb10.2-php7.2 package as the basis for the web server. After reviewing a few resources on installing or updating to PHP 7.4 on Amazon Linux 2, I put the following together that goes through the steps necessary to update if you used their tutorial to set up your server.

If all goes smoothly, it should take less than 15 minutes from start to finish.

Before proceeding, this article assumes you have some server knowledge, understand configuring a web server and the web server’s configuration options, have the privileges to update your server, and have a method to recover the original server state if things go sideways.


Step 1: Login to the Linux instance using a terminal shell such as PuTTy.


Step 2: Check for update to make sure the system is up to date as is.

$ sudo yum update

Step 3: Check your php version to confirm it is running PHP 7.2

$ php -v

If running PHP 7.2, you should see something similar to the following:

PHP 7.2 version summary

Step 4: Check to make sure that the amazon-linux-extras package is installed.

$ which amazon-linux-extras
amazon-linux-extras install response

If it isn’t installed, then you will need to install using the yum install command.


Step 5: Confirm the PHP7.x topic is available in Amazon Linux 2:

$ sudo amazon-linux-extras | grep php
php7.x topics with php7.2 enabled

It should show both the PHP 7.2 and lamp-mariadb10.2-php7.2 topics enabled, and php7.4 as available.


Step 6: Disable both the php7.2 and lamp-mariadb10.2-php7.2 topics

$ sudo amazon-linux-extras disable php7.2
$ sudo amazon-linux-extras disable lamp-mariadb10.2-php7.2

You may see a warning message, “Beware that disabling topics is not supported after they are installed.” This message can be safely ignored.


Step 7: Check the status of the topics – all should show as available.

$ sudo amazon-linux-extras | grep php
php7.x topics available on Amazon Linux 2

Step 8: Enable the php7.4 topic.

$ sudo amazon-linux-extras enable php7.4

Step 9: To be safe, check that the php7.4 topic is the only topic enabled before proceeding.

$ sudo amazon-linux-extras | grep php
php7.4 topic enabled

Step 10: If you already have ImageMagick installed on your server, you will need to remove it and reinstall later. Otherwise, there is a good chance that ImageMagick package dependencies will cause the upgrade process to fail.

$ sudo yum remove php-pecl-imagick

Here are the instructions I used to reinstall ImageMagick after the update to PHP 7.4 was complete.


Step 11: Update to PHP 7.4 by first cleaning up the metadata and then installing php along with the necessary php packages. Depending on your install and the applications you are running, you may need to install more (or less) packages. Installing these packages worked for my basic WordPress installation. Be sure to check the spelling of all the package names before hitting <ENTER> on the second command. (Note: while the code block below may make it look like the second command is entered on two lines, enter the second command as one line in the terminal shell. In other words, these are only two commands, not three.)

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

Step 12: Once the update finishes, check the php version. It should show PHP 7.4.

$ php -v
php 7.4.15

And we’re looking good at PHP 7.4.15!

Step 13: Restart the web service to be sure you’re using the latest PHP version

$ sudo systemctl restart httpd

IMPORTANT NOTE: if you are using the php-fpm module and the Apache event worker on your web server (mpm_event_module), there is an important change that requires you add the following lines to every virtual host entry that runs PHP scripts. If you don’t add these lines to your virtual host configuration files, your web server will become unresponsive and crash, at least that’s what happened in my case.

Step 14: [OPTIONAL if running php-fpm] Add the following lines at the virtual host of any sites that are running php, such as WordPress websites:

<FilesMatch ".php$">
    SetHandler "proxy:unix:/var/run/php-fpm/www.sock|fcgi://localhost/"
</FilesMatch>

Be certain to confirm that your *.sock file is contained in the path listing above. That’s where it was on my system, but there could be variations in the location and name of the file on your system, although it should generally be in the /var/run area of the server.


Step 15: Restart the web and php-fpm services, and you are all set!

$ sudo systemctl restart httpd
$ sudo systemctl restart php-fpm

After the upgrade, WordPress not only has stopped complaining about the PHP version but also is running faster. Both the front-end and admin sections feel more responsive.

Bottom line, it’s worth the 15 minute investment to make the update.


Disclaimer: These instructions applied to my specific development environment, which followed Amazon’s LAMP tutorial and used their lamp-mariadb10.2-php7.2 package to set up the web server. From the time I set up the web server to running these instructions, I made a few adjustments, such as running php-fpm. What I’m hinting at, in not so many words, is that these instructions may require adjustment based on the specifics of your setup. I am more than happy to entertain questions in the comments and will do my best to assist with any issues you may run into during the upgrade process, but I cannot guarantee timely and/or accurate responses.

Also, keep in mind that the instructions were created May 2021 and that certain steps may or may not be necessary as various pieces of the technology stack are updated by their respective owners.