PHP is a great open source, and versatile programming language popular in web development. PHP is known to power major web platforms such as WordPress and Joomla. One way of getting started in PHP journey is by installing it in your local system. In this article we share the how-to process of setting up PHP 8.3 on Rocky / AlmaLinux / CentOS 9|8. There are plenty of resources available for people interested in learning PHP. These include blog tutorials, official documentation, public forums, and thousands of open-source projects.

Enable Remi’s and EPEL repositories

At the time of doing this post, PHP 8.3 is the most recent release. PHP 8.3 packages are available in Remi’s third party RPM repository. We are going to add it into our system.

### Rocky / AlmaLinux / CentOS 9 ###
sudo dnf -y install http://rpms.remirepo.net/enterprise/remi-release-9.rpm

### Rocky / AlmaLinux / CentOS 8 ###
sudo dnf -y install http://rpms.remirepo.net/enterprise/remi-release-8.rpm

We also need EPEL repository as dependency.

### Rocky / AlmaLinux / CentOS 9 ###
sudo dnf -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm

### Rocky / AlmaLinux / CentOS 8 ###
sudo dnf -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm

Update dnf cache after adding the two repositories.

sudo dnf makecache -y

List configured repositories on the system.

$ sudo dnf repolist
repo id                                                                    repo name
appstream                                                                  CentOS Stream 9 - AppStream
baseos                                                                     CentOS Stream 9 - BaseOS
epel                                                                       Extra Packages for Enterprise Linux 9 - x86_64
epel-cisco-openh264                                                        Extra Packages for Enterprise Linux 9 openh264 (From Cisco) - x86_64
epel-next                                                                  Extra Packages for Enterprise Linux 9 - Next - x86_64
extras-common                                                              CentOS Stream 9 - Extras packages
remi-modular                                                               Remi's Modular repository for Enterprise Linux 9 - x86_64
remi-safe                                                                  Safe Remi's RPM repository for Enterprise Linux 9 - x86_64

Reset default PHP module on the system.

sudo dnf module reset php -y

Install PHP 8.3 and extensions

Enable PHP 8.3 Remi module

sudo dnf module -y install php:remi-8.3

This should complete in few seconds.

Last metadata expiration check: 0:01:28 ago on Thu 08 Feb 2024 06:27:51 PM UTC.
Dependencies resolved.
======================================================================================================================================================================================================
 Package                                            Architecture                             Version                                             Repository                                      Size
======================================================================================================================================================================================================
....
 php                                                                                         remi-8.3

Transaction Summary
======================================================================================================================================================================================================
Install  9 Packages

Total download size: 9.9 M
Installed size: 55 M
Downloading Packages:
...

Install php package for CLI use and other key extensions.

sudo dnf -y install php php-{common,pear,cgi,curl,mbstring,gd,mysqlnd,gettext,bcmath,json,xml,fpm,intl,zip,imap}

Check the current PHP version to confirm installation was successful.

$ php --version
PHP 8.3.9 (cli) (built: July 04 2024 13:46:41) (NTS gcc x86_64)
Copyright (c) The PHP Group
...

You can also run the following commands to show active modules.

php --modules

Using PHP with Nginx or Apache

If you are using Nginx or Apache virtual hosts you can configure them to use PHP.

1) With Nginx web server

You will require PHP-FPM (FastCGI Process Manager) to listen for incoming PHP requests and executes them.

sudo dnf install nginx php-fpm vim -y

Start and enable the services.

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

Cnfigure PHP-FPM to listen on a socket instead of IP and port, and set other configurations.

$ 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 the PHP FPM service after applying the changes.

sudo systemctl restart php-fpm

Add the following block inside the http block of Nginx configuration file to forward PHP requests to PHP-FPM.

$ 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;
    }
}

Check configurations syntax.

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

2) With Apache web server

The execution of PHP code in Apache web server is typically through the mod_php module. This comes packaged with apache. The modules does the processing PHP code directly within the Apache process.

If Apache httpd server package is not installed, perform it as below.

sudo dnf -y install httpd php-fpm

If Nginx is active, disable it or change listen address from 80/443 to a different ports.

sudo systemctl disable --now nginx

Now start and enable httpd service.

sudo systemctl enable --now httpd php-fpm

Testing PHP 8.3 installation

Create a test PHP file in the web root directory /var/www/html/ to test your PHP works.

sudo echo "<?php phpinfo(); ?>" > /var/www/html/test.php

Access PHP test page oat http://localhost_or_serverip/test.php.

install php centos rocky alma

We can confirm PHP is working as anticipated. You can now delete test script.

sudo rm -f /var/www/html/test.php

Here is a list of best books to read on PHP and web technologies.

For more in depth information about PHP visit the php.net documentation.

LEAVE A REPLY

Please enter your comment!
Please enter your name here