How to Install PHP 7.x on CentOS 8 For Nginx

How do I install PHP 7.x on CentOS 8 Linux server along with Nginx? How can I install the latest version of PHP 7.4 on CentOS Enterprise Linux 8 server?

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.

How to Install PHP 7.x on CentOS 8

  1. Open the terminal app and log in to the remote CentOS 8 server
  2. Update CentOS 8 box, run sudo yum update
  3. Search for PHP version, run sudo yum search php
  4. Install and enable Remi’s repo for PHP 7.4, run sudo yum install yum-utils http://rpms.remirepo.net/enterprise/remi-release-8.rpm
  5. Install PHP 7.2.11 or 7.4 and FastCGI module for Nginx on CentOS 8, execute: sudo yum install php php-fpm
  6. Configure Nginx to use PHP
  7. Search and install additional PHP modules for graphics and database support using sudo yum search php-
  8. Enable and restart both PHP and Nginx server
  9. Test and verify both PHP installation

Let us see all commands in details.

Step 1 – Updating the CentOS 8 box

Run the following yum command or dnf command:
sudo yum update
Reboot the Linux system if a new kernel installed:
sudo reboot

Step 2 – Searching for PHP version on CentOS 8

Let us find out PHP version on CentOS Enterprise Linux 8 server, execute:
sudo yum search php-

You may have multiple versions of PHP installed on your systems. One can verify it by merely running the following command:
sudo yum module list php
Session

Last metadata expiration check: 0:06:14 ago on Wed Oct 28 07:43:38 2020.
CentOS-8 - AppStream
Name              Stream         Profiles                       Summary                                                               
php               7.2 [d]        common [d], devel, minimal     PHP scripting language                                                
php               7.3            common [d], devel, minimal     PHP scripting language                                                

Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled

The following example, indicates that PHP version 7.2, 7.3 and 7.4 available for installation:
sudo yum module list php
Sample outputs:

Last metadata expiration check: 0:00:05 ago on Mon Dec 16 13:28:05 2019.
CentOS-8 - AppStream
Name            Stream              Profiles                              Summary                          
php             7.2 [d]             common [d], devel, minimal            PHP scripting language           

Remi's Modular repository for Enterprise Linux 8 - x86_64
Name            Stream              Profiles                              Summary                          
php             remi-7.2            common [d], devel, minimal            PHP scripting language           
php             remi-7.3            common [d], devel, minimal            PHP scripting language           
php             remi-7.4            common [d], devel, minimal            PHP scripting language           

Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled

By default, PHP version 7.2 would install as indicated by the [d] flag.

A note about enabling different versions of PHP such as 7.3 and 7.4 on CentOS 8

I strongly suggest using default PHP version 7.2 for production web apps. However, if you need PHP version 7.3 or 7.4, type the following commands to enable Remi’s repo:
sudo yum install yum-utils http://rpms.remirepo.net/enterprise/remi-release-8.rpm

Enable default version

The default PHP version locked to PHP 7.2. It would be best if you ran enable command to set the desired PHP version. In other words, to enable PHP version 7.4, run:
sudo yum module list php
sudo yum module reset php
sudo yum module enable php:remi-7.4
## verify it php set to 7.4 ##
sudo yum module list php

Want to Install PHP 7.4 on CentOS 8 Linux? Enable Remi repo and set default using yum

For PHP version 7.3, execute:
sudo yum module list php
sudo yum module reset php
sudo yum module enable php:remi-7.3
## verify it php set to 7.3 ##
sudo yum module list php

Step 3 – Installing PHP on CentOS 8

Now that PHP version set, it is time to install PHP 7.x on your CentOS 8 cloud server by typing the following command:
sudo yum install php php-fpm
If you do not want Apache (httpd) installed as dependencies, run:
sudo yum install php-fpm php-common php-cli

Enable php-fpm service

Type the following systemctl command:
sudo systemctl enable php-fpm.service
Start the php-fpm service, run:
sudo systemctl start php-fpm.service
sudo systemctl status php-fpm.service

See how to reload/start/restart PHP-fpm service for more info:
sudo systemctl stop php-fpm.service
sudo systemctl restart php-fpm.service

How to 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

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
Verify php version. To find PHP version, type:
php --version

PHP 7.4.7 (cli) (built: Jun  9 2020 10:57:17) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.7, Copyright (c), by Zend Technologies

So I am using PHP version 7.4.7.

Test and verify that PHP 7.x is working on CentOS 8 along with Nginx

Create a new file as follows:
sudo vi /usr/share/nginx/html/hello.php
Append the following PHP code:

<?php
	echo "Hello, world!\n";
?>

Save and close the file in vim. Run it as follows using the curl command curl -I http://localhost/hello.php
curl http://localhost/hello.php

An example demo.php program

Next, create a new file as follows:
sudo vi /usr/share/nginx/html/demo.php
Append the following code:

<?php
	phpinfo();
?>

Save and close the file. Fire a web browser and type the following url:
http://your-domain-OR-ip/hello.php
http://192.168.2.254/hello.php
http://192.168.2.254/demo.php

How to install additional php modules

Try searching and installing additional modules as follows:
sudo yum search php-
sudo yum search php- | grep mysql
sudo yum search php74- ## for version 7.2 ##
sudo yum search php74- ## for version 7.3 ##
sudo yum search php74- ## for version 7.4 ##

