Upgrade PHP7.2 to PHP7.4 on Ubuntu running NGINX

Upgrading PHP versions isn’t exactly an upgrade in the sense that you will end up with both PHP versions installed. Installing PHP7.4 won’t remove 7.2, you will have both and then you can configure your server to use 7.4.

First, you’ll need to install PHP7.4 and all the related packages that you used in 7.2. To list all PHP packages used with 7.2 run:

dpkg -l | grep php7.2

Copy the names of all the packages. We’ll need to install the same list along with 7.4. To install the newer version of PHP, we first need to add a repo and then install 7.4.

sudo apt install software-properties-common sudo add-apt-repository ppa:ondrej/php sudo apt update

Now we can install PHP7.4 and required packages

sudo apt install -y php7.4 php7.4-cli php7.4-common php7.4-fpm # replace the package names in the command below with the list of names copied from PHP7.2 sudo apt install php7.4-bcmath php7.4-zip php7.4-exampleextension
Code language: Bash (bash)

Now you have PHP7.4 installed. If you run php -v, you should see the version listed as 7.4.

Configuring NGINX to use PHP 7.4

At this stage, it is likely that the sockets for both PHP 7.2 and 7.4 are active on your server. Run:

systemctl status php7.2-fpm.service systemctl status php7.4-fpm.service
Code language: CSS (css)

If both are shown as active (running), it is fine. We will come back to this after configuring NGINX.

Check the path at which NGINX is listening for the PHP socket. You can see this from your default.conf file in /etc/nginx/sites-enabled/ folder. The exact path and filename may differ on your server but check your server’s default NGINX config file. The socket will usually be shown as:

upstream php-handler-https { server 127.0.0.1:9000; }
Code language: Nginx (nginx)

We now want PHP 7.4 to listen on this port instead of 7.2. Go to folder with 7.2’s worker pool configuration and find the config file.

cd /etc/php/7.2/fpm/pool.d ls -la # You should see a file named www.conf or similar. Make a backup of the file. cp www.conf www.conf-bak # Check that you have a similar config file for 7.4 and make a backup ls -la /etc/php/7.4/fpm/pool.d cp /etc/php/7.4/fpm/pool.d/www.conf /etc/php/7.4/fpm/pool.d/www.conf-bak
Code language: Bash (bash)

Now we need to comment out the listening port in 7.2’s config and enable it in PHP 7.4. Edit the www.conf file in the PHP 7.2 folder and comment the following line:

;listen = 127.0.0.1:9000 # Add ; to comment
Code language: Nginx (nginx)

Go to PHP7.4 worker pool folder /etc/php/7.4/fpm/pool.d and change the listen line from listen = /run/php/php7.4-fpm.sock to:

listen = 127.0.0.1:9000

At this point, you’ve configured NGINX to use PHP7.4 but 7.2 is (probably) still running. You can stop that with:

systemctl stop php7.2-fpm.service
Code language: CSS (css)

Run the status commands again to confirm that only 7.4 is running.

systemctl status php7.2-fpm.service systemctl status php7.4-fpm.service
Code language: Bash (bash)

Only PHP 7.4 should be active (running) now. Restart PHP7.4 and NGINX with:

systemctl restart php7.4-fpm.service service nginx restart
Code language: Bash (bash)

Your server is now running PHP 7.4. To confirm create a info.php file in your server’s root with the following content:

<?php phpinfo() ?>
Code language: HTML, XML (xml)

Access the file in your browser with your-domain-name.com/info.php and you should see the PHP version listed as 7.4.

Be sure to delete the info.php file when you’re done as this exposes your server’s details and is a security issue.


Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *