How To Install PHP 8.0 on Ubuntu 20.04 / Ubuntu 18.04 | Holhol24

326

PHP is an open-source programming language widely used for web development to create dynamic websites such as E-commerce, Blogs, Internet Forums, etc.

In this post, we will see how to install PHP 8.0 on Ubuntu 20.04 / Ubuntu 18.04.

Add PHP Repository

Ondřej Surý, a third-party repository, offers PHP 8.0 / 7.x for Ubuntu operating system.

Update the repository cache.

sudo apt update

Install the below packages.

sudo apt install -y curl wget gnupg2 ca-certificates lsb-release apt-transport-https

Add the repository to your system.

sudo apt-add-repository ppa:ondrej/php

Update the repository index.

sudo apt update

Install PHP

Install PHP 8.0 on Ubuntu 20.04 / Ubuntu 18.04

Install PHP 8.0 with the below command.

sudo apt install -y php8.0 php8.0-cli php8.0-common

Install PHP 7.x on Ubuntu 20.04 / Ubuntu 18.04

Install PHP 7.4 with the below command.

sudo apt install -y php7.4 php7.4-cli php7.4-common

You can have multiple PHP versions on the system. Each PHP version will have separate configuration files under /etc/php/{8/7}.X directory.

Verify PHP Version

Once you install PHP packages, check the version with the below command.

php -v

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

PHP Support for Web Server

Both Apache and Nginx do not support PHP language by default when the browser requests the PHP page. So, we need to install the PHP module package to support PHP.

PHP Support for Apache

You can install the below package with Apache webserver to support PHP for the LAMP stack. This package provides the PHP modules for Apache 2 web server.

READ: How To Install LAMP Stack on Ubuntu 20.04

READ: How To Install LAMP Stack on Ubuntu 18.04

Change PHP version, if required

sudo apt install -y apache2 libapache2-mod-php8.0

PHP Support for Ngnix

Nginx does not have a PHP modules package to support PHP for the LEMP stack. To have PHP support for Nginx, we can use the PHP FastCGI Process Manager to handle PHP requests.

READ: How To Install LEMP Stack on Ubuntu 20.04

READ: How To Install LEMP Stack on Ubuntu 18.04

sudo apt install -y php8.0-fpm

PHP-FPM listens on the socket run/php/php8.0-fpm.sock by default.

To make PHP-FPM use TCP connection, edit the below file.

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

Then, change the listen parameter.

FROM:

listen = /run/php/php8.0-fpm.sock

TO:

listen = 127.0.0.1:9000

Add socket details in Nginx virtual host.

server {

# other codes

  location ~* .php$ {
    fastcgi_pass    127.0.0.1:9000;
    include         fastcgi_params;
    fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
    fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
  }
}

Install PHP Extensions

PHP extensions enable specific functions for your code. For example, installing PHP MySQL extension will let your PHP code to connect with the MySQL database.

PHP extensions package is normally named like php-.

To have MySQL support for PHP v8.0, you can install a php8.0-mysql package.

sudo apt install -y php8.0-mysql

Once you have installed a required extension, use the below command to verify it.

php -m | grep -i mysql

Output:

mysqli
mysqlnd
pdo_mysql

Conclusion

That’s All. Please share your feedback in the comments section.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More