habibzain Just husband, father and enthusiastic men about System Administration. Love to write short article about it. Perhaps can help and be useful for others.

Install Apache PHP-FPM Centos 7

3 min read

habibza-Install-Apache-PHP-FPM-Centos-7

Today I want to write about install apache web server with PHP-FPM as a php handler in Centos7. PHP-FPM is an alternative PHP FastCGI implementation with some additional features useful for sites of any size, especially busier sites. It runs as its own process and is not dependent on the web server, like mod_php (default Apache module). Lets follow step by step install apache php-fpm centos.

For MPM apache use MPM Event. The default is MPM Prefork.

install apache php-fpm centos
php info Apache 2.0 handler

let’s get started.

There is Already Centos 7 Installed Properly

Before starting everything, I assume that there is already a linux server running well.

Install the packages needed to create a Web Server

Open your terminal editor, or remote ssh access. Then type this command :

yum install yum-utils
yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
yum-config-manager --enable remi-php71
yum update
yum install httpd httpd-tools mod_ssl php-fpm php-mcrypt php-cli php-gd php-curl php-mysql php-ldap php-zip php-fileinfo

Update Apache Configuration

Next, update the Apache configuration to use the mpm_event_module instead of the mpm_prefork_module.

root@habibza ~#vim /etc/httpd/conf.modules.d/00-mpm.conf

# LoadModule mpm_prefork_module modules/mod_mpm_prefork.so
LoadModule mpm_event_module modules/mod_mpm_event.so

Tell Apache to send all PHP requests over to PHP-FPM

Next, tell Apache to send all PHP requests over to PHP-FPM by creating a new configuration file.

root@habibza~# vim /etc/httpd/conf.d/php.conf
# Tell the PHP interpreter to handle files with a .php extension.

# Proxy declaration
<Proxy "unix:/var/run/php-fpm/default.sock|fcgi://php-fpm">
	# we must declare a parameter in here (doesn't matter which) or it'll not register the proxy ahead of time
    	ProxySet disablereuse=off
</Proxy>

# Redirect to the proxy
<FilesMatch \.php$>
	SetHandler proxy:fcgi://php-fpm
</FilesMatch>

#
# Allow php to handle Multiviews
#
AddType text/html .php

#
# Add index.php to the list of files that will be served as directory
# indexes.
#
DirectoryIndex index.php

#
# Uncomment the following lines to allow PHP to pretty-print .phps
# files as PHP source code:
#
#<FilesMatch \.phps$>
#	SetHandler application/x-httpd-php-source
#</FilesMatch>

Tweak PHP-FPM to Use Sockets

Tweak PHP-FPM to use sockets instead of TCP connections for performance purposes as follows.

root@habibza~# vim /etc/php-fpm.d/www.conf
; listen = 127.0.0.1:9000
listen = /var/run/php-fpm/default.sock
...
listen.allowed_clients = 127.0.0.1
listen.owner = apache
listen.group = apache
listen.mode = 0660
user = apache
group = apache

The last, enable the services to start on boot and start them up.

systemctl enable php-fpm
systemctl enable httpd
systemctl start php-fpm
systemctl start httpd

If you are using a firewall, open ports 80/443 accordingly. This example will open them up to the world. Adjust yours accordingly.

firewall-cmd --zone=public --permanent --add-service=http
firewall-cmd --zone=public --permanent --add-service=https
firewall-cmd –reload

Create simple file php.

root@habibza ~# vim /var/www/html/index.php
<?php 
phpinfo(); 
?>
install apache php-fpm centos
Php-fpm handler

Using multiple PHP-FPM pools with different User.

What happens if there is multi user and you want to isolate each site to their own PHP-FPM pool instead of using a shared pool? That is easy enough to do.

See also  Setup SpamAssassin Block Email Spam Alma Linux 8 / Centos 8

I assume I already have a user shell on Linux. my user is “habibza”.

root@habibza ~# id habibza
uid=1002(habibza) gid=1002(habibza) groups=1002(habibza)

And you already followed everything in this guide to get to this point, do the following.

First, disable the global Apache configuration for PHP:

root@habibza ~# mv /etc/httpd/conf.d/php.conf /etc/httpd/conf.d/php.conf.bak

Create a new PHP-FPM pool for this specific site and update it accordingly:

