Skip to Content

How to upgrade PHP to 8.1 in Ubuntu

record ssh session

How to upgrade PHP to 8.1 in Ubuntu?


 Overview: This article provides the step to upgrade the PHP version.


Prerequisites: Any installed PHP version with apache and nginx web server.

Enter the following commands on the terminal to upgrade PHP version:

Step 1: To install the latest  PHP 8.1, you need to install PHP 8 binary packages that are available in the Ondrej Sury PPA repository.

root@gateway:~# add-apt-repository ppa:ondrej/php 
Press ENTER to add the repository.
 
Step 2: Install PHP 8.1

root@gateway:~#  apt-get update

root@gateway:~#  apt-get install php8.1

Step 3: Now install PHP 8.1 with all necessary modules with the command below:

root@gateway:~# apt install php8.1-cli php8.1-xml php8.1-gd php8.1-curl php8.1-mysql php8.1-ldap php8.1-zip
Step 4: Restart Apache2
root@gateway:~# systemctl restart apache2
Step 5: Disable the current version PHP modules whichever you are running and enable the modules for 8.1 version

root@gateway:~# a2dismod php* ( eg: a2dismod php7.4 )

root@gateway:~# a2enmod php8.1

Step 6:. Restart the Apache2 service.
root@gateway:~# systemctl restart apache2


To upgrade PHP to 8/8.1 with nginx web server

Step 1: Update and upgrade your system packages.

root@gateway:~#  apt-get update -y

root@gateway:~# apt-get upgrade -y

Step 2: Allow apt to use the system repository.

root@gateway:~# apt install ca-certificates apt-transport-https

root@gateway:~# apt install software-properties-common

Step 3: You need to install PHP 8 binary packages that are available in the Ondrej Sury PPA repository.

root@gateway:~# add-apt-repository ppa:ondrej/php

Press ENTER when prompted while adding the repository. 

Step 4: Install PHP 8/ 8.1 with nginx.

root@gateway:~# apt install php8.1 php8.1-fpm

root@gateway:~# systemctl status php8.1-fpm

Step 5: For Nginx to process PHP files, configure your Nginx server block by updating/adding the server section: 

root@gateway:~# nano /etc/nginx/sites-available/example

server {
listen 80;
server_name 192.168.0.105;
root /var/www/html;
index info.php;

location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
}
}

Step 6: Restart the Nginx service.

root@gateway:~# systemctl restart nginx

Step 7: Install the php8.1 needed packages.

root@gateway:~# apt install php8.1-cli php8.1-xml php8.1-gd php8.1-curl php8.1-mysql php8.1-ldap php8.1-zip

To check installed version of PHP and set the needed PHP version.

root@gateway:~# update-alternatives --config php


To confirm the PHP version:

root@gateway:~# php -v

root@gateway:~# nano /var/www/html/info.php

<?php

phpinfo();

?>


Related Articles:

Install and switch different PHP versions.

Correct PHP time in different o.s.