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.
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
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.
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
If you are using Apache as your web server, use the following command to restart the httpd
service:
sudo systemctl restart httpd
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
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