loading...

Upgrading to PHP 7.4

anandpushkar088 profile image Pushkar Anand Updated on ・3 min read

The awaited latest version of the PHP 7.4 was released on 28th Nov, 2019. It comes with a lot of new features such as

  • Typed Properties
  • Arrow Functions
  • Limited Return Type Covariance and Argument Type Contravariance
  • Unpacking Inside Arrays
  • Numeric Literal Separator
  • Weak References
  • Allow Exceptions from __toString()
  • Opcache Preloading
  • Several Deprecations
  • Extensions Removed from the Core

PHP 7.1 approaches EOL & 7.2 will become security only on 1st December.

PHP EOL

Let's see how to upgrade your PHP installation to 7.4.

1. Add the ondrej/php PPA

Ubuntu:

sudo add-apt-repository ppa:ondrej/php # Press enter to confirm.
sudo apt-get update

Debian:

sudo apt install apt-transport-https lsb-release
sudo wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg # Download the signing key
sudo sh -c 'echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list' # Add Ondrej's repo to sources list.
sudo apt update

2. Install PHP 7.4 and required extensions

To install PHP 7.4 core

sudo apt install php7.4 php7.4-common php7.4-cli

Install required extensions

Simply prefix php7.4- with an extension that you need to install.
For example to get the php-curl extension use php7.4-curl

sudo apt install php7.4-curl php7.4-json php7.4-gd php7.4-mbstring php7.4-intl php7.4-bcmath php7.4-bz2 php7.4-readline php7.4-zip

3. PHP 7.4 for web:

To integrate PHP with your web server, If you are using Nginx, or Apache with mod_event, you will need to install php7.4-fpm package. If you are using PHP as an embedded Apache module, you will need the package libapache2-mod-php7.4. For Apache, you can use apachectl -V to see your current MPM, whether it’s prefork or event.

Nginx or Apache with event MPM:

sudo apt install php7.4-fpm

Apache with prefork MPM:

sudo apt install libapache2-mod-php7.4
sudo a2enmod php7.4

4. Uninstall old PHP versions:

sudo apt purge php7.3 libapache2-mod-php7.3 # For removing PHP 7.3
sudo apt purge php7.2 libapache2-mod-php7.2 # For removing PHP 7.2
sudo apt purge php7.1 libapache2-mod-php7.1 # For removing PHP 7.1
sudo apt purge php7.0 libapache2-mod-php7.0 # For removing PHP 7.0

5. Verify PHP 7.4 installation:

To verify the installation of PHP 7.4

From CLI:

php -v

php -v output

From web server:

Create a file info.php. It must be saved to a very specific directory, which is called the “web root”. In Ubuntu and Debian this is located at /var/www/html/.

sudo nano /var/www/html/info.php

Type the following in the editor.

<?php
    phpinfo();
?>

When you are finished, save and close the file.

Now visit this script in your browser. http://your_server_IP_address/info.php
You should see something similar to this.

phpinfo() output

So now you have PHP 7.4 up and running. See the migration guide for PHP 7.4 at https://www.php.net/migration74

Discussion

pic
Editor guide
Collapse
devvyd profile image
Dāvis Naglis

Good job.
Switched to PHP 7.4 yesterday. Ended up having a "MySQL server gone away" error when working with Laravel. Weirdly enough, the fix was to change MySQL authentication method to caching_sha2_password.

Collapse
anandpushkar088 profile image
Pushkar Anand Author

MySQL 8.X uses caching_sha2_password as the default authentication mechanism.

Collapse
devvyd profile image
Dāvis Naglis

I still had it on auth_socket as authentication mechanism (after I reinstalled MySQL).

Collapse
funnelxpert profile image
Peter Nyiri

Hi,
I am not an expert and having trouble updating to PHP 7.4 from 7.2.
I have a VPS, Apache-NGINX-Varnish, CentOS 7.7 and CWP Pro.
I did the steps to install:

sudo yum -y install dl.fedoraproject.org/pub/epel/epel...

sudo yum -y install rpms.remirepo.net/enterprise/remi-...

yum --enablerepo=remi-php74 install php

yum --enablerepo=remi-php74 install php-xml php-soap php-xmlrpc php-mbstring php-json php-gd php-mcrypt

sudo yum-config-manager --enable remi-php74

At this point php 7.4 seems to be installed, however php -v still returns 7.2 and CWP also shows the old version.

What else do I need to do to with Apache/NGINX?

Server version: Apache/2.4.6 (CentOS)
Server MPM: prefork

I tried this:

sudo apt install libapache2-mod-php7.4

sudo: apt: command not found

Thanks.

Collapse
anandpushkar088 profile image
Pushkar Anand Author

Try sudo dnf -y install dnf-utils and sudo dnf module install php:remi-7.4

Collapse
funnelxpert profile image
Peter Nyiri

I use yum, not dnf.

I already ran this:
sudo yum -y install yum-utils

Package yum-utils-1.1.31-52.el7.noarch already installed and latest version
Nothing to do

Also I just remembered that my server is running on php-cgi, not php-fpm

Thread Thread
anandpushkar088 profile image
Pushkar Anand Author

I have no experience with CGI.

Collapse
webmarielle profile image
Marielle

Hello, Petter. I have the same trouble with updating PHP on CWP. Can you possibly write me if you finded the solution?

Collapse
hungnguyenkt profile image
Hung Nguyen Manh

hahahaha it's like TypeScript systax

Collapse
dmitridon profile image
Dmitri Don

php7.4-fpmpackage -> php7.4-fpm package

also, would be cool if you would publish how all these new features would change our code!

Collapse
anandpushkar088 profile image
Pushkar Anand Author

Thanks for finding the typo. I plan on writing about it shortly.

Collapse
codestuff2 profile image
Adam Whitlock

Php is looking a bit more like typescript with all the typing and arrow functions! Thanks for writing

Collapse
jeremycmorgan profile image
Jeremy Morgan

Very cool stuff. I haven't coded anything real in PHP in about a decade but I'm thinking about putting something together just for fun. Thanks for the article!

Collapse
anandpushkar088 profile image
Pushkar Anand Author

Do post about what you build.

Collapse
kstka profile image
Konstantin

Thank you for the article. There is a tiny mistake here (php7.3):

sudo apt install php7.4-curl php7.4-json php7.4-gd php7.4-mbstring php7.4-intl php7.4-bcmath php7.4-bz2 php7.3-readline php7.3-zip

Collapse
anandpushkar088 profile image
Pushkar Anand Author

Thanks for letting me know. I'll update.

Collapse
madhavassociate profile image
Injection Moulding

Hi pushkar, can you tell me how can I install ziparchive on php 7.4? i am running virtuamin on centos 7.

Collapse
anandpushkar088 profile image
Pushkar Anand Author

I am not that familiar with virtuamin, but I think it should be same as installing any other packages. Try installing as you usually do. The ziparchive is available in php-zip.

Collapse
natanfelles profile image
Natan Felles

How do you installed xdebug?

Collapse
anandpushkar088 profile image
Pushkar Anand Author

I used the pear package to install Xdebug.