Update PHP version on Amazon EC2

It was time to update the PHP version on my WordPress server. WordPress gave me warnings; the site health plugin gave me a warning. Plugins gave me warnings. PHP, IT news sites, the internet, warnings everywhere.

I knew that my PHP version was very old. But still supported. At least until beginning of 2019. When I configured the server for the first time several years ago, the installed PHP version was already not the latest. It was what yum install php gave me. Updating software is crucial, so I decided to finally touch my running system.

WordPress provides a site explaining how to update your PHP version. The update process in the documentation goes like: write an email to your hoster. Or: Not working in my case. For those that want to know how to update PHP on a Amazon AMI EC2 instance, here are the stops and my lessons learned.

Preparations

First, do a backup. Update WordPress and the plugins. Check that the plugins are compatible with PHP 7.2

  • Backup: See my blog on how to create a snapshot of a EC2 instance.
  • Update WordPress and plugins: Easy: just do as always and keep it up-to-date.
  • Check plugins for compatibility: A plugin is available to check the installed plugins and files for compatibility with PHP 7.x. Install and activate it and run a test.

The PHP Compatibility plugin is started from the WP Admin site. Hint: in my case, the plugin worked fine, but also crashed the server. After running it and saving the results, uninstall it.

This gives as an output an evaluation of the plugins and their compatibility status.

Update

Next step is to update PHP. Use the package manager for this. I’ll split the installation process in two parts: PHP and the additional packages.

sudo yum update
sudo yum install php72
sudo yum install php72-opcache php72-mysqlfnd php72-gd php72-pecl-imagick php72-bcmath

Result installation PHP 7.2

Result Installation of additional PHP packages

Activate PHP

After installing PHP 7.2 it must be activated. The old PHP version is still the default one, meaning that calling php is not calling php 7.2. To change the paths, run alternative. It will show the available alternatives and asks which one you want to use. I am going to use php 7.2, so the input here was 2.

alternatives --config php

php -version

Now PHP 7.2 is installed and activated. After restarting Apache WordPress will run on a newer PHP version.

Let the world know

Matomo: Generating report data fails

Recently I saw that my Matomo reports were not showing the correct data. It seemed like the daily cron job wasn’t running or failing. To see what was causing this issue, I ran the archiving tool manually.

sudo /usr/bin/php /var/www/matomo/console core:archive --url=https://www.itsfullofstars.de/

Problem

The PHP script exists with an error.

Error: Got invalid response from API request: 
?module=API&method=API.get&idSite=1&period=year&date=
last7&format=php&trigger=archivephp. 
Response was 'PHP Fatal error: Allowed memory size of 
805306368 bytes exhausted (tried to allocate 131072 bytes) 
in /var/www/matomo/core/DataAccess/ArchiveWriter.php on 
line 142 PHP Fatal error: Allowed memory size of 
805306368 bytes exhausted (tried to allocate 32768 bytes) in
 /var/www/piwik/core/Http.php on line 248 '

The archive script is reaching its memory limit of 805306368 bytes. Using more is not allowed, therefore the error. 805306368 bytes == 786432 Kbyte == 768 Mbyte. Somewhere a configuration is limiting the memory usage of PHP to 768 MB.

Solution

There are many, many configuration files for PHP available in my system. Matamo is using its own configuration, located at:

/var/www/piwik/config/global.ini.php

The file contains a parameter for setting a memory limit for archiving task.

minimum_memory_limit_when_archiving = 768

768 is exactly the value reported in the error. Increasing this value to 1024 (1GB) should solve the problem.

sudo vim /var/www/piwik/config/global.ini.php
minimum_memory_limit_when_archiving = 1024

Result

Let the world know