[PHP] Upgrading from PHP 7.4 to 8.1
Introduction I upgraded my project’s PHP version from 7.4 to 8.1. This article summarizes the steps I took and the considerations I encountered during the upgrade process. What This Article Covers Upgrading PHP from version 7.4 to 8.1. Environment Using EC2 OS Image: Amazon Linux 2 Kernel 5.10 PHP Version: PHP 7.4.33 (cli) PHPUnit Version: 9.5.28 CakePHP Version: 4.2.12 nginx: nginx/1.22.0 php-fpm: PHP 7.4.33 (fpm-fcgi) Preparation Gathering Upgrade Information PHP Each PHP version is supported for three years (2 years active support + 1 year security support). Since support for PHP 7.4 ended on November 28, 2022, I decided to upgrade to PHP 8.1 to ensure continued maintainability. PHP Supported Versions First, I reviewed the official documentation for breaking changes: Migrating from PHP 7.4.x to PHP 8.0.x Migrating from PHP 8.0.x to PHP 8.1.x PHPUnit As I was using PHPUnit for testing code, I also checked compatibility between PHP and PHPUnit. I checked the current PHPUnit version with the following command: $ ./vendor/bin/phpunit --version PHPUnit 9.5.28 by Sebastian Bergmann and contributors. According to the PHPUnit and PHP version compatibility chart, PHPUnit 9.x supports PHP 7.3 and above. Since I'm upgrading to PHP 8.1, our current version (9.5.28) remains compatible. CakePHP Next, I checked compatibility between CakePHP and PHP. I confirmed the current CakePHP version: $ cat ./vendor/cakephp/cakephp/VERSION.txt ... 4.2.12 Based on the CakePHP 4 System Requirements, CakePHP 4 supports PHP 7.4 and higher, including PHP 8.1. So there are no issues here either. Preparing a Test Environment Since the service was already deployed, I needed to verify if the upgrade would succeed and ensure functionality post-upgrade in a separate environment. I used another EC2 instance to test the upgrade to PHP 8.1, ran the test code, confirmed stable behavior, and then proceeded with the production upgrade. Steps to Upgrade to PHP 8.1 Back up the php.ini file (PHP configuration file). First, locate php.ini: $ find / -name "php.ini" -ls 8651969 64 -rw-r--r-- 1 root root 62221 Jan 21 03:18 /etc/php.ini Create a backup: $ cp -a /etc/php.ini /etc/php.ini.org If you have a custom version of php.ini, back that up as well. Check the current PHP version: $ php -v PHP 7.4.33 (cli) (built: Nov 19 2022 00:22:13) ( NTS ) List installed PHP packages to reinstall them later: $ yum list installed php* Installed Packages: php-cli.x86_64, php-common.x86_64, php-fpm.x86_64, php-intl.x86_64, php-json.x86_64, php-mbstring.x86_64, php-mysqlnd.x86_64, php-pdo.x86_64, php-xml.x86_64 Remove current PHP packages: $ yum remove "php*" Check available PHP versions via Amazon Linux Extras: $ amazon-linux-extras list available | grep php 42 *php7.4=latest enabled [ =stable ] _ php8.0 available [ =stable ] _ php8.1 available [ =stable ] Disable PHP 7.4: $ amazon-linux-extras disable php7.4 Install PHP 8.1: $ amazon-linux-extras install php8.1 Verify installed PHP packages: $ yum list installed php* Installed Packages: php-cli.x86_64, php-common.x86_64, php-fpm.x86_64, php-mysqlnd.x86_64, php-pdo.x86_64 Install any missing packages: $ yum install php-intl php-json php-mbstring php-xml Confirm PHP version: $ php -v PHP 8.1.14 (cli) (built: Jan 20 2023 18:11:30) (NTS) Restart nginx and php-fpm: $ systemctl restart nginx php-fpm The upgrade is complete! Now run the test code and verify behavior through the browser. Conclusion Through this upgrade, I learned the importance of checking compatibility with related packages and frameworks like PHPUnit and CakePHP. While I relied on reading documentation and running tests to identify incompatibilities, in the future, I’d like to try static analysis tools for migration checks (see: PHP8 Migration Compatibility Check). Feel free to share your thoughts or feedback! References PHP 8.1 Upgrade Steps: Amazon Linux 2 FAQs Upgrading PHP on Amazon Linux
![[PHP] Upgrading from PHP 7.4 to 8.1](https://media2.dev.to/dynamic/image/width%3D1000,height%3D500,fit%3Dcover,gravity%3Dauto,format%3Dauto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqmaaabplfbcjejg2rr5n.png)
Introduction
I upgraded my project’s PHP version from 7.4 to 8.1.
This article summarizes the steps I took and the considerations I encountered during the upgrade process.
What This Article Covers
Upgrading PHP from version 7.4 to 8.1.
Environment
- Using EC2
- OS Image: Amazon Linux 2 Kernel 5.10
- PHP Version: PHP 7.4.33 (cli)
- PHPUnit Version: 9.5.28
- CakePHP Version: 4.2.12
- nginx: nginx/1.22.0
- php-fpm: PHP 7.4.33 (fpm-fcgi)
Preparation
Gathering Upgrade Information
PHP
Each PHP version is supported for three years (2 years active support + 1 year security support).
Since support for PHP 7.4 ended on November 28, 2022, I decided to upgrade to PHP 8.1 to ensure continued maintainability.
First, I reviewed the official documentation for breaking changes:
PHPUnit
As I was using PHPUnit for testing code, I also checked compatibility between PHP and PHPUnit.
I checked the current PHPUnit version with the following command:
$ ./vendor/bin/phpunit --version
PHPUnit 9.5.28 by Sebastian Bergmann and contributors.
According to the PHPUnit and PHP version compatibility chart, PHPUnit 9.x supports PHP 7.3 and above.
Since I'm upgrading to PHP 8.1, our current version (9.5.28) remains compatible.
CakePHP
Next, I checked compatibility between CakePHP and PHP.
I confirmed the current CakePHP version:
$ cat ./vendor/cakephp/cakephp/VERSION.txt
...
4.2.12
Based on the CakePHP 4 System Requirements, CakePHP 4 supports PHP 7.4 and higher, including PHP 8.1. So there are no issues here either.
Preparing a Test Environment
Since the service was already deployed, I needed to verify if the upgrade would succeed and ensure functionality post-upgrade in a separate environment.
I used another EC2 instance to test the upgrade to PHP 8.1, ran the test code, confirmed stable behavior, and then proceeded with the production upgrade.
Steps to Upgrade to PHP 8.1
Back up the php.ini file (PHP configuration file).
First, locate php.ini:
$ find / -name "php.ini" -ls
8651969 64 -rw-r--r-- 1 root root 62221 Jan 21 03:18 /etc/php.ini
Create a backup:
$ cp -a /etc/php.ini /etc/php.ini.org
If you have a custom version of php.ini, back that up as well.
Check the current PHP version:
$ php -v
PHP 7.4.33 (cli) (built: Nov 19 2022 00:22:13) ( NTS )
List installed PHP packages to reinstall them later:
$ yum list installed php*
Installed Packages:
php-cli.x86_64, php-common.x86_64, php-fpm.x86_64, php-intl.x86_64, php-json.x86_64,
php-mbstring.x86_64, php-mysqlnd.x86_64, php-pdo.x86_64, php-xml.x86_64
Remove current PHP packages:
$ yum remove "php*"
Check available PHP versions via Amazon Linux Extras:
$ amazon-linux-extras list available | grep php
42 *php7.4=latest enabled [ =stable ]
_ php8.0 available [ =stable ]
_ php8.1 available [ =stable ]
Disable PHP 7.4:
$ amazon-linux-extras disable php7.4
Install PHP 8.1:
$ amazon-linux-extras install php8.1
Verify installed PHP packages:
$ yum list installed php*
Installed Packages:
php-cli.x86_64, php-common.x86_64, php-fpm.x86_64, php-mysqlnd.x86_64, php-pdo.x86_64
Install any missing packages:
$ yum install php-intl php-json php-mbstring php-xml
Confirm PHP version:
$ php -v
PHP 8.1.14 (cli) (built: Jan 20 2023 18:11:30) (NTS)
Restart nginx and php-fpm:
$ systemctl restart nginx php-fpm
The upgrade is complete!
Now run the test code and verify behavior through the browser.
Conclusion
Through this upgrade, I learned the importance of checking compatibility with related packages and frameworks like PHPUnit and CakePHP.
While I relied on reading documentation and running tests to identify incompatibilities, in the future, I’d like to try static analysis tools for migration checks (see: PHP8 Migration Compatibility Check).
Feel free to share your thoughts or feedback!
References
- PHP 8.1 Upgrade Steps: