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.

 
[email protected]:~# add-apt-repository ppa:ondrej/php
 
  Press ENTER to add the repository.

2. Install PHP 8.1
[email protected]:~#  apt-get update

[email protected]:~#  apt-get install php8.1


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

[email protected]:~# 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
[email protected]:~# systemctl restart apache2

5. Disable the current version PHP modules whichever you are running and enable the modules for 8.1 version
[email protected]:~# a2dismod php* ( eg: a2dismod php7.4 )
[email protected]:~# a2enmod php8.1

6. Restart the Apache2 service.
[email protected]:~# systemctl restart apache2


To upgrade PHP to 8/8.1 with nginx web server

1. Update and upgrade your system packages.

[email protected]:~#  apt-get update -y

[email protected]:~# apt-get upgrade -y

2. Allow apt to use the system repository

[email protected]:~# apt install ca-certificates apt-transport-https

[email protected]:~# apt install software-properties-common

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

[email protected]:~# add-apt-repository ppa:ondrej/php

Press ENTER when prompted while adding the repository. 
4. Install PHP 8/ 8.1 with nginx

[email protected]:~# apt install php8.1 php8.1-fpm

[email protected]:~# systemctl status php8.1-fpm

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

[email protected]:~# 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.

[email protected]:~# systemctl restart nginx

7. Install the php8.1 needed packages.

[email protected]:~# 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

[email protected]:~# update-alternatives --config php


Testing PHP

[email protected]:~# php -v

[email protected]:~# nano /var/www/html/info.php

<?php

phpinfo();

?>