We recently had to upgrade a webserver running php7.4 to php8.0. It’s a relatively easy task, but as with everything, it’s a lot easier if you know the right buttons to press. So below are the steps I took to complete the procedure.

Firstly, I needed to make a note of the PHP modules installed and the various important settings in php.ini.

To see your list of modules run:

> php -m

 

The output should look something like:

[PHP Modules]
bz2
calendar
Core
ctype
curl
date
dom
exif
etc...

 

To find which php.ini file you're using run:

> php --ini

 

Then edit the file to see what’s what. Things you might want to check are memory_limit, post_max_size and upload_max_filesize. They always seems to be set too low for me and cause user errors when uploading larger files for example.

Next you’ll need to remove the current version of PHP, in my case php7.4. If you don’t, you’ll get the following error when trying to install the new one:

“Refusing because php8.0 could cause an invalid combination.”

So to remove php, run the following:

> sudo yum remove -y php php-*...
Complete!

This will remove php and any php modules you have installed.

Now change the version of PHP enabled with Amazon Linux Extras:

> sudo amazon-linux-extras disable php7.4
> sudo amazon-linux-extras enable php8.0

 

Now, install the new version. For me, I needed the following install string, but the exact string depends on your PHP modules.

> sudo yum install php php-{cli,devel,pdo,process,pear,cgi,common,curl,mbstring,gd,mysqlnd,gettext,bcmath,json,xml,fpm,intl,zip}

 

So for you that might largely be it, but for me, I need the Imagick module for some more advanced image manipulation (QR code generation for 2FA). Unfortunately this module is a little more complicated to install, but as I already have imagick on the server, installing the module for PHP8 is pretty easy.

Just run:

> sudo pecl install imagick

 

You then need to just add the extension manually to your php.ini file

You should add “extension=imagick.so” to php.ini

Restart PHP-FPM and Apache:

> sudo systemctl restart php-fpm
> sudo systemctl restart httpd

Run the PHP version check to confirm:

> php -v
PHP 8.0.25 (cli) (built: Oct 31 2022 22:42:34) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.25, Copyright (c) Zend Technologies

Double check your installed modules:

> php -m
[PHP Modules]
bcmath
bz2
calendar
Core
ctype
curl
date
dom
exif
etc...

 

So hopefully that’s just about it, you’re on PHP8. Now we just need to wait until Amazon Linux Extras releases php8.1 and do it all over again.

Useful articles and resources:

https://greggborodaty.com/amazon-linux-2-installing-imagemagick-for-php-7-4/
https://techviewleo.com/install-php-8-on-amazon-linux/
https://arstech.net/install-php-7-on-amazon-linux-2/
https://stackoverflow.com/questions/72292343/amazon-linux2-upgrade-php-version-from-7-4-to-8-0-for-wordpress-application
https://dev.to/andreaolivato/install-php-8-on-aws-amazon-linux-2-2mdl