root@habibza ~# cp /etc/php-fpm.d/www.conf /etc/php-fpm.d/habibza.in.conf
root@habibza ~# vim /etc/php-fpm.d/habibza.in.conf
; listen = 127.0.0.1:9000
listen = /var/run/php-fpm/habibza.sock
...
listen.allowed_clients = 127.0.0.1
listen.owner = habibza
listen.group = apache
listen.mode = 0660
user = habibza
group = habibza

Then update the site’s Apache vhost to point to a new PHP-FPM pool in httpd conf file.

root@habibza ~# vim /etc/httpd/conf.d/habibza.conf
<VirtualHost *:80>
        ServerAdmin [email protected]
        ServerName habibza.habibza.in
        ServerAlias habibza.habibza.in
        DocumentRoot /var/www/html/habibza
      ErrorLog /var/log/httpd/habibza.erorr.log
     CustomLog /var/log/httpd/habibza.access.log combined

                 <Directory /var/www/html/habibza>
                        AllowOverride All
                        Options -Indexes +FollowSymLinks +MultiViews
                        Order allow,deny
                        allow from all
                </Directory>


        # Proxy declaration
        <Proxy "unix:/var/run/php-fpm/habibza.sock|fcgi://php-fpm">
                # we must declare a parameter in here (doesn't matter which) or it'll not register the proxy ahead of time
                ProxySet disablereuse=off
                # Note: If you configure php-fpm to use the "ondemand" process manager, then use "ProxySet disablereuse=on"
        </Proxy>

        # Redirect to the proxy
        <FilesMatch \.php$>
                SetHandler proxy:fcgi://php-fpm
        </FilesMatch>

</VirtualHost>

Restart Apache and PHP-FPM.

service httpd restart && service php-fpm restart

And now check running socker php-fpm in /var/run/php-fpm

Create simple php file in home directory /var/www/html/habibza.

root@habibza ~# vim /var/www/html/index.php 
<?php  
phpinfo();  
?> 
php info with php-fpm

To see isolated user with php-fpm, lets type ps faxu | grep habibza. See picture below.

See also  Install ISPConfig Centos7

That is short article about install apache php-fpm in centos 7. May be it’s helpful, please feel free to leave a comment if you have any questions and I’ll appreciate it

Credit Article :

  • https://www.stephenrlang.com/2018/02/centos-7-apache-2-4-with-php-fpm
  • https://www.nixtree.com/blog/php-fpm-fastcgi-process-manager/
habibzain Just husband, father and enthusiastic men about System Administration. Love to write short article about it. Perhaps can help and be useful for others.

Easy Create Laravel Project with Composer

Requirement Laravel, a popular PHP framework, is renowned for its elegant syntax and robust features, making it a top choice for web developers. One...
habibzain
1 min read

Virtualbox Change Storage Size

Changing the storage size of a virtual machine in VirtualBox involves a few steps. Here’s a general guide. Backup your Virtual Machine (VM) Before...
habibzain
35 sec read

Easy Setup Logrotate Nginx Ubuntu

Log rotation is an important aspect of managing log files on a Linux system, including those generated by Nginx. Logrotate is a utility that...
habibzain
1 min read

7 Replies to “Install Apache PHP-FPM Centos 7”

  1. Howdy wоuld you mind stating which blog platform you’re working with?
    I’m looking tօ start my own Ьlog іn the near futᥙre
    but I’m having a tough time selecting between BlogEngіne/Ꮃordpress/B2evolᥙtion and Drupal.
    Ƭhe reason I ask is bеϲause your layout seems differеnt then most blogѕ and I’m looking
    for somеthing uniqսe. P.S Apoⅼogies
    for getting off-topic but I had to ask!

    1. So, would you mind stating which blog platform you use?
      I want to start my own blog in the near future
      but I’m having trouble choosing between BlogEngіne/Ꮃordpress/B2evolutionᥙtion and Drupal.
      The reason I ask is because your layout seems different then most blogs and that’s what I’m looking for
      for something unique. P.S Apologies
      because it’s off topic but I had to ask!

      Okay no problem. I will be happy to answer it.
      1. I use WordPress as my blog platform.
      2. The theme I use, Mediumish from Wow Themes
      https://wowthemes.net/themes/mediumish-wordpress/
      3. I use a little CSS modification for the display layout.

      Hopefully my information can answer what you ask.
      Thank You.

Leave a Reply

Your email address will not be published. Required fields are marked *

Never miss good article from us, get weekly updates in your inbox