Install PHP8 in ubuntu with nginx

Tags:

PHP is arguably one of the most widely used server-side programming languages. It’s the language of choice when developing dynamic and responsive websites.

PHP 8 is currently latest stable version of PHP which was released on November 26, 2020. This latest version introduced many new features like Attributes, named parameter, match expression and improvements in error handling and consistency.

Step 1: Add repository

To install php we are going to use Ondrej PPA. This repository contains multiple versions of PHP.

sudo apt install software-properties-common

And then

sudo add-apt-repository ppa:ondrej/php

If prompted, press Enter

Step 2: Update APT

sudo apt update

Step 3: Install PHP along with PHP extensions

sudo apt install  php8.0 php8.0-fpm php8.0-cli php8.0-curl php8.0-mysql php8.0-curl php8.0-gd php8.0-mbstring php8.0-pear -y

Now start PHP FPM and enable it to launch every time system restarts.

systemctl start php8.0-fpm
systemctl enable php8.0-fpm

Make sure PHP is running by executing below command:

systemctl status php7.4-fpm

Step 4: Install NGINX

sudo apt install nginx

Step 5: Enable NGINX server to use PHP 8

server {

   # ...  server configurations

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

Once you make above changes, restart your nginx server to apply changes

sudo service nginx restart