How to install PHP on CentOS 8

PHP is one of the most widely used server-side programming languages in the world. Many well-known CMS and frameworks, such as WordPress, Magento, and Laravel are built on 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 your application supports it.

1. Install PHP on CentOS 8

CentOS 8 is released together with PHP7.2. This version supports most modern PHP applications, but will no longer be maintained as of November 2019. For the updated PHP version, please visit: Remi repository

  1. Enable Remi source warehouse

If you want to install the stable version of PHP 7.2, please ignore this step. Otherwise, if you want to install PHP7.3 or 7.4, please run the following command as root or another user with sudo permission to enable the Remi source repository:

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

The above command can also enable [EPEL software source repository] (https://linuxize.com/post/how-to-enable-epel-repository-on-centos/).

Once the above installation is complete, run the following command to get all available PHP versions:

sudo dnf module list php

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

Last metadata expiration check:0:02:11 ago on Fri 18 Oct 201908: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 the latest PHP release, enable 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

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

Two, 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 PHP dependency and used as a FastCGI server. Start the FPM server and enable startup:

sudo systemctl enable --now php-fpm

2.1、 Configure PHP, adapt to Apache

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

sudo systemctl restart httpd

2.2、 Configure PHP, adapt to Nginx

By default, PHP FPM runs as the apache user. To avoid permission issues, we changed the user to nginx. To do this, edit the following:

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

Make sure that the /var/lib/php directory has the correct user identity:

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

Once modified, restart the PHP FPM service:

sudo systemctl restart php-fpm

Next, edit the Nginx virtual host instruction and add the following code 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 work, restart the Nginx service:

sudo systemctl restart nginx

Three, conclusion##

PHP 7.2 can be installed from the default CentOS 8 source repository. If you want to install the latest version, you can enable Remi source repository.

Recommended Posts

How to install PHP on CentOS 8
How to install jdk1.8 on centOS7
How to install MySQL on CentOS 8
How to install Memcached on CentOS 8
How to install R on CentOS 8
How to install FFmpeg on CentOS 8
How to install Virtualbox on CentOS 8
How to install TeamViewer on CentOS 8
How to install Perl 5 on CentOS
How to install Git on CentOS 8
How to install PHP7.4 in CentOS
How to install Elasticsearch on CentOS 8
How to install Jenkins on CentOS 8
How to install Java on CentOS 8
How to install Go on CentOS 8
How to install GCC on CentOS 8
How to install Yarn on CentOS 8
How to install Nginx on CentOS 8
How to install Asterisk on CentOS 7
How to install Vagrant on CentOS 8
How to install Python 3.8 on CentOS 8
How to install Tomcat 9 on CentOS 8
How to install Webmin on CentOS 8
How to install Ruby on CentOS 8
How to install Skype on CentOS 8
How to install htop on CentOS 8
How to install Python on CentOS 8
How to install Elasticsearch on CentOS 8
How to install Postgresql on CentOS 8
How to install htop on CentOS 8
How to install TeamViewer on CentOS 8
How to install MariaDB on CentOS 8
How to install MongoDB on CentOS 7
How to install Odoo 13 on CentOS 8
How to install Apache on CentOS 8
How to install OpenCV on CentOS 8
How to install MongoDB on CentOS 8
How to install Apache Maven on CentOS 8
How to install Apache Kafka on CentOS 7
[Graphic] How to install tomcat on centos
How to install GCC compiler on CentOS 7
How to install offline JDK1.8 on centos7.0
How to install and configure Elasticsearch on CentOS 7
How to install and use Docker on CentOS 7
How to install RPM packages on CentOS Linux
How to install and configure VNC on CentOS 8
How to install and use Composer on CentOS 8
How to install and configure Redis on CentOS 8
How to install Node.js and npm on CentOS 8
How to install jdk1.8.0_151 and mysql5.6.38 on centos7.2.1511
How to install and configure phpMyAdmin on CentOS 6
How to install php7.3 in centos8 custom directory
How to install and use Curl on CentOS 8
How to install and configure Owncloud on CentOS 8
How to install VirtualBox client extension on CentOS 8
How to install Docker CE on RHEL 8 / CentOS 8
How to install and uninstall tomcat on centos
How to install and configure Redmine on CentOS 8
How to install Ruby on Ubuntu 20.04
How to install Memcached on Ubuntu 20.04
How to install Java on Ubuntu 20.04