Google Cloud Linux

How to Install or Upgrade PHP 8.1 on Ubuntu 20.04

Disclosure: This post may contain affiliate links, which means we may receive a commission if you click a link and purchase something that we recommended.

Pinterest LinkedIn Tumblr

PHP 8.1 is the latest PHP version released on 2021. In this guide you are going to learn how to install the latest PHP version which is currently 8.1 on your Ubuntu 20.04 system or server and configure it with Apache and Nginx. You will also learn how to upgrade your PHP version to latest.

This tutorial guides you to configure PHP INI settings, FPM settings, Pools, etc which is more useful for your application to run smooth.

This installation is tested on Google Cloud Platform with a Compute Compute Engine VM Instance. This set up will work on all Linux servers.


Basic knowledge of using SSH Terminal on Linux.

Getting Started

Make sure your Ubuntu server is having the latest packages by running the following command.

sudo apt update
sudo apt upgrade

This will update the package index and update the installed packages to the latest version.

Add PPA for PHP 8.1

Add the ondrej/php which has PHP 8.1 package and other required PHP extensions.

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

Once you have added the PPA you can install PHP 8.1.

Install PHP 8.1 for Apache

Execute the following command to install PHP 8.1

sudo apt install php8.1

After the installation has completed, you can confirm the installation using the following command

php -v

Install PHP 8.1 FPM for Nginx

For Nginx you need to install FPM. Execute the following command to install PHP 8.1 FPM

sudo apt install php8.1-fpm

After the installation has completed, confirm that PHP 8.1 FPM has installed correctly with this command

php-fpm8.1 -v

Install PHP 8.1 Extensions

Installing PHP extensions are simple with the following syntax.

sudo apt install php8.1-extension_name

Now, install some commonly used php-extensions with the following command.

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

Configure PHP 8.1

Now we configure PHP for Web Applications by changing some values in php.ini file.

For PHP 8.1 with Apache the php.ini location will be in following directory.

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

For PHP 8.1 FPM with Nginx the php.ini location will be in following directory.

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

Hit F6 for search inside the editor and update the following values for better performance.

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

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

For users with Nginx to who use PHP-FPM, you need to restart PHP-FPM.

sudo service php8.1-fpm restart

Configure PHP 8.1 FPM Pools

PHP-FPM allows you to configure the user and group that the service will run under. You can modify these with these commands

sudo nano /etc/php/8.1/fpm/pool.d/www.conf

If you want to change the username name you can change the following lines by replacing the www-data with your username.

user = username 
group = username 
listen.owner = username = username

Hit CTRL+X and Y to save the configuration and check if the configuration is correct and restart PHP.

Restart PHP 8.1 FPM

Once you have updated your PHP FPM settings you need to restart it to apply the changes.

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

Now you are having PHP 8.1 Installed and configured.

Prepare yourself for a role working as an Information Technology Professional with Linux operating system

Upgrade to PHP 8.1 for Apache

Once you have installed PHP 8.1 you need to upgrade to the latest installed version of PHP.

You need to tell Apache to use the PHP 8.1version we installed right now. Disable the old PHP module (below I have mentioned php7.4, you need to use your current php version used by Apache) and enable the new PHP module using the following command.

Replace the current enabled PHP version with your version.

sudo a2dismod php7.4
sudo a2enmod php8.1

Restart Apache for the changes to take effect.

sudo service apache2 restart

Upgrade PHP 8.1 for Nginx

For Nginx you need to update the PHP-FPM socket in your Nginx configration located inside the sites-available directory. This will be located inside the location block location ~ \.php$

Edit your configuration…

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

The line you need to modify will look like this…

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

You need to replace the old PHP version with the new version.

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

Test your configration.

sudo nginx -t

Save the file and exit the editor and restart Nginx for the changes to take effect.

sudo service nginx restart


Now you have learned how to install PHP 8.1 on your Ubuntu server for Apache and Nginx and also how to upgrade to latest version.

Thanks for your time. If you face any problem or any feedback, please leave a comment below.


  1. Justin Rodino Reply

    You probably took the text from a previous article as above it says:

    For PHP 7.4 FPM with Nginx the php.ini location will be in following directory.

    When you really mean for PHP 8.1 with FPM and nginx….

  2. Noel Kaiye Reply

    I have followed the above post upgrading my PHP from 8.0 to 8.1 in Ubuntu 18.04 with Apache/2.4.52. The apache web server seems to work fine after the upgrade, HOWEVER. My php files do not work especially those that I place them in the directory /var/www/html/mysite folder. It seems PhpMyAdmin works which I understand runs on php. Why do files in the mentioned directory give me http 503 error “Service Unavailable
    The server is temporarily unable to service your request due to maintenance downtime or capacity problems. Please try again later.

    Apache/2.4.52 (Ubuntu) Server at localhost Port 80”

    Please I will highly appreciate your help in resolving this issue. THANK YOU!

  3. Thanks for the article. BTW – You’re missing the word ‘service’ after sudo in `sudo php8.1-fpm restart`

  4. I’m working on Ubuntu server 20.10, and after updating PHP version command “php -v” returns that My current version of PHP is 8.1.2, yet phpinfo(); in my script returns that my PHP version is 8.0.15. So I did update PHP version, but old version is still in active use.

    “sudo a2dismod php8.0” gives “Module php8.0 already disabled”, while “sudo a2enmod php8.1” returns:
    Considering dependency mpm_prefork for php8.1:
    Considering conflict mpm_event for mpm_prefork:
    Considering conflict mpm_worker for mpm_prefork:
    Module mpm_prefork already enabled
    Considering conflict php5 for php8.1:
    Module php8.1 already enabled

    What should I do in order to avtivate correct PHP version? I did “sudo service apache2 restart”, I even rebooted whole server – nothing helps.

    • Hi, to changing the command line PHP version, you need to use the below command.

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

      • Command returned nothing, which usually is a good thing, but even after restarting apache, phpinfo(); still returns version 8.0.15 🙁

  5. This installs php 8.2 btw…. because of dependency recommendations…

Write A Comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.