PHP is a language used to develop websites and web-based applications. It is well-suited for creation of interactive web pages which are dynamic in nature and can be embedded directly into HTML code. The execution of PHP code happens in the server while the output is sent to the client’s browser as plain HTML. PHP enjoys a large and active community of developers who have created frameworks and libraries that can be used to quickly and easily create web applications.
PHP 8.2 is the most recent version of PHP, released on December 8, 2022. This version includes new features and improvements, such as:
- Readonly Classes – When a class is declared as
readonly
, all of its properties are automatically declaredreadonly
- Type System Improvements – Added support for
true
type, and allowingnull
andfalse
types to be used as stand-alone types, and support for DNF types. - New random extension – Provides a new OOP API to generate random numbers with a pluggable architecture.
- Constants in Traits – In PHP 8.2, it is now possible to declare constants in traits.
- Sensitive Parameter Value Redaction – Addition of new built-in parameter Attribute named
#[\SensitiveParameter]
that PHP makes sure to redact the actual value in stack traces and error messages. - New Functions and Classes –
ini_parse_quantity
function,curl_upkeep
function ,openssl_cipher_key_length
,memory_reset_peak_usage
- Dynamic Properties Deprecated – PHP 8.2 deprecates class properties that are dynamically declared
- utf8_encode and utf8_decode Functions Deprecated
If you’re on Ubuntu Linux check out the guide below:
Install PHP 8.2 on Debian 11 / Debian 10
In this section we will cover installation process of PHP 8.2 on Debian 11 (Bullseye) and Debian 10 (Buster) Linux system.
1. Add Sury PPA repository
We start by adding PPA that contains the latest PHP packages. Install dependency packages for this.
sudo apt update
sudo apt install lsb-release apt-transport-https ca-certificates software-properties-common -y
With the tools added let’s import repository GPG key.
sudo wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg
Then add the repository to your sources list.
sudo sh -c 'echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list'
Update the package list to validate its functionality.
$ sudo apt update
Hit:1 http://mirror.hetzner.com/debian/packages bullseye InRelease
Hit:2 http://mirror.hetzner.com/debian/packages bullseye-updates InRelease
Hit:3 http://mirror.hetzner.com/debian/security bullseye-security InRelease
Hit:4 http://deb.debian.org/debian bullseye InRelease
Hit:5 http://security.debian.org/debian-security bullseye-security InRelease
Hit:6 http://deb.debian.org/debian bullseye-updates InRelease
Get:7 https://packages.sury.org/php bullseye InRelease [6,841 B]
Get:8 https://packages.sury.org/php bullseye/main amd64 Packages [376 kB]
Fetched 383 kB in 2s (245 kB/s)
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
2. Install PHP 8.2 packages
Once the repository has been added and confirmed to be working we can now install PHP 8.2 on Debian 11 / Debian 10. Run the commands below to perform the installation.
$ sudo apt install php8.2
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
apache2-bin libapache2-mod-php8.2 libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap libgdbm-compat4 liblua5.3-0 libpcre2-8-0 libperl5.32 libsodium23 perl perl-modules-5.32 php-common
php8.2-cli php8.2-common php8.2-opcache php8.2-readline psmisc
Suggested packages:
apache2-doc apache2-suexec-pristine | apache2-suexec-custom www-browser php-pear perl-doc libterm-readline-gnu-perl | libterm-readline-perl-perl make libtap-harness-archive-perl
Recommended packages:
apache2
The following NEW packages will be installed:
apache2-bin libapache2-mod-php8.2 libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap libgdbm-compat4 liblua5.3-0 libperl5.32 libsodium23 perl perl-modules-5.32 php-common php8.2
php8.2-cli php8.2-common php8.2-opcache php8.2-readline psmisc
The following packages will be upgraded:
libpcre2-8-0
1 upgraded, 19 newly installed, 0 to remove and 16 not upgraded.
Need to get 14.1 MB of archives.
After this operation, 76.0 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Run the following command to check the version of PHP currently installed on your Debian system.
$ php -v
PHP 8.2.4 (cli) (built: Mar 16 2023 14:37:38) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.4, Copyright (c) Zend Technologies
with Zend OPcache v8.2.4, Copyright (c), by Zend Technologies
3. Install PHP 8.2 extensions
PHP 8.2 extensions are libraries created to provide extra functionality to the PHP programming language not available natively.
See below example that demonstrates the installation of cli,zip,mysql,bz2,curl,mbstring,intl,common
PHP modules.
sudo apt install php8.2-{cli,zip,mysql,bz2,curl,mbstring,intl,common}
Any other module can be installed using the command syntax:
sudo apt install php8.2-<extension-name>
Where <extension-name> is replaced with the name of PHP module to be installed.
4. Using PHP with Nginx / Apache web server
You can use PHP with either Nginx or Apache web server to create your dynamic and interactive web pages.
Using PHP 8.2 with Nginx
With Nginx, PHP code is typically executed by a separate process, such as PHP-FPM (FastCGI Process Manager). PHP-FPM is a daemon that listens for incoming PHP requests and runs them in a separate process. Nginx acts as a reverse proxy, forwarding incoming requests to PHP-FPM to be executed.
You’ll need to install both Nginx and PHP-FPM
sudo apt install nginx php8.2-fpm
Once Nginx and FPM extension are installed, you’ll need to configure Nginx to forward incoming requests to PHP-FPM, using the FastCGI protocol. Edit the Nginx configuration file and add the following block inside the http
block to configure Nginx to forward PHP requests to PHP-FPM:
$ sudo vim /etc/nginx/nginx.conf
server {
listen 80;
server_name mysite.example.com;
root /var/www/mysite;
index index.php index.html;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
}
}
Verify Nginx configuration after making the change:
$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Using PHP 8.2 with Apache
When Apache web server PHP code is typically executed using the mod_php module, which is built into Apache. The mod_php is an Apache module responsible for processing PHP code directly within the Apache process.
Install both Apache and PHP, and any other necessary PHP extensions:
sudo apt install apache2 libapache2-mod-php8.2
Next enable mod_php module which allows Apache to process PHP code:
sudo a2enmod php8.2
Restart Apache web server
sudo systemctl restart apache2
5. Removing Old PHP and extensions
You can disable a module and enable a new one. See FPM example below.
sudo a2enconf php8.2-fpm #Enable PHP 8.2 FPM extension
sudo a2disconf php8.1-fpm #Disable PHP 8.1 FPM extension
To remove all PHP 8.1 packages after upgrading to PHP 8.2 run the commands below.
sudo apt purge php8.1*
Recommended books to read:
- Best Books to learn Web Development – PHP, HTML, CSS, JavaScript and jQuery
- Best Books To Master Web Design
- Best Books To Learn CSS & CSS3
- Best Books To Learn HTML & HTML5
- Best Apache and Nginx reference Books
Conclusion
We can conclude that PHP 8.2 brings many new features and improvements that aim at making PHP flexible, more powerful, and easy to use for web apps developers. With these new features you can experience a more efficient development process and make your code more readable, maintainable and secure. In this article we discussed how you can install PHP 8.2 on your Debian machine, and necessary configurations to host PHP applications on both Nginx and Apache web server. We hope you enjoyed this content and we look forward to creating more articles for you.