PHP stands for Hypertext Preprocessor. It is a free programming language that allows developers to create dynamic websites that interact with databases. PHP was created 26 years ago in 1994 by a Danish Programmer Rasmus Lerdorf and was referred to as Personal Home Page. It is cross-platform and open-source meaning it is available to anyone for use.

PHP is widely used by a number of established companies in developing web servers. This includes Content Management Systems like WordPress, Facebook, Wikipedia, and Web Hosting Platforms. PHP runs on a web browser but can also run non a command-line interface.

The latest version is version 8 with 8.1 being the latest.

PHP 8.0 includes features like;

  • Allows named parameters in function calls.
  • A new syntax to declare class properties right from the class constructor.
  • Just In Time (JIT) can be enabled to compile and cache native instructions.
  • The null-safe operator provides safety in method chaining when a return value is null.
  • New functions to ease string inspections.
  • Introduced throw exceptions from an expression.

PHP 8.1 include improved features like;

  • Use enum instead of a set of constants and get validation out of the box.
  • First-class callable syntax which allows referencing any function.
  • The use of nested attributes allows objects to be used as default parameter values.
  • Use intersection types to satisfy values with multiple type constraints at the same time.
  • Possible to declare final class constants.
  • Consistent type errors.

This guide will show you how to install PHP 8.1, and 8.0 on KDE Neon|Kubuntu.

Install PHP 8.1 on KDE Neon / Kubuntu

Update your system packages.

### Kubuntu ###
sudo apt update && sudo apt upgrade -y

### KDE Neon ###
sudo apt update && sudo pkcon update -y

Install dependencies using the following command.

sudo apt install lsb-release ca-certificates apt-transport-https software-properties-common

Add the Ondřej Surý, a Debian developer repository that has various PHP versions.

sudo add-apt-repository ppa:ondrej/php

Update your system and install PHP 8.1

sudo apt update
sudo apt install php8.1

Confirm the installation by checking the version.

$ php --version
PHP 8.1.6 (cli) (built: May 17 2022 16:48:09) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.6, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.6, Copyright (c), by Zend Technologies

Install PHP 8.1 Extensions

To install an extension you simply use the following syntax

sudo apt install php8.1-<extension-name>

You can check the available extensions by using the following command

sudo apt install php8.1-<TAB>

You can install the most prevalent extensions using the following command.

sudo apt install php8.1-cli php8.1-common php8.1-imap php8.1-redis php8.1-snmp php8.1-xml php8.1-zip php8.1-mbstring

Install PHP 8.0 on KDE Neon / Kubuntu

Update your system packages

### Kubuntu ###
sudo apt update && sudo apt upgrade -y

### KDE Neon ###
sudo apt update && sudo pkcon update -y

Install missing dependencies using the following command.

sudo apt install lsb-release ca-certificates apt-transport-https software-properties-common

Import Ondřej Surý repository which has various PHP versions. Press Enter to add the repository.

sudo add-apt-repository ppa:ondrej/php

Update your system and install PHP 8.0

sudo apt update 
sudo apt install php8.0

Confirm the installation by checking the version

$ php -version
PHP 8.0.19 (cli) (built: May 17 2022 18:48:59) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.19, Copyright (c) Zend Technologies
    with Zend OPcache v8.0.19, Copyright (c), by Zend Technologies

Install PHP 8.0 Extensions

The syntax to install PHP extensions is as follows

sudo apt install php8.0-<extension-name>

Use the following command to check the available extensions.

sudo apt install php8.0-<TAB>

Then install some of the relevant extensions using the following command.

sudo apt install php8.0-cli php8.0-common php8.0-imap php8.0-redis php8.0-snmp php8.0-xml php8.0-zip php8.0-mbstring

Using PHP with Apache

Install the required packages.

### PHP 8.1 ###
sudo apt install php8.1-fpm libapache2-mod-fcgid

### PHP 8.0 ###
sudo apt install php8.0-fpm libapache2-mod-fcgid

Then enable php-fpm using the following command.

sudo a2enmod proxy_fcgi setenvif
sudo a2enconf php8.0-fpm

Restart the Apache to save changes.

sudo systemctl restart apache2

Test PHP with Apache

Add the info.php file to the document

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

Append the following code

<?php 
phpinfo(); 
?>

Save and exit the file.

Then if you go to http://localhost/info.php on your browser, you should see a page like the one below.

Using PHP with Nginx

Install php-fpm using the following command.

### PHP 8.1 ###
sudo apt install nginx php8.1-fpm

### PHP 8.0 ###
sudo apt install nginx php8.0-fpm

Start and enable Nginx Server using the following command.

sudo systemctl start nginx
sudo systemctl enable nginx

Then check the status of php-fpm to use the following command.

$ systemctl status php*-fpm.service
● php8.1-fpm.service - The PHP 8.1 FastCGI Process Manager
     Loaded: loaded (/lib/systemd/system/php8.1-fpm.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2022-05-20 23:36:15 EAT; 50s ago
       Docs: man:php-fpm8.1(8)
    Process: 21990 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /run/php/php-fpm.sock /etc/php>
   Main PID: 21982 (php-fpm8.1)
     Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec"
      Tasks: 3 (limit: 4572)
     Memory: 9.1M
     CGroup: /system.slice/php8.1-fpm.service
             ├─21982 php-fpm: master process (/etc/php/8.1/fpm/php-fpm.conf)
             ├─21988 php-fpm: pool www
             └─21989 php-fpm: pool www

Remove the default configuration file

sudo rm /etc/nginx/sites-enabled/default

Create a Nginx server block file

sudo nano /etc/nginx/conf.d/default.conf

Append the following code which includes the lines for Nginx to process PHP files.

server {
  listen 80;
  listen [::]:80;
  server_name _;
  root /usr/share/nginx/html/;
  index index.php index.html index.htm index.nginx-debian.html;

  location / {
    try_files $uri $uri/ /index.php;
}

  location ~ \.php$ {
    fastcgi_pass unix:/run/php/php-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
    include snippets/fastcgi-php.conf;
  }

 # A long browser cache lifetime can speed up repeat visits to your page
  location ~* \.(jpg|jpeg|gif|png|webp|svg|woff|woff2|ttf|css|js|ico|xml)$ {
       access_log        off;
       log_not_found     off;
       expires           360d;
  }

 # disable access to hidden files
  location ~ /\.ht {
      access_log off;
      log_not_found off;
      deny all;
  }
}

Test the file

$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Restart the Nginx Server to save changes.

sudo systemctl restart nginx

Create a welcoming HTML file to test the connection

sudo nano /usr/share/nginx/html/welcome.html

Append the following command

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Welcome</title>
  </head>
  <body>
    <h1>Hello There, You have completed a Successsful Installation!</h1>
  </body>
</html>

Save and exit the file

Go to http://localhost/welcome.html on your browser which should show something like this.

Conclusion

From this guide, we installed PHP 8.1 and 8.0 on KDE Neon|Kubuntu. It is an easy setup by just installing the Ondřej Surý repository. PHP is a scripting language that is used by developers to create dynamic websites with interactive databases. We have also seen how to install PHP to work with Apache and Nginx on KDE Neon|Kubuntu.

Check out more guides

LEAVE A REPLY

Please enter your comment!
Please enter your name here