Install/Upgrade PHP 8.1 on Linux Mint 20 LTS

PHP 8.1 is a significant update of the PHP language that was “officially” released on November 25, 2021. As we advance from the existing PHP 8.0 release, this is a standard upgrade. The new PHP 8.1 brings enums, fibers, never return type, final class constants, intersection types, read-only properties amongst the long list of new features and changes.

Linux Mint is known to be a desktop distribution and not as a full-fledged web server. However, developers may require to install PHP on their Mint system for development purposes.

In the following tutorial, you will learn how to import the Ondřej Surý PPA and install PHP 8.1 on your Linux Mint 20.

Update Linux Mint System

Update your Linux Mint operating system to make sure all existing packages are up to date:

sudo apt update && sudo apt upgrade -y

Install Depedencies

To successfully install PHP 8.1, you may need to install the following dependencies.

sudo apt install software-properties-common -y

Import Ondřej Surý PHP PPA

To successfully install PHP 8.1, you will need to import the good renowned PPA from Ondřej Surý, the lead developer on PHP and Debian, and maintain Ubuntu and Debian packages.

First, install the prerequisites and Ondřej Surý PPA.

sudo add-apt-repository ppa:ondrej/php -y

Once done, it is a good idea to refresh your APT repositories as the PPA may bring additional upgrades to existing dependencies.

sudo apt update

Next, upgrade any packages that require it.

sudo apt upgrade -y

Install PHP 8.1 with Apache Option

If you run an Apache HTTP server, you can run PHP as an Apache module or PHP-FPM.

Install Apache Module

To install PHP 8 as an Apache module, enter the following command.

sudo apt install php8.1 libapache2-mod-php8.1

Example output:

Install PHP 8.1 on Linux Mint 20

Type Y, then press the ENTER KEY to complete the installation.

The module should be loaded automatically after the installation. However, if you need to load the module, use the following command.

sudo a2enmod php{version}

Example:

sudo a2enmod php8.1

Restart your Apache server once complete for changes to be active.

sudo systemctl restart apache2

Install Apache with PHP-FPM

PHP-FPM (an acronym of FastCGI Process Manager) is a hugely popular alternative PHP (Hypertext Processor) FastCGI implementation.

To install PHP-FPM with the following commands.

sudo apt install php8.1-fpm libapache2-mod-fcgid

Note, by default, PHP-FPM is not enabled for Apache. You must enable it by the following command.

sudo a2enmod proxy_fcgi setenvif && sudo a2enconf php8.1-fpm

Lastly, restart Apache.

sudo systemctl restart apache2

Verify that PHP-FPM is working:

systemctl status php8.1-fpm

Example output:

As a reminder to see what version of PHP 8.1 is installed on your system, use the following command.

php --version

Example output:

Install PHP 8.1 on Linux Mint 20

Install PHP 8.1 with Nginx Option

Nginx does not contain native PHP processing like other web servers like Apache. To handle the PHP files, you will need to install PHP-FPM “fastCGI process manager”.

First, check for updates on your system and install PHP-FPM, natively installing the PHP packages required.

In your terminal, use the following command to install PHP 8.1 and PHP 8.1-FPM.

sudo apt install php8.1 php8.1-fpm php8.1-cli -y

Once installed, the PHP-FPM service is automatically started, and you can check the status to make sure it’s running ok.

sudo systemctl status php8.1-fpm

Example output:

Install PHP 8.1 on Linux Mint 20

You will need to edit your Nginx server block and add the example below for Nginx to process the PHP files.

Below, example for all server blocks that process PHP files that need the location ~ .php$ added.

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

Test Nginx to make sure you have no errors with the adjustments made with the code above; enter the following.

sudo nginx -t

Example output:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Restart Nginx service for installation to be complete.

sudo systemctl restart nginx

As a reminder to see what version of PHP 8.1 is installed on your system, use the following command.

php --version

Example output:

Install PHP 8.1 on Linux Mint 20

Comments and Conclusion

In the tutorial, you have learned how to install PHP 8.1 for developers that request the latest version for web development and configure how to use it with Apache and Nginx. PHP 8.1 is exciting. However, at the current moment, it is still coming out of beta and not considered stable, such as 8.0 or the old stable 7.4, so beware you may find that many of your favorite software like WordPress or Plugins/Themes for CMS software may conflict until developers can update.

Do some research, prepare, and have PHP 7.4 or 8.0 installed and ready to replace if anything goes wrong when making the switch. The stable versions such as 8.0 are still actively developed, and packages are pushed simultaneously along with the 8.1 packages.



Follow LinuxCapable.com!

Like to get automatic updates? Follow us on one of our social media accounts!