How to Install PHP 8 on Ubuntu 20.04 with Apache and Nginx

install php 8 ubuntu 20.04

PHP also know as “PHP: Hypertext Preprocessor” is an open-source and most widely used scripting language used for developing dynamic and responsive web applications. It was created by Rasmus Lerdorf and used as a general-purpose programming language.

At the time of writing this tutorial, the latest version of PHP is PHP 8.0. It was officially released on November 26th, 2020. It contains a number of new features including, Union Types, Match Expressions, Named Arguments, Attributes, Weak Maps, and much more.

In this tutorial, we will explain how to install PHP 8 on Ubuntu 20.04 and integrate it with Apache and Nginx web server.

Prerequisites

  • A server running Ubuntu 20.04 with Apache/Nginx installed.
  • A root password is configured in your server.

Install PHP 8.0

By default, Ubuntu 20.04 ships with the PHP version 7.4. So you will need to add the Ondrej/php repository to your system. With Ondrej repository, you can install multiple PHP versions in your system.

You can install the Ondrej PHP repository by running the following command:

apt-get install ca-certificates apt-transport-https software-properties-common gnupg2 -y
add-apt-repository ppa:ondrej/php

Once the repository is enabled, install the PHP 8.0 by running the following command:

apt-get install php8.0 -y

Once the PHP 8.0 is installed, you can verify the installed version with the following command:

php -v

You should get the following output:

PHP 8.0.0 (cli) (built: Nov 27 2020 12:26:22) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.0-dev, Copyright (c) Zend Technologies
    with Zend OPcache v8.0.0, Copyright (c), by Zend Technologies

Install PHP 8.0 Extensions

You can also install some commonly used PHP extensions by running the following command:

apt-get install php8.0-common php8.0-curl php8.0-gd php8.0-mysql php8.0-xml php8.0-imagick php8.0-cli php8.0-mbstring php8.0-opcache php8.0-soap php8.0-zip php8.0-dev php8.0-imap -y

Install PHP 8.0 with Apache

In this section, we will show you how to integrate PHP 8.0 with Apache webserver.

Install PHP Module for Apache

If you are using Apache as a web server then you will need to install PHP module for Apache in your system. You can install it with the following command:

apt-get install libapache2-mod-php8.0 -y

Once the installation is finished, reload the Apache service to apply the configuration changes:

systemctl restart apache2

Install PHP-FPM for Apache

If you are using Php-FPM (FastCGI process manager) with Apache then you will also need to install some extra packages in your system. You can install all of them with the following command:

apt-get install php8.0-fpm libapache2-mod-fcgid -y

Once the installation is finished, enable the PHP-FPM with the following command:

a2enmod proxy_fcgi setenvif
a2enconf php8.0-fpm

Next, restart the Apache service to apply the changes:

systemctl restart apache2

Configure PHP 8.0 for Apache

Next, you will need to tweak the php.ini file for PHP 8.0 for better performance. To do so, edit the /etc/php/8.0/apache2/php.ini file:

nano /etc/php/8.0/apache2/php.ini

Tweak the following values:

upload_max_filesize = 32M 
post_max_size = 32M 
memory_limit = 256M 
max_execution_time = 500 
max_input_vars = 3000 
max_input_time = 1000
date.timezone = Asia/Kolkata

Save and close the file then restart the Apache service to apply the changes:

systemctl restart apache2

Upgrade to PHP 8.0 for Apache

If the older version of PHP is installed in your system then you will need to disable the old PHP version and enable the new PHP version 8.0.

To disable the old PHP version 7.4, run the following command:

a2dismod php7.4

Next, enable the new PHP version 8.0 using the following command:

a2enmod php8.0
update-alternatives --set php /usr/bin/php8.0

Now, restart the Apache webserver to apply the changes:

systemctl restart apache2

Install PHP 8.0 with Nginx

In this section, we will show you how to integrate PHP 8.0 with Nginx web server.

Install PHP-FPM for Nginx

PHP-FPM is the preferred method of processing PHP pages with NGINX. It provides some additional features useful for high traffic websites. Nginx does not have any support for processing PHP files. So you will need to install PHP-FPM to handle the PHP files.

You can install the PHP-FPM by running the following command:

apt-get install php8.0-fpm -y

After installing PHP-FPM, you can verify the status of the PHP-FPM service using the following command:

systemctl status php8.0-fpm

Upgrade to PHP 8.0 for Nginx

If the older version of PHP is installed in your system and Nginx is configured to use the older version of PHP-FPM then you will need to define your new PHP-FPM socket in your website virtual host configuration file to process the PHP files:

Edit your website’s Nginx configuration file with the following command:

nano /etc/nginx/sites-enabled/your-website

Add / Modify the following virtual blocks:

server {

    # . . . other configuration

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php8.0-fpm.sock;
    }
}

Save and close the file when you are finished then restart the Nginx service to apply the changes:

systemctl restart nginx

Configure PHP 8.0 for Nginx

Next, you will need to tweak the php.ini file for PHP 8.0 for better performance. To do so, edit the /etc/php/8.0/fpm/php.ini file:

nano /etc/php/8.0/fpm/php.ini

Tweak the following values:

upload_max_filesize = 32M 
post_max_size = 32M 
memory_limit = 256M 
max_execution_time = 500 
max_input_vars = 3000 
max_input_time = 1000
date.timezone = Asia/Kolkata

Save and close the file then restart the PHP-FPM service to apply the changes:

systemctl restart php8.0-fpm

In the above guide, you learned how to install PHP 8.0 on Ubuntu 20.04 with Apache and Nginx.

About Hitesh Jethva

I am Hitesh Jethva Founder and Author at LinuxBuz.com. I felt in love with Linux when i was started to learn Linux. I am a fan of open source technology and have more than 15+ years of experience in Linux and Open Source technologies.

View all posts by Hitesh Jethva