How to Install PHP on CentOS 8

By 

Published on

4 min read

Install PHP on CentOS 8

PHP is one of the most used server-side programming languages. Many popular CMS and frameworks such as WordPress, Magento, and Laravel are built in PHP.

In this guide, we will discuss how to install PHP 7.2, 7.3, or 7.4 on CentOS 8. Before choosing which version of PHP to install, make sure that your applications support it.

We’ll also show you how to integrate PHP with Nginx and Apache.

Installing PHP on CentOS 8

CentOS 8 is distributed with PHP 7.2. This version supports most of the modern PHP applications, but will no longer be actively maintained as of November 2019. The newer PHP versions are available from the Remi repository .

Enable the Remi repository

If you’re going to install the distro stable PHP version 7.2, skip this step. Otherwise, if you want to install PHP 7.3 or 7.4 enable the Remi repository by running the following command as root or user with sudo privileges :

Terminal
sudo dnf install dnf-utils https://rpms.remirepo.net/enterprise/remi-release-8.rpm

The command above will also enable the EPEL repository .

Once the installation is complete, run the command below to get a list of all available PHP versions :

Terminal
sudo dnf module list php

The output will show a list of all available modules, including the associated stream, version, and installation profiles.

output
Last metadata expiration check: 0:02:11 ago on Fri 18 Oct 2019 08:31:43 PM UTC.
CentOS-8 - AppStream
Name    Stream       Profiles                     Summary                 
php     7.2 [d][e]   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

The default PHP module is set to PHP 7.2. To install a newer PHP release, enable the appropriate version:

PHP 7.3

Terminal
sudo dnf module reset php
sudo dnf module enable php:remi-7.3

PHP 7.4

Terminal
sudo dnf module reset php
sudo dnf module enable php:remi-7.4

You are now ready to install PHP on your CentOS server.

Install PHP

The following command will install PHP and some of the most common PHP modules:

Terminal
sudo dnf install php php-opcache php-gd php-curl php-mysqlnd

FPM is installed as a dependency and used as a FastCGI server. Start the FPM service and enable it to automatically start on boot:

Terminal
sudo systemctl enable --now php-fpm

Configuring PHP to work with Apache

If SELinux is running on your system, you’ll need to update the SELinux security context:

Terminal
sudo chcon -Rt httpd_sys_rw_content_t /var/www

If you are using Apache as your web server, restart the httpd service using the following command, and you are good to go:

Terminal
sudo systemctl restart httpd

Configuring PHP to work with Nginx

By default, PHP FPM runs as user apache. To avoid permission issues, we’ll change the user to nginx. To do so, edit the lines highlighted in yellow:

Terminal
sudo nano /etc/php-fpm.d/www.conf
/etc/php-fpm.d/www.confini
...
user = nginx
...
group = nginx

Make sure the /var/lib/php directory has the correct ownership :

Terminal
chown -R root:nginx /var/lib/php

Once done, restart the PHP FPM service:

Terminal
sudo systemctl restart php-fpm

Next, edit the Nginx virtual host directive, and add the following location block so that Nginx can process PHP files:

nginx
server {

    # . . . other code

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/run/php-fpm/www.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

For the new configuration to take effect, restart the Nginx service :

Terminal
sudo systemctl restart nginx

Update the SELinux security context:

Terminal
sudo chcon -Rt httpd_sys_rw_content_t /var/www

Conclusion

PHP 7.2 is available for installation from the default CentOS 8 repositories. If you want to install more recent version you need to enable the Remi repository.

If you have any questions or feedback, do not hesitate to leave a comment.

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page