How to Install Latest PHP 7.2 and PHP-fpm in CentOS 7.

share on:
install latest php-fpm and configure

PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation with some additional features useful for sites of any size, especially busier sites.

These features include:

  • Adaptive process spawning
  • Basic statistics (ala Apache’s mod_status)
  • Advanced process management with graceful stop/start
  • Ability to start workers with different uid/gid/chroot/environment and different php.ini (replaces safe_mode)
  • Stdout & stderr logging
  • Emergency restart in case of accidental opcode cache destruction
  • Accelerated upload support
  • Support for a “slowlog”
  • Enhancements to FastCGI, such as fastcgi_finish_request() – a special function to finish request & flush all data while continuing to do something time-consuming (video converting, stats processing, etc.)

 

Follow this link to Setup LAMP server on Centos 7.

Preparing the server.

Before installing anything let’s make our system up-to-date by using:

[root@debyum ~]# yum update

 

This updates package and the repositories caches.

To add latest version of PHP-FPM which is still receiving security updates we need to add a third party repository being maintained by a French named REMI. This repository has latest packages.

# yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm

 

After installation you can check the list of Remi repos available for PHP in /etc/yum.repos.d directory.

[root@debyum ~]# ls -l /etc/yum.repos.d/
total 80
-rw-r--r--. 1 root root 1664 May 17 19:23 CentOS-Base.repo
-rw-r--r--. 1 root root 1309 May 17 19:23 CentOS-CR.repo
-rw-r--r--. 1 root root 649 May 17 19:23 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root 314 May 17 19:23 CentOS-fasttrack.repo
-rw-r--r--. 1 root root 630 May 17 19:23 CentOS-Media.repo
-rw-r--r--. 1 root root 1331 May 17 19:23 CentOS-Sources.repo
-rw-r--r--. 1 root root 4768 May 17 19:23 CentOS-Vault.repo
-rw-r--r--. 1 root root 951 Oct 2 2017 epel.repo
-rw-r--r--. 1 root root 1050 Oct 2 2017 epel-testing.repo
-rw-r--r--. 1 root root 446 Jun 19 13:58 remi-glpi91.repo
-rw-r--r--. 1 root root 446 Jun 19 13:58 remi-glpi92.repo
-rw-r--r--. 1 root root 446 Jun 19 13:58 remi-glpi93.repo
-rw-r--r--. 1 root root 456 Jun 19 13:58 remi-php54.repo
-rw-r--r--. 1 root root 1314 Jun 19 13:58 remi-php70.repo
-rw-r--r--. 1 root root 1314 Jun 19 13:58 remi-php71.repo
-rw-r--r--. 1 root root 1314 Jun 19 13:58 remi-php72.repo
-rw-r--r--. 1 root root 1314 Jun 19 13:58 remi-php73.repo
-rw-r--r--. 1 root root 2605 Jun 19 13:58 remi.repo
-rw-r--r--. 1 root root 750 Jun 19 13:58 remi-safe.repo

 

We can enable Remi repo related to a particualar php version.

  • For php 5.6, we can enable remi-php56 repo.
  • For php 7.0, we can enable remi-php70 repo. Then remi-php71 for PHp7.1 and so on till Php 7.3.

Here we are going to Install latest stable PHP 7.2 on our server so we will enable the remi-php72 repo.

We can enable these repos by manually editing the repo file directly in /etc/yum.repos.d directory.

For example to enable PHP 7.2, we need to edit remi-php72.repo file and enable the [remi-php72] section.

[root@debyum ~]# vi /etc/yum.repos.d/remi-php72.repo

[remi-php72]
name=Remi's PHP 7.2 RPM repository for Enterprise Linux 7 - $basearch
#baseurl=http://rpms.remirepo.net/enterprise/7/php72/$basearch/
#mirrorlist=https://rpms.remirepo.net/enterprise/7/php72/httpsmirror
mirrorlist=http://cdn.remirepo.net/enterprise/7/php72/mirror
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi

 

Or we can use yum-config-manager from yum-utils package to enable the repo easily.

