Install PHP 7 on CentOS 7 _ Linuxize
Install PHP 7 on CentOS 7 _ Linuxize
CentOS 7 ships with PHP version 5.4 which has been officially EOL for quite some time and is no
longer supported.
By using PHP 7 your applications will load faster and consume less system resources.
In this tutorial, we will explain how to install or upgrade to PHP 7.0. 7.1, 7.2 and 7.3 on a CentOS 7
system. We’ll also show you how to integrate PHP with Nginx and Apache.
Also prior to installing a specific PHP 7.x version make sure that it is supported by your application.
Prerequisites
https://linuxize.com/post/install-php-7-on-centos-7/ 1/11
10/28/21, 6:30 PM Install PHP 7 on CentOS 7 | Linuxize
Before starting with this tutorial, make sure you are logged into your server with a user account
with sudo privileges or with the root user. It is best practice to run administrative commands as
sudo user instead of root. If you don’t have sudo user on your system you can create one by
following these instructions .
The Remi repository depends on the EPEL repository . Run the following commands to enable both
EPEL and Remi repositories:
Yum may prompt you to import the repository GPG key. Type y and hit Enter .
In the following sections, we will be covering how to install PHP 7.x by enabling the appropriate
Remi repository. If you already have PHP 5.4 installed on your system yum will update the PHP
packages.
https://linuxize.com/post/install-php-7-on-centos-7/ 2/11
10/28/21, 6:30 PM Install PHP 7 on CentOS 7 | Linuxize
02. Install PHP 7.3 and some of the most common PHP modules:
$ sudo yum install php php-common php-opcache php-mcrypt php-cli php-gd php-
03. Verify the PHP installation, by typing the following command which will print the PHP version :
$ php -v
Output
PHP 7.3.1 (cli) (built: Jan 8 2019 13:55:51) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.1, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.3.1, Copyright (c) 1999-2018, by Zend Technologies
https://linuxize.com/post/install-php-7-on-centos-7/ 3/11
10/28/21, 6:30 PM Install PHP 7 on CentOS 7 | Linuxize
01. First enable the PHP 7.2 Remi repository by running the following command:
02. Once the repository is enabled install PHP 7.2 and few most common PHP modules:
$ sudo yum install php php-common php-opcache php-mcrypt php-cli php-gd php-
$ php -v
Output
PHP 7.2.9 (cli) (built: Aug 15 2018 09:19:33) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.2.9, Copyright (c) 1999-2018, by Zend Technologies
02. Install PHP 7.1 and few most common PHP modules:
https://linuxize.com/post/install-php-7-on-centos-7/ 4/11
10/28/21, 6:30 PM Install PHP 7 on CentOS 7 | Linuxize
$ sudo yum install php php-common php-opcache php-mcrypt php-cli php-gd php-
03. To verify the installation, run the following command which will print the PHP version:
$ php -v
Output
PHP 7.1.21 (cli) (built: Aug 15 2018 17:56:55) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.1.21, Copyright (c) 1999-2018, by Zend Technologie
https://linuxize.com/post/install-php-7-on-centos-7/ 5/11
10/28/21, 6:30 PM Install PHP 7 on CentOS 7 | Linuxize
By default PHP FPM will run as user apache on port 9000. We’ll change the user to nginx and
switch from TCP socket to Unix socket. To do so edit the lines highlighted in yellow:
/etc/php-fpm.d/www.conf
...
user = nginx
...
group = nginx
...
listen = /run/php-fpm/www.sock
...
listen.owner = nginx
listen.group = nginx
https://linuxize.com/post/install-php-7-on-centos-7/ 6/11
10/28/21, 6:30 PM Install PHP 7 on CentOS 7 | Linuxize
Once you made the changes, enable and start the PHP FPM service:
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;
https://linuxize.com/post/install-php-7-on-centos-7/ 7/11
10/28/21, 6:30 PM Install PHP 7 on CentOS 7 | Linuxize
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 :
Conclusion
You have learned how to install PHP 7 on your CentOS server and how to configure your web server
to be able to handle PHP files.
php centos
BUY ME A COFFEE
Sign up to our newsletter and get our latest tutorials and news straight
to your mailbox.
Your email...
Subscribe
https://linuxize.com/post/install-php-7-on-centos-7/ 9/11
10/28/21, 6:30 PM Install PHP 7 on CentOS 7 | Linuxize
Subscribe
Related Articles
NOV 1, 2018
How to Install phpMyAdmin with Nginx on CentOS 7
OCT 5, 2018
https://linuxize.com/post/install-php-7-on-centos-7/ 10/11
10/28/21, 6:30 PM Install PHP 7 on CentOS 7 | Linuxize
© 2021 Linuxize.com
https://linuxize.com/post/install-php-7-on-centos-7/ 11/11