How to Install PHP on CentOS 8

Published on

3 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 :

sudo dnf install dnf-utils http://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 :

sudo dnf module list php

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

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

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

PHP 7.4

sudo dnf module reset phpsudo 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:

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:

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:

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:

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:

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

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

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

Once done, restart the PHP FPM service:

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:

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 :

sudo systemctl restart nginx

Update the SELinux security context:

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.

This post is a part of the Install LAMP Stack on CentOS 8 series.
Other posts in this series: