How To Install PHP 8.1 on Ubuntu 20.04 / Ubuntu 18.04

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

Here, we will see how to install PHP 8.1 on Ubuntu 20.04 / Ubuntu 18.04.

Add PHP Repository

Ondřej Surý, a third-party repository, offers PHP 8.1 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 8.1 on Ubuntu 20.04 / Ubuntu 18.04

Install PHP 8.1 with the below command.

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

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.1

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.1-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.1/fpm/pool.d/www.conf

Then, change the listen parameter.

FROM:

listen = /run/php/php8.1-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 a PHP MySQL extension will let your PHP code connect with the MySQL database.

PHP extensions package is normally named like php<version>-<extn_name>.

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

sudo apt install -y php8.1-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.

phpubuntu 18.04ubuntu 20.04
Comments (0)
Add Comment