• Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate
TecNStuff
Menu
  • Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate

How to Install PHP on CentOS 8

Written by Admin, Updated On January 2, 2020
centos, php
How to Install PHP on CentOS 8

PHP is a popular server-side scripting language and widely used for creating interactive and dynamic web pages. PHP 7.4 is the latest stable version of PHP. In this tutorial, we will show you how to install PHP 7.2, 7.3, or 7.4 on CentOS 8.

Install PHP on CentOS 8#

At first, you need to add EPEL & Remi repository from where you will be able to install PHP 7.4 on CentOS 8 Linux.

Enable the Remi repository#

Run the below command to enable the EPEL and 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

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

sudo dnf module list php

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

Last metadata expiration check: 0:05:18 ago on Thu 02 Jan 2020 18:15: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. You can install a newer PHP release by enabling the appropriate version:

PHP 7.3#

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

PHP 7.4#

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

Now your CentOS server ready for installation of PHP for specific version.

Install PHP#

Run the below command to install PHP with some of other common PHP modules:

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

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

sudo systemctl enable --now php-fpm

Configuring PHP with Apache#

If you are using Apache as your HTTP server then restart the httpd service. Run the below command:

sudo systemctl restart httpd

Configure PHP with Nginx#

The apache user is the default user for PHP FPM so it will create permission issues when using nginx web server. To prevent from this issue we need to change the user from apache to nginx. Edit the /etc/php-fpm.d/www.conf file and set as given below:

sudo nano /etc/php-fpm.d/www.conf

Change user and group from apache to nginx.

user = nginx
group = nginx

Also, ensure that the /var/lib/php directory has the correct ownership:

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

After that, restart the PHP FPM service by typing:

sudo systemctl restart php-fpm

Now, you have to edit the Nginx virtual host directive. Add the following location block so that Nginx can process PHP files:

server {
    ...
    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;
    }
}

To take the effect of new configuration you should restart nginx service by typing:

sudo systemctl restart nginx

Conclusion#

This tutorial explains how to install PHP 7.2, 7.3 or 7.4 on CentOS 8 server.

If you have any query or suggestion, feel free to comment below.

If our content helps you, please consider buying us a coffee

Thank you for your support.

Share On
Share on Facebook
Share on Twitter
Share on Reddit
Share on Tumblr
 Previous Article How to Install MySQL on CentOS 8
Next Article   How to Install Google Chrome Web Browser on CentOS 8

Related Posts

  • How to Install Php 8 on CentOS 8

    How to Install PHP 8 on CentOS 8

    January 27, 2021
  • How to Install Php 8 on Debian 10

    How to Install PHP 8 on Debian 10

    January 2, 2021
  • How to Install Python 3.9 on CentOS 8

    How to Install Python 3.9 on CentOS 8

    December 31, 2020

Leave a Reply Cancel reply

DigitalOcean Referral Badge

Popular Posts

© 2020 TecNStuff All rights reserved. This website is using and storing cookies on your browser. By using this website you agree our Privacy Policy.  Follow us -  Twitter | Facebook