[root@debyum ~]# yum-config-manager --enable remi-php72
Loaded plugins: fastestmirror
======================================================== repo: remi-php72 =========================================================
[remi-php72]
........
........

 

Update and check the version of PHP on the server.
[root@debyum ~]# yum update

 

Then check the version of PHP and PHP-FPM package available on server.

[root@debyum ~]# yum info php
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: centos.myfahim.com
* epel: mirror.ehost.vn
* extras: centos.myfahim.com
* remi-php72: mirrors.thzhost.com
* remi-safe: mirrors.thzhost.com
* updates: centos.myfahim.com
remi-php72 | 2.9 kB 00:00:00 
remi-php72/primary_db | 195 kB 00:00:08 
Available Packages
Name : php
Arch : x86_64
Version : 7.2.8
Release : 1.el7.remi
Size : 3.2 M
Repo : remi-php72
Summary : ........

 

[root@debyum ~]# yum info php-fpm
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: centos.myfahim.com
* epel: mirror.ehost.vn
* extras: centos.myfahim.com
* remi-php72: mirrors.thzhost.com
* remi-safe: mirrors.thzhost.com
* updates: centos.myfahim.com
Available Packages
Name : php-fpm
Arch : x86_64
Version : 7.2.8
Release : 1.el7.remi
Size : 1.7 M
Repo : remi-php72
Summary : ......

 

Installation

In order to install PHP-FPM , run the following command:

[root@debyum ~]# yum install php-fpm php-gd php-pdo php-mbstring php-pear -y

 

Now that PHP-FPM is installed let’s ensure it also starts automatically with the server, run the following command:

[root@debyum ~]# systemctl enable php-fpm

 

In order to start it

[root@debyum ~]# systemctl start php-fpm

 

To make sure NGINX knows how to send PHP requests to PHP-FPM we need to  add some configuration in NGINX configuration file.

Now depending on how you choose to install NGINX,

the default location of your Nginx configration files  can be different.

Usually it is in  /etc/nginx/ ( 99.9%)  time but it can also be in /usr/share/nginx or /usr/local/share/nginx if you compiled it from source and used default values.

Assuming it is in /etc/nginx/ directory.

Open nginx.conf file and add the following content to it.

[root@debyum ~]# vi /etc/nginx/nginx.conf
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root           /usr/share/nginx/html;
fastcgi_pass   unix:/run/php-fpm/newuser.sock;
fastcgi_index  index.php;
fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
include        fastcgi_params;
}

 

Here we are using a unix socket to communicate between Nginx and PHP-FPM as they offer better performance.

 

To setup a unix socket for PHP-FPM, we can edit /etc/php-fpm.d/www.conf and make sure the value of listen directive is set to this.

listen = /run/php-fpm/newuser.sock

 

You can also make changes to these directives if you’re running NGINX as a particular user or group.

for example if you’re running Nginx as user=newuser and group=newuser then change the follows.

user = newuser
group = newuser

listen.owner = newuser
listen.group = newuser

 

and should also change expose_php directive in /etc/php.ini.

[root@debyum ~]# vi /etc/php.ini

expose_php = off

 

Further more you should also set appropriate values to following directives according to your applications requirements.

[root@debyum ~]# vi /etc/php.ini

    upload_max_filesize =
    post_max_size =
    max_execution_time =
    max_input_time =

 

After making all these changes you can restart PHP-FPM service again.

[root@debyum ~]# systemctl restart php-fpm.service

 

To test working of PHP we are going to put phpinfo() code in our Document root. Assuming Document root is  /usr/share/nginx/html/.

[root@debyum ~]# vi /usr/share/nginx/html/test.php

<? phpinfo(); ?>

 

DON’T FORGET TO DELETE THIS FILE AFTER TESTING

 

Save this file, then Open your browser and go to  http://server-ip-address/test.php  and you will see all the info about PHP and server.

 

Now you know how to install and configure latest PHP-FPM in your server.

 

Thanks for visiting this page. Good Day. 🙂

 

share on:
engy

engy

Hello there, My name is Rishi Guleria and I work as a Linux system administrator. I have created this blog to share what I have learned so far and to learn new things. Good Day. :)

1 Comment

Leave a Response

share on: