Install PHP 8.0 on Ubuntu 20.04 | 18.04

This brief tutorial shows students and new users how to install PHP 8.0 on Ubuntu 20.04 | 16.04.

PHP 8.0 is a new major version with boat loads of improvements and features. It will officially be released on November 26, 2020.

PHP is a popular scripting language suited for web development. Many of the popular content management systems like WordPress, Joomla and Drupal are PHP based platform.

If you’re going to be developing PHP based applications, you’ll need it installed on the systems you’re developing on.

For students and new users, this post will come in handy if they want to learn how to install it on Ubuntu, with either Apache or Nginx HTTP server.

PHP is usually combined with a web server and/or database server to be usable. Many times, you’ll see PHP being used with Apache or Nginx web servers.

Add PPA Repository to install PHP 8.0

 Since some versions of Ubuntu don’t have the latest version of PHP, including PHP 8.0, you can add a third-party PPA repository to install the latest from there.

The command below will add a third-party PPA to Ubuntu.

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

Then update and upgrade to the latest version of PHP of your choice.

sudo apt update

After installing the PPA above, you can now go and install PHP 8.0.

Using PHP 8.0 with Apache

Below is how one uses PHP with Apache web server on Ubuntu.

Install the commands below to use PHP with Apache web server.

sudo apt update
sudo apt install php8.0 libapache2-mod-php8.0

The commands above will install the latest stable version of PHP along with Apache on Ubuntu. After installing, you can restart Apache using the commands below:

sudo systemctl restart apache2

This is how to install PHP 8.0 on Ubuntu with Apache support.

To review PHP version, run the commands below:

php -v

It should display similar lines as below:

HP 8.0.0RC3 (cli) (built: Oct 31 2020 17:06:41) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.0-dev, Copyright (c) Zend Technologies
with Zend OPcache v8.0.0RC3, Copyright (c), by Zend Technologies

Using PHP 8.0 with Nginx

For most people, PHP with Nginx is a better suite then Apache. For those, they can run the commands below to use PHP with Nginx.

Unlike Apache2, Nginx doesn’t have built-in support for processing PHP files, so they both need to be installed and configured separately.

To install Nginx with PHP, run the commands below:

sudo apt update
sudo apt install nginx php8.0-fpm

The commands above will install the latest stable version of PHP-FPM along with Nginx web server on Ubuntu.

After installing the packages above, restart Nginx and PHP-FPM.

sudo systemctl restart nginx
sudo systemctl restart php8.0-fpm

To validate PHP status, run the commands below:

sudo systemctl status php8.0-fpm

You should get something similar to the lines below:

 ● php8.0-fpm.service - The PHP 8.0 FastCGI Process Manager
Loaded: loaded (/lib/systemd/system/php8.0-fpm.service; enabled; vendor pr>
Active: active (running) since Tue 2020-11-24 09:27:00 CST; 13s ago
Docs: man:php-fpm8.0(8)
Process: 11761 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /ru>
Main PID: 11740 (php-fpm8.0)
Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req>
Tasks: 3 (limit: 4657)
Memory: 7.0M
CGroup: /system.slice/php8.0-fpm.service
├─11740 php-fpm: master process (/etc/php/8.0/fpm/php-fpm.conf)
├─11756 php-fpm: pool www
└─11757 php-fpm: pool www
Nov 24 09:27:00 ubuntu2004 systemd[1]: Starting The PHP 8.0 FastCGI Process Man>

Because Nginx doesn’t have built-in support for PHP, you’ll need to manually edit the default site server block to enable PHP support.

Open the default server block on Ubuntu by running the commands below:

sudo nano /etc/nginx/sites-available/default

Then edit the lines below to enable PHP support.

     # Add index.php to the list if you are using PHP
        index index.php index.html index.htm index.nginx-debian.html;



        # pass PHP scripts to FastCGI server
        #
        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
        #
        #       # With php-fpm (or other unix sockets):
                fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;
        #       # With php-cgi (or other tcp sockets):
        #       fastcgi_pass 127.0.0.1:9000;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one

Save the file and exit.

Then reload Nginx and PHP.

sudo systemctl reload nginx
sudo systemctl reload php8.0-fpm

Testing PHP Processing

Now that you know how to install PHP with Apache and Nginx processing by running the commands below.

First, create a test PHP file called info.php in the web server default home directory.

sudo nano /var/www/html/info.php

Then copy and paste the line into the file and save it.

<?php
phpinfo();

Save the file and exit.

After that, open your browser and browse to the server hostname or IP address followed by the name of the file you created above.

http://localhost/info.php

That should display a PHP configuration settings are shown below:

That’s it!

Conclusion:

This post showed you how to install PHP 8.0 with Apache and Nginx support on Ubuntu 20.04 | 18.04. If you find any error above, please use the comment form below to report it.

Thanks,

You may also like the post below:

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.