Skip to main content
Table of contents

Update Ubuntu 20.04

To begin update the server using the command

sudo apt update && sudo apt upgrade -y

For more details about this process see How To Update All Packages Linux.

 

PHP version

Check the current PHP version using

php -v

The response I had was

PHP 7.4.3 (cli) (built: Nov  2 2022 09:53:44) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies

 

Install prerequisites

To action the installation of PHP 8.1, there are a few prerequisites that need to be installed.  Install the following requirements for the installation to proceed smoothly.

sudo apt install ca-certificates apt-transport-https software-properties-common

As an idea the response for me was:

Reading package lists... Done
Building dependency tree
Reading state information... Done
ca-certificates is already the newest version (20211016ubuntu0.20.04.1).
software-properties-common is already the newest version (0.99.9.8).
software-properties-common set to manually installed.
apt-transport-https is already the newest version (2.0.9).
The following packages were automatically installed and are no longer required:
  geoip-database libatomic1 libfwupdplugin1 libgeoip1 libllvm11 libxmlb1
  linux-azure-5.15-cloud-tools-5.15.0-1019 linux-azure-5.15-headers-5.15.0-1019
  linux-azure-5.15-tools-5.15.0-1019 linux-cloud-tools-5.15.0-1019-azure
  linux-headers-5.15.0-1019-azure linux-image-5.15.0-1019-azure
  linux-modules-5.15.0-1019-azure linux-tools-5.15.0-1019-azure
Use 'sudo apt autoremove' to remove them.
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

Already installed.

 

Install Ondrej PHP repository on Ubuntu 20.04

PHP 8.1 is not available on the main Ubuntu repositories.  Therefore we can install it, by adding the Ondrej PHP repository.  This action provides the latest version of PHP.

sudo add-apt-repository ppa:ondrej/php

The initial response will be

 Co-installable PHP versions: PHP 5.6, PHP 7.x and most requested extensions are included. Only Supported Versions of PHP (http://php.net/supported-versions.php) for Supported Ubuntu Releases (https://wiki.ubuntu.com/Releases) are provided. Don't ask for end-of-life PHP versions or Ubuntu release, they won't be provided.

Debian oldstable and stable packages are provided as well: https://deb.sury.org/#debian-dpa

You can get more information about the packages at https://deb.sury.org

IMPORTANT: The <foo>-backports is now required on older Ubuntu releases.

BUGS&FEATURES: This PPA now has a issue tracker:
https://deb.sury.org/#bug-reporting

CAVEATS:
1. If you are using php-gearman, you need to add ppa:ondrej/pkg-gearman
2. If you are using apache2, you are advised to add ppa:ondrej/apache2
3. If you are using nginx, you are advised to add ppa:ondrej/nginx-mainline
   or ppa:ondrej/nginx

PLEASE READ: If you like my work and want to give me a little motivation, please consider donating regularly: https://donate.sury.org/

WARNING: add-apt-repository is broken with non-UTF-8 locales, see
https://github.com/oerdnj/deb.sury.org/issues/56 for workaround:

# LC_ALL=C.UTF-8 add-apt-repository ppa:ondrej/php
 More info: https://launchpad.net/~ondrej/+archive/ubuntu/php
Press [ENTER] to continue or Ctrl-c to cancel adding it.

Press ENTER

Now you'll see a response like

Hit:1 http://azure.archive.ubuntu.com/ubuntu focal InRelease
Get:2 http://azure.archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]
Get:3 http://azure.archive.ubuntu.com/ubuntu focal-backports InRelease [108 kB]
Get:4 https://packages.microsoft.com/ubuntu/20.04/prod focal InRelease [10.5 kB]
Get:5 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]
Get:6 https://packages.microsoft.com/ubuntu/20.04/prod focal/main amd64 Packages [222 kB]
Hit:7 http://nginx.org/packages/ubuntu focal InRelease
Hit:8 http://ppa.launchpad.net/nginx/stable/ubuntu focal InRelease
Get:9 http://ppa.launchpad.net/ondrej/php/ubuntu focal InRelease [23.9 kB]
Get:10 http://ppa.launchpad.net/ondrej/php/ubuntu focal/main amd64 Packages [114 kB]
Get:11 http://ppa.launchpad.net/ondrej/php/ubuntu focal/main Translation-en [36.6 kB]
Fetched 744 kB in 4s (172 kB/s)
Reading package lists... Done

You can confirm that ondrej has been installed

sudo grep -rhE ^deb /etc/apt/sources.list* | grep -i ondrej

And see the following response

deb http://ppa.launchpad.net/ondrej/php/ubuntu focal main

 

Watch out

I ran a one last check by running the update command

