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.
7 Comments
Renato · June 22, 2020 at 15:58
A little correction:
sudo yum install php72-mysqlnd (instead of php72-mysqlfnd)
Thank you
Rodrigo · August 12, 2020 at 05:41
Mine get stuck on mysql missing đ
kavita sawant · October 13, 2020 at 11:41
You saved my life..very nice commnads..
Darren Lambert · November 5, 2020 at 11:07
sudo yum install php72-mysqlnd – fixes the missing MySql error
Jordan · May 7, 2021 at 15:07
When I run the last command to choose the version of PHP it say permission denied for the var/lib/alternatives/php.new
Tobias Hofmann · May 7, 2021 at 15:42
Have you tried running the alternatives command using sudo?
aniket y patil · April 29, 2022 at 07:27
*********************** Change PHP version *****************************
step.1
sudo yum-config-manager –enable epel
yum install dnf -y
step.2
yum install epel-release yum-utils -y
step.3
yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
step.4
dnf install php74.x86_64
step.5
dnf clean metadata
step.6
dnf install php-cli php-pdo php-fpm php-json php-mysqlnd
step.7
dnf list installed php-cli php-pdo php-fpm php-json php-mysqlnd
step.8
which php
step.9
php -v
step.10
yum update
step.11
sudo systemctl restart httpd