Updating a Magento Project from PHP v7.0 to v7.3
- Check your Environment
- Install PHP v7.3
- Switch to your new PHP Version
- Checking your Code
- mcrypt
- Eval option for mb_ereg_replace() and mb_eregi_replace()
- Unquoted strings
- png2wbmp() and jpeg2wbmp()
- INTL_IDNA_VARIANT_2003 variant
- __autoload() method
- track_errors ini setting and $php_errormsg variable
- create_function() function
- mbstring.func_overload ini setting
- (unset) cast
- parse_str() without a second argument
- gmp_random() function
- each() function
- assert() with a string argument
- $errcontext argument of error handlers
- read_exif_data() function
- Re-Deploy Magento
Check your Environment
First check the version of PHP you have installed:
php -v
PHP 7.0.4 (cli) (built: Dec 18 2019 15:01:47) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.0.4, Copyright (c) 1999-2018, by Zend Technologies
Then check what modules you are using:
php -m
[PHP Modules]
bcmath
calendar
Core
ctype
curl
date
dom
exif
fileinfo
filter
ftp
gd
gettext
hash
iconv
intl
json
libxml
mbstring
mysqli
mysqlnd
openssl
pcntl
pcre
PDO
pdo_mysql
Phar
posix
readline
Reflection
session
shmop
SimpleXML
soap
sockets
sodium
SPL
standard
sysvmsg
sysvsem
sysvshm
tokenizer
wddx
xml
xmlreader
xmlwriter
xsl
Zend OPcache
zip
zlib
[Zend Modules]
Zend OPcache
Note them down, as you will have to install all of them for the new version of PHP that you are going to install.
Install PHP v7.3
Run below commands to upgrade the current packages to the latest version:
sudo apt update
sudo apt upgrade
Now setup PPA on Debian 10. Then import packages signing key. After that configure PPA for the PHP packages on your system:
sudo apt install ca-certificates apt-transport-https
wget -q https://packages.sury.org/php/apt.gpg -O- | sudo apt-key add -
echo "deb https://packages.sury.org/php/ buster main" | sudo tee /etc/apt/sources.list.d/php.list
Now run the following commands to install PHP 7.3:
sudo apt update
sudo apt install php7.3
Install the necessary PHP modules:
sudo apt install php7.3-cli php7.3-common php7.3-curl php7.3-gd php7.3-json php7.3-mbstring php7.3-mysql php7.3-xml php7.3-bcmath php7.3-gd php7.3-intl php7.3-opcache php7.3-soap
Now we need to configure PHP v7.3 - to find out which php.ini
file is the one that is loaded run php -i
:
php -i
phpinfo()
PHP Version => 7.3.26-1+0~20191218.33+debian10~1.gbpb5a34b
System => Linux Magento2 4.19.0-6-amd64 #1 SMP Debian 4.19.67-2+deb10u2 (2019-11-11) x86_64
Build Date => Dec 18 2019 15:01:47
Server API => Command Line Interface
Virtual Directory Support => disabled
Configuration File (php.ini) Path => /etc/php/7.3/cli
Loaded Configuration File => /etc/php/7.3/cli/php.ini
Scan this dir for additional .ini files => /etc/php/7.3/cli/conf.d
Additional .ini files parsed => /etc/php/7.3/cli/conf.d/10-mysqlnd.ini,
Note: on my system there was a second configuration file in
/etc/php/7.3/apache2/php.ini
. In a later step I am going to install NGINX that is going to add more files in/etc/php/7.3/fpm/php.ini
and/etc/php/7.3/cli/php.ini
. The Magento documentation recommend to do all changes to allphp.ini
files on your system.
We can see that the loaded configuration file is /etc/php/7.3/cli/php.ini
:
nano /etc/php/7.3/cli/php.ini
Set the Timezone
Edit the php.ini
configuration file and update the timezone value in date.timezone
setting tag:
date.timezone = "Europe/Berlin"
Increase PHP memory limit
Simply increase the default value to the recommended value:
- Compiling code or deploying static assets:
756M
- Installing and updating Magento components from Magento Marketplace:
2G
- Testing:
~3-4G
memory_limit = 2G
Enable opcache.save_comments
Enable opcache.save_comments and it is recommended to enable the PHP OpCache for performance reasons.
[opcache]
; Determines if Zend OPCache is enabled
opcache.save_comments=1
opcache.enable=1
Switch to your new PHP Version
To set PHP 7.3 as your active PHP version for CLI and Apache2 disable Apache2 modules for all other installed PHP versions and configure CLI using the update-alternatives command. Run the following commands to make changes.
- Apache:
sudo a2dismod php5.6 php7.1 php7.0 ## Shows error for modules not installed
sudo a2enmod php7.3
sudo service apache2 restart
- CLI:
sudo update-alternatives --set php /usr/bin/php7.3
sudo update-alternatives --set phar /usr/bin/phar7.3
sudo update-alternatives --set phar.phar /usr/bin/phar.phar7.3
sudo update-alternatives --set phpize /usr/bin/phpize7.3
sudo update-alternatives --set php-config /usr/bin/php-config7.3
Running a PHP version check again should show you that you are now using the updated version:
php -v
PHP 7.3.26-1+0~20191218.33+debian10~1.gbpb5a34b (cli) (built: Dec 18 2019 15:01:47) ( NTS )
Copyright (c) 1997.3018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.3.26-1+0~20191218.33+debian10~1.gbpb5a34b, Copyright (c) 1999-2018, by Zend Technologies
- NGINX:
Go to your NGINX server configuration, e.g.:
nano /etc/nginx/sites-available/default
Change the FastCGI backend to use the new PHP-FPM socket, save and exit the file:
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.3-fpm.sock;
}
Run the configuration test and restart the web server:
nginx -t
sudo service nginx restart
Checking your Code
mcrypt
The mcrypt extension has been deprecated in favour of OpenSSL, where it will be removed from the core and into PECL in PHP 7.3. Make sure that your code does not use something like:
$initVector = mcrypt_create_iv(mcrypt_get_iv_size(SAFETY_CIPHER, SAFETY_MODE), MCRYPT_RAND);
$encrypted = mcrypt_encrypt(SAFETY_CIPHER, $key, $password, SAFETY_MODE, $initVector);
echo base64_encode($initVector) . ENCRYPTION_DIVIDER_TOKEN . base64_encode($encrypted) . '<br/>';
echo mcrypt_decrypt(SAFETY_CIPHER, $key, $encrypted, SAFETY_MODE, $initVector) . '<br/>';
Eval option for mb_ereg_replace() and mb_eregi_replace()
The e pattern modifier has been deprecated for the mb_ereg_replace() and mb_eregi_replace() functions.
mb_eregi_replace ( string $pattern , string $replace , string $string [, string $option = "msri" ] ) : string
mb_ereg_replace ( string $pattern , string $replacement , string $string [, string $option = "msr" ] ) : string
Unquoted strings
Unquoted strings that are non-existent global constants are taken to be strings of themselves. This behaviour will now emit an E_WARNING:
<?php
var_dump(NONEXISTENT);
/* Output:
Warning: Use of undefined constant NONEXISTENT - assumed 'NONEXISTENT' (this will throw an Error in a future version of PHP) in %s on line %d
string(11) "NONEXISTENT"
*/
png2wbmp() and jpeg2wbmp()
The png2wbmp() and jpeg2wbmp() functions from the GD extension have now been deprecated and will be removed in the next major version of PHP.
png2wbmp ( string $pngname , string $wbmpname , int $dest_height , int $dest_width , int $threshold ) : bool
jpeg2wbmp ( string $jpegname , string $wbmpname , int $dest_height , int $dest_width , int $threshold ) : bool
INTL_IDNA_VARIANT_2003 variant
The Intl extension has deprecated the INTL_IDNA_VARIANT_2003 variant, which is currently being used as the default for idn_to_ascii() and idn_to_utf8(). PHP 7.4 will see these defaults changed to INTL_IDNA_VARIANT_UTS46, and the next major version of PHP will remove INTL_IDNA_VARIANT_2003 altogether.
idn_to_ascii ( string $domain [, int $options = IDNA_DEFAULT [, int $variant = INTL_IDNA_VARIANT_UTS46 [, array &$idna_info ]]] ) : string
idn_to_utf8 ( string $domain [, int $options = IDNA_DEFAULT [, int $variant = INTL_IDNA_VARIANT_UTS46 [, array &$idna_info ]]] ) : string
__autoload() method
The __autoload() method has been deprecated:
__autoload ( string $class ) : void
track_errors ini setting and $php_errormsg variable
When the track_errors ini setting is enabled, a $php_errormsg variable is created in the local scope when a non-fatal error occurs. Given that the preferred way of retrieving such error information is by using error_get_last(), this feature has been deprecated.
create_function() function
Given the security issues of this function, this dated function has now been deprecated. The preferred alternative is to use anonymous functions.
create_function ( string $args , string $code ) : string
mbstring.func_overload ini setting
Given the interoperability problems of string-based functions being used in environments with this setting enabled, it has now been deprecated.
(unset) cast
Casting any expression to this type will always result in NULL, and so this superfluous casting type has now been deprecated.
parse_str() without a second argument
Without the second argument to parse_str(), the query string parameters would populate the local symbol table. Given the security implications of this, using parse_str() without a second argument has now been deprecated. The function should always be used with two arguments, as the second argument causes the query string to be parsed into an array:
parse_str ( string $encoded_string [, array &$result ] ) : void
gmp_random() function
This function generates a random number based upon a range that is calculated by an unexposed, platform-specific limb size. Because of this, the function has now been deprecated. The preferred way of generating a random number using the GMP extension is by gmp_random_bits() and gmp_random_range():
gmp_random ([ int $limiter = 20 ] ) : GMP
gmp_random_range ( GMP $min , GMP $max ) : GMP
gmp_random_bits ( int $bits ) : GMP
each() function
This function is far slower at iteration than a normal foreach(), and causes implementation issues for some language changes. It has therefore been deprecated.
each ( array &$array ) : array
assert() with a string argument
Using assert() with a string argument required the string to be eval()'ed. Given the potential for remote code execution, using assert() with a string argument has now been deprecated in favour of using boolean expressions.
assert ( mixed $assertion [, Throwable $exception ] ) : bool
eval ( string $code ) : mixed
$errcontext argument of error handlers
The $errcontext argument contains all local variables of the error site. Given its rare usage, and the problems it causes with internal optimisations, it has now been deprecated.
read_exif_data() function
The read_exif_data() alias has been deprecated. The exif_read_data() function should be used instead.
Re-Deploy Magento
php7.3 bin/magento setup:upgrade
php7.3 bin/magento setup:di:compile
php7.3 bin/magento setup:static-content:deploy -f
chown -R www-data:www-data .
Purge Old PHP Version
apt purge php7.2 php7.2-common php7.2-cli