sudo apt update && sudo apt upgrade -y

However, doing this action installed PHP8.2 apache.  I was seeing the following when I ran the php -v command

PHP 8.2.0 (cli) (built: Dec 10 2022 10:52:42) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.0, Copyright (c) Zend Technologies
    with Zend OPcache v8.2.0, Copyright (c), by Zend Technologies

For me I need to install PHP8.1 as the software I'm using isn't ready for 8.2.

 

How to uninstall PHP 8.x

To remove an unwanted version of PHP, run

sudo apt-get purge php8.*

As I already had ondrej installed (earlier step), if you don't or need to check run

sudo add-apt-repository ppa:ondrej/php

 

Install PHP 8.1

With the pre-work completed, now is time to install PHP8.1.  If you'd like to use PHP 8.1 with Nginx installation, the most recommended step is to install PHP-FPM to process PHP files. You can install PHP and PHP-FPM using the following command:

sudo apt install php8.1 php8.1-fpm

Performing a version check 'php -v'

PHP 8.1.13 (cli) (built: Nov 26 2022 14:07:36) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.13, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.13, Copyright (c), by Zend Technologies

Looking good.

The PHP-FPM service should start automatically. You can verify this by running:

sudo systemctl status php8.1-fpm

And the response will be similar to:

● php8.1-fpm.service - The PHP 8.1 FastCGI Process Manager
     Loaded: loaded (/lib/systemd/system/php8.1-fpm.service; enabled; vendor preset: e>
     Active: active (running) since Thu 2023-01-05 13:33:33 UTC; 28s ago
       Docs: man:php-fpm8.1(8)
    Process: 1638371 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /run/php>
   Main PID: 1638368 (php-fpm8.1)
     Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec"
      Tasks: 3 (limit: 9531)
     Memory: 8.9M
     CGroup: /system.slice/php8.1-fpm.service
             ├─1638368 php-fpm: master process (/etc/php/8.1/fpm/php-fpm.conf)
             ├─1638369 php-fpm: pool www
             └─1638370 php-fpm: pool www

Jan 05 13:33:32 {project}-UAT systemd[1]: Starting The PHP 8.1 FastCGI Process Manag>
Jan 05 13:33:33 {project}-UAT systemd[1]: Started The PHP 8.1 FastCGI Process Manage>

 

Configure NGINX for PHP-FPM

Now create an NGINX server block that will make use of the above FPM pool. Do this by editing your NGINX configuration file and passing the path of the pool’s socket file using the option fastcgi_pass inside the location block for PHP.  By default, the configuration file is named nginx.conf and usually placed in one of the following directories /usr/local/nginx/conf, /etc/nginx or /usr/local/etc/nginx.  In my situation the nginx.conf file was location in /etc/nginx directory.

Well not quite, actually the /etc/nginx/nginx.conf file didn't have the server {} reference.  Instead it pointed to virtual server files

  • default.conf
  • drupal.conf

The file that required to be altered in this instance was Drupal.conf

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

Test your configuration

sudo nginx -t

Finally, restart the Nginx web server for the changes to take effect.

sudo service nginx restart

 

Install PHP 8.1 Extensions

With PHP running, you can now extend the functionality using extensions.  These extensions are packages that can be installed by doing the following:

sudo apt install php8.1-[extension-name]

You can install some of the common PHP extensions by running

sudo apt install php8.1-common php8.1-mysql php8.1-xml php8.1-xmlrpc php8.1-curl php8.1-gd php8.1-imagick php8.1-cli php8.1-dev php8.1-imap php8.1-mbstring php8.1-opcache php8.1-soap php8.1-zip php8.1-redis php8.1-intl -y

or

sudo apt install -y -q php8.1-{common,mysql,xml,xmlrpc,curl,gd,imagick,cli,dev,imap,mbstring,opcache,soap,zip,redis,intl,bcmath}

When I compared the production server against the UAT server, the following extensions were missing from UAT

dom
gd
mastering
mysqli
mysqlnd
pdo_mysql
SimpleXML
xml
xmlreader
xmlwriter
xsl

Initially by running the XML extension

sudo apt install php8.1-xml

I had the response

Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
  geoip-database libatomic1 libfwupdplugin1 libgeoip1 libllvm11 libxmlb1
  linux-azure-5.15-cloud-tools-5.15.0-1019 linux-azure-5.15-headers-5.15.0-1019
  linux-azure-5.15-tools-5.15.0-1019 linux-cloud-tools-5.15.0-1019-azure
  linux-headers-5.15.0-1019-azure linux-image-5.15.0-1019-azure
  linux-modules-5.15.0-1019-azure linux-tools-5.15.0-1019-azure
Use 'sudo apt autoremove' to remove them.
The following NEW packages will be installed:
  php8.1-xml
0 upgraded, 1 newly installed, 0 to remove and 22 not upgraded.
Need to get 106 kB of archives.
After this operation, 480 kB of additional disk space will be used.
Get:1 http://ppa.launchpad.net/ondrej/php/ubuntu focal/main amd64 php8.1-xml amd64 8.1.14-1+ubuntu20.04.1+deb.sury.org+1 [106 kB]
Fetched 106 kB in 2s (48.1 kB/s)
Selecting previously unselected package php8.1-xml.
(Reading database ... 162199 files and directories currently installed.)
Preparing to unpack .../php8.1-xml_8.1.14-1+ubuntu20.04.1+deb.sury.org+1_amd64.deb ...
Unpacking php8.1-xml (8.1.14-1+ubuntu20.04.1+deb.sury.org+1) ...
Setting up php8.1-xml (8.1.14-1+ubuntu20.04.1+deb.sury.org+1) ...

Creating config file /etc/php/8.1/mods-available/dom.ini with new version

Creating config file /etc/php/8.1/mods-available/simplexml.ini with new version

Creating config file /etc/php/8.1/mods-available/xml.ini with new version

Creating config file /etc/php/8.1/mods-available/xmlreader.ini with new version

Creating config file /etc/php/8.1/mods-available/xmlwriter.ini with new version

Creating config file /etc/php/8.1/mods-available/xsl.ini with new version
Processing triggers for php8.1-fpm (8.1.14-1+ubuntu20.04.1+deb.sury.org+1) ...
Processing triggers for php8.1-cli (8.1.14-1+ubuntu20.04.1+deb.sury.org+1) ...

Therefore, adding com, SimpleXML, xml, xmlreader, xmlwriter and xsl

To add the MySQL extension

sudo apt install php8.1-mysql

Whereas, running the XML extension added mysqli, mysqlnd and pdo_mysql.  Leaving the only missing extensions as gd and mbstring.

Ok, you are probably getting the pattern.  So what about mbstring

sudo apt install php8.1-mbstring

Finally, the GD library

sudo apt install php8.1-gd 

Not quite.  When running the composer update, I had the following error

Composer is operating significantly slower than normal because you do not have the PHP curl extension enabled.
Loading composer repositories with package information
Info from https://repo.packagist.org: #StandWithUkraine
Updating dependencies
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - instaclick/php-webdriver[1.1, ..., 1.4.16] require ext-curl * -> it is missing from your system. Install or enable PHP's curl extension.

To add curl run

sudo apt install curl

or specific to php8.1

sudo apt install php8.1-curl

To check the version of Curl running

dpkg -l curl

Response

Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name           Version            Architecture Description
+++-==============-==================-============-=======================================================
ii  curl           7.68.0-1ubuntu2.15 amd64        command line tool for transferring data with URL syntax

 

Configure PHP 8.1 (Optional)

How do you configure PHP for web applications?  By changing relevant values in the php.ini file.  For PHP 8.1 FPM with Nginx, the php.ini location will be in the following directory.

/etc/php/8.1/fpm/php.ini

Review in your preferred editor and update the following values for better performance

upload_max_filesize = 50M
post_max_size = 50M
memory_limit = 2G
max_execution_time = 600
max_input_vars = 3000
max_input_time = 600

Once you have modified your PHP settings you need to restart your PHP-FPM for the changes to take effect. 

sudo service php8.1-fpm restart

 

 

Error - PHP 8.2 pop-up

Running a regular server update, the PHP updated to 8.2.  Whereas, I was wanting to keep it at PHP 8.1.

To check what versions of PHP you have running

sudo update-alternatives --list php

The response for me was

/usr/bin/php7.4
/usr/bin/php8.1
/usr/bin/php8.2

If you have multiple versions of PHP running, you can set the version by running

sudo update-alternatives --set php /usr/bin/php8.1

 

Related articles

Andrew Fletcher01 May 2024
Common commands used in Ubuntu - in progress
A growing list of commands I've used and what they do in no specific ordersudo snap install bw ps aux | grep java whoami ip addr show uptime sudo apt update &amp;&amp; sudo apt upgrade -y cat /etc/os-release sudo apt-get install needrestart sudo reboot sudo needrestart sudo ckan sysadmin...
Andrew Fletcher18 Mar 2024
Resolving CVE-2022-48624 less issue
To resolve the CVE-2022-48624 vulnerability on Ubuntu using Nginx, it's crucial to understand that the issue lies within the "less" package, not Nginx itself. The vulnerability affects "less" before version 606, where close_altfile in filename.c in less omits shell_quote calls for LESSCLOSE,...