Skip to Content

How to upgrade PHP to 8.1 in Ubuntu

 

record ssh session

 


How to upgrade PHP to 8.1 in Ubuntu?


     Prerequisites

      Any installed PHP version with apache and nginx web server.


Enter the following commands on the terminal to upgrade PHP:

 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.

2. Install PHP 8.1
root@gateway:~#  apt-get update

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


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 

4. Restart Apache2
root@gateway:~# systemctl restart apache2

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

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


To upgrade PHP to 8/8.1 with nginx web server

1. Update and upgrade your system packages.

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

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

2. Allow apt to use the system repository

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

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

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. 
4. Install PHP 8/ 8.1 with nginx

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

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

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;
}
}

6. Restart the Nginx service.

root@gateway:~# systemctl restart nginx

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@ubuntu:~# update-alternatives --config php


Testing PHP

root@gateway:~# php -v

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

<?php

phpinfo();

?>