How to install latest PHP-FPM in CentOS 7

share on:
install latest php-fpm and configure

keywords: install latest php-fpm

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.)

 

Preparing the server.

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

# 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

 

Now edit /etc/yum.repos.d/remi.repo and It looks something like that.

[remi]
name=Remi’s RPM repository for Enterprise Linux 7 – $basearch
#baseurl=http://rpms.remirepo.net/enterprise/7/remi/$basearch/
mirrorlist=http://rpms.remirepo.net/enterprise/7/remi/mirror
enabled=0 
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi

[remi-php55]
name=Remi's PHP 5.5 RPM repository for Enterprise Linux 7 - $basearch
#baseurl=http://rpms.remirepo.net/enterprise/7/php55/$basearch/
mirrorlist=http://rpms.remirepo.net/enterprise/7/php55/mirror
# NOTICE: common dependencies are in "remi-safe"
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi

.......................
.......................

[remi-php56]
name=Remi’s PHP 5.6 RPM repository for Enterprise Linux 7 – $basearch
#baseurl=http://rpms.remirepo.net/enterprise/7/php56/$basearch/
mirrorlist=http://rpms.remirepo.net/enterprise/7/php56/mirror
# WARNING: If you enable this repository, you must also enable “remi”
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi

 

Now we want to install PHP-5.6 and for that we need to enable [remi] & [remi-php56]  and we can do that by changing enabled=0 to enabled=1.

additionally we don’t need  [remi-php55] block and can delete it (should delete it) or keep it disabled by enabled=0. The end result should be as follows

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

    ............................
    ............................

    [remi-php56]
    name=Remi’s PHP 5.6 RPM repository for Enterprise Linux 7 – $basearch
    #baseurl=http://rpms.remirepo.net/enterprise/7/php56/$basearch/
    mirrorlist=http://rpms.remirepo.net/enterprise/7/php56/mirror
    # WARNING: If you enable this repository, you must also enable “remi”
    enabled=1
    gpgcheck=1
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi

 

 

Installation

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

# 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:

# systemctl enable php-fpm

 

In order to start it

# systemctl start php-fpm

 

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

Now depending on your version of NGINX or how you choose to install NGINX

(compiling it from source or installing it from repositories) , the default location where your Nginx configration files are 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:

$ 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   127.0.0.1:9000;
fastcgi_index  index.php;
fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
include        fastcgi_params;
}

 

Now we can make Nginx pass request to PHP-FPM via port 127.0.0.1:9000 or via a unix socket.

Here we are using port 9000 to pass request, now we also need to make sure that PHP-FPM is also listening on 127.0.0.1:9000.

For this we can edit /etc/php-fpm.d/www.conf and make sure the value of listen directive is set to this.

listen = 127.0.0.1:9000

 

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=nginx and group=nginx then change the follows.

user = nginx
group = apache

listen.owner = nginx
listen.group = apache

 

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

vi /etc/php.ini

expose_php = off

 

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

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.

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/.

 vi /usr/share/nginx/html/test.php

<? phpinfo(); ?>

( DON’T FORGET TO DELETE THIS FILE AFTER TESTING )

 

save the file, 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.

 

Hope this tutorial is useful to you in some way or another and 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. :)

Leave a Response

share on: