PHP (Hypertext Preprocessor) is an open-source scripting language for creating dynamic web sites.
PHP code runs on the web server such as Nginx, Apache, and others. This page explains how to install PHP 7.x (7.2/7.3/7.4) on CentOS 8 Linux server along with the Nginx web server.
Ensure latest packages
sudo dnf update
Enable remi’s repo
sudo dnf install dnf-utils http://rpms.remirepo.net/enterprise/remi-release-8.rpm
Search for php
sudo dnf search php
For centos 8 you can check which php modules are enabled
sudo dnf module list php
By default php 7.2
is enabled for centos 8 so if that is the version you want install it with
sudo dnf -y install php
# To install an old version of php
sudo dnf install -y php70
# Install php modules
sudo dnf install php-fpm php-common php-cli
Install FastCGI module for Nginx on CentOS 8
sudo dnf install -y php-fpm
Additional php modules are available with pgp-*
packages
sudo sudo dnf search php-
Enabling different versions of PHP such as 7.3 and 7.4 on CentOS 8
if you need PHP version 7.3 or 7.4, ensure remi repo is enabled then type the following commands
sudo yum module reset php
sudo yum module enable php:remi-7.4
Verify php version
php -v
php --version
Enable and restart both PHP and Nginx server
sudo systemctl enable nginx php-fpm
sudo systemctl restart nginx php-fpm
Configure PHP to work with Nginx server
First, find out the location of PHP-FPM FastCGI server config using the cat command
cat /etc/nginx/conf.d/php-fpm.conf
Make sure Unix socket is up and running, run:
ls -l /run/php-fpm/www.sock
My php-fpm config for CentOS 8 with Nginx:
cat /etc/nginx/default.d/php.conf
The file
index index.php index.html index.htm;
location ~ \.php$ {
try_files $uri =404;
fastcgi_intercept_errors on;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass php-fpm;
}
Restart the nginx service/server
Again, run the systemctl command:
sudo systemctl restart nginx.service
Test and verify both PHP installation
Add contet to /usr/share/nginx/html/info.php
cat > /usr/share/nginx/html/info.php <<EOF
<?php
phpinfo();
?>
EOF
Check in browser by visiting http://localhost/info.php
or:
curl -I http://localhost/info.php
How to configure PHP 7.x
You need to edit the following files as per your needs:
/etc/php.ini – PHP’s initialization and config file. Do not modify this file. Instead create custom.ini in /etc/php.d/ directory.
/etc/php-fpm.conf – Gloable FPM (FastCGI) configuration file.
/etc/php-fpm.d/www.conf – FastCGI (FPM) www pool config file.
/etc/php.d/ – PHP modules config file.