Install or Upgrade PHP 8.1 on Ubuntu 22.04

Install or Upgrade PHP 8.1 on Ubuntu 22.04

Install or Upgrade PHP 8.1 on Ubuntu 22.04

PHP Oct 30, 2022

Install or Upgrade PHP 8.1 on Ubuntu 22.04

Getting Started
Run the following command to verify that your Ubuntu server has the most recent packages.

sudo apt update
sudo apt upgrade

This will refresh the package index and bring the installed packages up to date.

If you want to install PHP 8.1, you may skip this step. However, if you are preparing to install a lower version of PHP, such as 7.4, you must add this PPA.

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

Only include this if you're intending to install a PHP version other than 8.1.

Install PHP 8.1 for Apache
To install PHP 8.1, run the following command.

sudo apt install php8.1

After the installation is complete, use the following command to confirm the installation.

php -v

Install PHP 8.1 FPM for Nginx
FPM must be installed for Nginx. To install PHP 8.1 FPM, use the following command.

sudo apt install php8.1-fpm

After the installation is complete, use this command to ensure that PHP 8.1 FPM was properly installed.

php-fpm8.1 -v

Install PHP 8.1 Extensions
The following syntax is used to install PHP extensions.

sudo apt install php8.1-extension_name

Now, use the following command to install some widely used php-extensions.

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

Verify PHP Version
The PHP version may be simply verified with the command below.

php -v
PHP 8.1.5 (cli) (built: Apr 21 2022 10:32:13) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.5, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.5, Copyright (c), by Zend Technologies

Configure PHP 8.1
Now we setup PHP for Web Applications by making changes to the php.ini file.

The php.ini file will be located in the following directory for PHP 8.1 with Apache.

sudo nano /etc/php/8.1/apache2/php.ini

The php.ini file will be located in the following directory for PHP 8.1 FPM with Nginx.

sudo nano /etc/php/8.1/fpm/php.ini

To improve performance, use F6 to search within the editor and alter the following settings.

upload_max_filesize = 32M 
post_max_size = 48M 
memory_limit = 256M 
max_execution_time = 600 
max_input_vars = 3000 
max_input_time = 1000

After making changes to your PHP settings, you must restart Apache for the changes to take effect.
PHP-FPM must be restarted for Nginx users that utilise PHP-FPM.

sudo service php8.1-fpm restart

Restart PHP 8.1 FPM
After you've adjusted your PHP FPM settings, you must restart it to see the changes take effect.

sudo php-fpm8.1 -t 
sudo service php8.1-fpm restart

Now you are having PHP 8.1 Installed and configured.

Upgrade/Downgrade PHP in CLI
When you change the PHP version for your online apps, you must also change the PHP version in your CLI (Command Line Interface).

To update the PHP version on your CLI, use the command below. You will be requested to select your PHP version in interactive mode.

There are 3 choices for the alternative php (providing /usr/bin/php).

  Selection    Path             Priority   Status
* 0            /usr/bin/php8.1   81        auto mode
  1            /usr/bin/php7.4   74        manual mode
  2            /usr/bin/php8.0   80        manual mode
  3            /usr/bin/php8.1   81        manual mode

Press <enter> to keep the current choice[*], or type selection number:

Enter you choice and hit ENTER.

Upgrade/Downgrade PHP for Apache
If you wish to utilise a different PHP version, you may do so by upgrading or downgrading to the one mentioned below.

You must instruct Apache to utilise the correct PHP version that you have installed right now. Disable the old PHP module (I've used php 8.1 below, but you should use the php version used by Apache) and enable the new PHP module using the following command.

Replace the current PHP version that is enabled with your version.

sudo a2dismod php8.1
sudo a2enmod php7.4

To make the modifications take effect, restart Apache.

sudo service apache2 restart

Upgrade/Downgrade PHP for Nginx
You must update or downgrade the PHP-FPM socket in your Nginx configuration, which is found in the sites-available directory. This will be contained within the location block location ~ .php$

Change your configuration.

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

The line that has to be changed will look like this.

fastcgi_pass unix:/run/php/php8.1-fpm.sock; 

The old PHP version must be replaced with the new version.

fastcgi_pass unix:/run/php/php7.4-fpm.sock; 

Test your configration.

sudo nginx -t

To make the modifications take effect, save the file, close the editor, and restart Nginx.

sudo service nginx restart

You now know how to install PHP 8.1 for Apache and Nginx on your Ubuntu 22.04 server, as well as how to upgrade or downgrade to a different PHP version.


Anurag Deep

Logical by Mind, Creative by Heart