======================== Summary & Name Matched: php74- ========================
php74-php-zephir-parser-devel.x86_64 : php74-php-zephir-parser developer files
============================= Name Matched: php74- =============================
php74-php.x86_64 : PHP scripting language for creating dynamic web sites
php74-build.x86_64 : Package shipping basic build configuration
php74-php-gd.x86_64 : A module for PHP applications for using the gd graphics
php74-xhprof.noarch : A Hierarchical Profiler for PHP - Web interface
php74-zephir.noarch : Zephir language for creation of extensions for PHP.
php74-php-ast.x86_64 : Abstract Syntax Tree
php74-php-cli.x86_64 : Command-line interface for PHP
php74-php-dba.x86_64 : A database abstraction layer module for PHP applications
php74-php-dbg.x86_64 : The interactive PHP debugger
php74-php-ffi.x86_64 : Foreign Function Interface
php74-php-fpm.x86_64 : PHP FastCGI Process Manager
php74-php-gmp.x86_64 : A module for PHP applications for using the GNU MP
php74-php-lz4.x86_64 : LZ4 Extension for PHP
php74-php-pdo.x86_64 : A database access abstraction module for PHP applications
php74-php-xml.x86_64 : A module for PHP applications which use XML
php74-runtime.x86_64 : Package that handles php74 Software Collection.
php74-php-imap.x86_64 : A module for PHP applications that use IMAP
php74-php-intl.x86_64 : Internationalization extension for PHP applications
php74-php-json.x86_64 : JavaScript Object Notation extension for PHP
php74-php-ldap.x86_64 : A module for PHP applications that use LDAP
php74-php-oci8.x86_64 : A module for PHP applications that use OCI8 databases
php74-php-odbc.x86_64 : A module for PHP applications that use ODBC databases
php74-php-pear.noarch : PHP Extension and Application Repository framework
php74-php-pggi.x86_64 : GTK bindings
php74-php-snmp.x86_64 : A module for PHP applications that query SNMP-managed
php74-php-soap.x86_64 : A module for PHP applications that use the SOAP protocol
php74-php-tidy.x86_64 : Standard PHP module provides tidy library support
php74-php-zstd.x86_64 : Zstd Extension for PHP
php74-scldevel.x86_64 : Package shipping development files for php74
php74-unit-php.x86_64 : PHP module for NGINX Unit
php74-php-devel.x86_64 : Files needed for building PHP extensions
php74-php-pgsql.x86_64 : A PostgreSQL database module for PHP
php74-php-pinba.x86_64 : Client extension for Pinba statistics server
php74-php-bcmath.x86_64 : A module for PHP applications for using the bcmath
php74-php-brotli.x86_64 : Brotli Extension for PHP
php74-php-common.x86_64 : Common files for PHP
php74-php-pspell.x86_64 : A module for PHP applications for using pspell
php74-php-recode.x86_64 : A module for PHP applications for using the recode
php74-php-snappy.x86_64 : Snappy Extension for PHP
php74-php-sodium.x86_64 : Wrapper for the Sodium cryptographic library
php74-php-sqlsrv.x86_64 : Microsoft Drivers for PHP for SQL Server
php74-php-xmlrpc.x86_64 : A module for PHP applications which use the XML-RPC
php74-php-enchant.x86_64 : Enchant spelling extension for PHP applications
php74-php-libvirt.x86_64 : PHP language binding for Libvirt
php74-php-mysqlnd.x86_64 : A module for PHP applications that use MySQL
php74-php-opcache.x86_64 : The Zend OPcache
php74-php-process.x86_64 : Modules for PHP script using system process
php74-php-embedded.x86_64 : PHP library for embedding in applications
php74-php-mbstring.x86_64 : A module for PHP applications which need multi-byte
php74-php-phalcon4.x86_64 : Phalcon Framework
php74-php-componere.x86_64 : Composing PHP classes at runtime
php74-php-litespeed.x86_64 : LiteSpeed Web Server PHP support
php74-php-maxminddb.x86_64 : MaxMind DB Reader extension
php74-php-pdo-dblib.x86_64 : PDO driver for Microsoft SQL Server and Sybase
php74-php-phpiredis.x86_64 : Client extension for Redis
php74-php-smbclient.x86_64 : PHP wrapper for libsmbclient
php74-php-wkhtmltox.x86_64 : HTML Converter
php74-php-libvirt-doc.noarch : Document of php-libvirt
php74-php-snuffleupagus.x86_64 : Security module for PHP
php74-php-zephir-parser.x86_64 : Zephir parser extension
php74-php-horde-horde-lz4.x86_64 : Horde LZ4 Compression Extension

Install PHP 7.x CentOS 8 modules

For example, install grphics and database support, run:
sudo yum install php-mysqlnd php-gd
Typical WordPress installation on CentOS 8 needs the following PHP extensions or modules:
sudo yum install php-mysqlnd php-gd php-pecl-zip php-mbstring php-xml php-opcache php-pecl-imagick

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.

See best PHP security practices for web apps for more info.

Conclusion

And, there you have it PHP installed and running on CentOS 8. By default, PHP version 7.2 installed on CentOS 8 Linux. However, devloper or sysadmin can install the latest version such as PHP 7.4 using the Remi repository for CentOS Enterprise Linux 8 server.

This entry is 2 of 3 in the Linux, Nginx, MySQL, PHP (LEMP) Stack for CentOS 8 Tutorial series. Keep reading the rest of the series:
  1. Nginx on CentOS 8
  2. PHP 7.x on CentOS 8 For Nginx
  3. Setup Let's Encrypt on CentOS 8 for Nginx
🐧 If you liked this page, please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04

Comments on this entry are closed.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.