PHP (Hypertext Preprocessor) is a server-side scripting and programming language majorly used in developing web applications and websites. PHP allows developers to create dynamic and interactive web pages, and can be integrated with HTML, CSS, and JavaScript to create complete web applications that includes content management systems, e-commerce sites, forums, and blogs.
PHP code can be executed and is supported by web servers running on a variety of operating systems, including Linux, Windows, BSD, and macOS. It also enjoys support by a large and active community, which is heavily involved in its development of useful libraries, frameworks, and tools that unlocks PHP potential to thousands of developers.
PHP has support for various data types, including numbers, strings, arrays, and objects, and various programming constructs such as conditional statements, loops, and functions. In this article we shall cover the entire process of installing and using PHP 8.2 on Rocky Linux 8 / AlmaLinux 8.
For installation on other Linux distributions check out the links below:
- How To Install PHP 8.2 on Ubuntu
- How To Install PHP 8.2 on Debian
- How To Install PHP 8.2 on CentOS 7 / RHEL 7
- Install PHP 8.2 on Rocky Linux 9 / AlmaLinux 9
PHP 8.2 new features
- Type System Improvements – Added support for
true
type, and allowingnull
andfalse
types to be used as stand-alone types, and support for DNF types. - Readonly Classes – When a class is declared as
readonly
, all of its properties are automatically declaredreadonly
- Constants in Traits – In PHP 8.2, it is now possible to declare constants in traits.
- New random extension – Provides a new OOP API to generate random numbers with a pluggable architecture.
- 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
- utf8_encode and utf8_decode Functions Deprecated
- Dynamic Properties Deprecated – PHP 8.2 deprecates class properties that are dynamically declared
Install PHP 8.2 on Rocky Linux 8 / AlmaLinux 8
On RHEL 8 based systems and later, PHP modules are available through the AppStream modules. AppStream is a modular repository that allows you to install and manage software packages from different sources and versions.
Check AppStream modules for PHP on the system.
$ dnf module list|grep php
php 7.2 [d] common [d], devel, minimal PHP scripting language
php 7.3 common [d], devel, minimal PHP scripting language
php 7.4 common [d], devel, minimal PHP scripting language
php 8.0 common [d], devel, minimal PHP scripting language
From the output we can confirm the latest version of PHP module available is 8.0. To install PHP 8.2 on Rocky Linux 8 / AlmaLinux 8, we’ll use REMI third-party repository.
Step 1: Update system
Updating your Rocky Linux 8 / AlmaLinux 8 system is an important step in maintaining the stability and security of your system. You can check for available updates.
sudo dnf check-update
Run the commands below to update your system.
sudo yum update -y
After a successful update process, you can check if the system is up-to-date:
sudo dnf check-update
Optionally reboot the server after an update to make sure that all changes take effect.
sudo reboot
Step 2: Add EPEL & REMI repository
REMI is a third-party YUM repository with the latest and updated versions of PHP and other software packages for Red Hat Enterprise Linux (RHEL) and its derivatives, such as Rocky Linux and AlmaLinux.
First, we’ll install EPEL (Extra Packages for Enterprise Linux) repository, which is a dependency for the REMI repository. This can be done by running the following commands in your terminal:
sudo dnf -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
Next, we can install REMI repository on our system.
sudo dnf -y install http://rpms.remirepo.net/enterprise/remi-release-8.rpm
You can confirm they work by doing DNF cache update.
sudo dnf makecache -y
Then listing configured repositories on the system.
$ sudo dnf repolist
repo id repo name
appstream Rocky Linux 8 - AppStream
baseos Rocky Linux 8 - BaseOS
epel Extra Packages for Enterprise Linux 8 - x86_64
extras Rocky Linux 8 - Extras
remi-modular Remi's Modular repository for Enterprise Linux 8 - x86_64
remi-safe Safe Remi's RPM repository for Enterprise Linux 8 - x86_64
Step 3: Install PHP 8.2
Reset default PHP module.
sudo dnf module reset php -y
Enable PHP 8.2 REMI module which contains the packages to be installed.
sudo dnf module install php:remi-8.2
Since the required packages are available in the repository added to the system, let’s install of PHP 8.2 on Rocky Linux 8 / AlmaLinux 8 using dnf
or yum
package manager tool.
sudo dnf -y install php
Confirm installation prompts:
....
Transaction Summary
======================================================================================================================================================================================================
Install 9 Packages
Total download size: 9.9 M
Installed size: 45 M
Is this ok [y/N]: y
Confirm version of PHP installed on your Rocky / AlmaLinux system.
$ php -v
PHP 8.2.1 (cli) (built: Jan 3 2023 18:40:55) (NTS gcc x86_64)
Copyright (c) The PHP Group
Zend Engine v4.2.1, Copyright (c) Zend Technologies
Step 4: Install PHP 8.2 modules
You can also install additional PHP modules for your applications, such as php-zip, php-mysqlnd, php-curl
, and any other on the system from repository.
In this example we’re installing a number of PHP extensions usually dependencies to other PHP applications.
sudo dnf install php-{common,pear,cgi,curl,mbstring,gd,mysqlnd,gettext,bcmath,json,xml,fpm,intl,zip,imap}
Agree to continue with the installation.
....
Transaction Summary
======================================================================================================================================================================================================
Install 36 Packages
Total download size: 23 M
Installed size: 72 M
Is this ok [y/N]: y
To list all installed modules use the command:
$ php --modules
[PHP Modules]
bcmath
bz2
calendar
Core
ctype
curl
date
dom
exif
fileinfo
filter
ftp
gd
gettext
hash
iconv
imap
intl
json
libxml
mbstring
mysqli
mysqlnd
openssl
pcntl
pcre
PDO
pdo_mysql
pdo_sqlite
Phar
posix
random
readline
Reflection
session
shmop
SimpleXML
sockets
SPL
sqlite3
standard
sysvmsg
sysvsem
sysvshm
tokenizer
xml
xmlreader
xmlwriter
xsl
zip
zlib
[Zend Modules]
Step 5: Using PHP with Apache / Nginx
You can use PHP with either Apache and Nginx web servers, but the configurations required differ slightly depending on the web server.
Using PHP 8.2 with Nginx
PHP-FPM (FastCGI Process Manager) is a daemon that listens for incoming PHP requests and executes them. In this instance Nginx acts as a reverse proxy, forwarding incoming requests to PHP-FPM to be executed.
Install Nginx and FPM PHP extension on your system.
sudo dnf install nginx php-fpm vim -y
Start the services and enable them to come up when system is rebooted.
$ sudo systemctl enable --now nginx php-fpm
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service.
Status can be checked using the commands below:
systemctl status nginx php-fpm
Next we configure PHP-FPM to listen on a socket instead of IP and port, and set other configurations.
Open the file and make the following modifications.
$ sudo vim /etc/php-fpm.d/www.conf
user = nginx
group = nginx
;listen = 127.0.0.1:9000
listen = /var/run/php-fpm.sock
listen.owner = nginx
listen.group = nginx
listen.mode = 0660
Restart php-fpm service after the changes are made.
sudo systemctl restart php-fpm
Edit Nginx configuration file and add the following block inside the http
block to configure Nginx to forward PHP requests to PHP-FPM.
VirtualHost configuration sample:
$ sudo vim /etc/nginx/nginx.conf
server {
listen 80;
server_name myapp.example.com;
root /var/www/myapp;
index index.php index.html;
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/var/run/php-fpm.sock;
}
}
Validate your nginx configurations.
$ 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 Apache httpd package on the system.
sudo yum install httpd -y
We can’t have both Nginx and Apache web server listening on port 80. We need to disable Nginx to run Apache Httpd web server.
sudo systemctl disable --now nginx
Next start httpd
service.
sudo systemctl enable --now httpd
To ensure that PHP is running correctly, we can create a test PHP file in the web root directory, for example in /var/www/html/
sudo echo "<?php phpinfo(); ?>" > /var/www/html/test.php
Access PHP info page on your web browser http://localhost_or_serverip/test.php, you should see the PHP information page.
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
PHP 8.2 ships with great new features that aims at making development of web applications interesting. The steps captured in this article were to enable you install and configure PHP 8.2 on Rocky Linux 8 / AlmaLinux 8 system to work with either Nginx or Apache web server. It is recommended to test your applications against PHP 8.2 before upgrading PHP to ascertain everything works properly.