Last updated on November 18th, 2022 at 10:02 am

In this post, I will explain to you how we can upgrade the PHP version to the latest version. With the help of this post, you are able to update to PHP version on the ubuntu server.

So let get started, I am using the Digital Ocean server to test this you can create an account on the digital ocean and create a new droplet. If you have Cpanel installed then this is a very easy task you can go to setting and select a specific PHP version but If you are running a server with a CLI interface then we have to manually install the PHP version and tell the server to use a specific version.

Upgrade to latest PHP 7.4 version

Here are the complete steps to upgrade to PHP’s latest version (7.4) on Apache and in the NGINX server.

Add PPA to install PHP 7.4

Add the ondrej/php which has the latest PHP 7.4 package and other required PHP extensions.

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

Once the installation is done you ready to install PHP latest version.

Upgrade PHP version on NGINX server

Step 1. Login to the server using a terminal.

Step 2. To upgrade in NGINX we need to install FPM, execute the below command to install PHP FPM 7.4

sudo apt install php7.4-fpm

Step 3. Once the installation is complete you can check your PHP version using php -v.

How to Install PHP 7.4 extension

Extension installation in PHP is very easy you can run below command to install PHP extension in your server.

sudo apt install php7.4-selected_extension

For example, if you want to install PHP MySql or XML extension you and run bellow command to install.

sudo apt install php7.4-mysql php7.4-xml

Configure NGINX to use PHP 7.4

For NGINX you need to update your site configuration file you need to update your PHP-FPM socket in the configuration file located inside /etc/nginx/sites-available the directory. You can open this file using this command

sudo nano /etc/nginx/sites-available/my.conf

Now you need to search for location fastcgi_pass and update php7.0-fpm.sock to php7.4-fpm.sock as shown in the below code snippet.

location ~ \.php$ {
  include snippets/fastcgi-php.conf;
  # With php7.3-fpm:
  fastcgi_pass unix:/run/php/php7.4-fpm.sock;

Once you update this save your file using ctrl+o and check your Nginx config file using

nginx -t

If everything okay you can restart your Nginx configuration using below command.

sudo service nginx restart

Upgrade PHP version in Apache

Run below command to install PHP 7.4 latest version in the Apache server.

sudo apt install php7.4

Now we need to tell Apache to use the latest installed version of PHP 7.4 by disabling the old PHP module (below I have mentioned php7.0, you need to use your current PHP version used by Apache) and enabling the new PHP module using the following command.

sudo a2dismod php7.0
sudo a2enmod php7.4

Once it has done we need to restart our apache server

sudo service apache2 restart

Now you can check your PHP version using php -v.

Hope you like this post, don’t forget to share with other.