In this article we are setting up apache to run PHP as a fastCGI process server, and address that fastCGI server directly from within apache, via a dedicated proxy module (mod_proxy_fcgi.).  In short it means that we can now run php code using only stock apache and php binaries. No need to configure anything like suphp, mod_php or suexec.

Install Apache

yum install epel-release
yum install httpd httpd-devel

Install php-fpm

yum install php-fpm
systemctl enable php-fpm
systemctl start php-fpm

Create below two directories.

mkdir /etc/httpd/sites-available
mkdir /etc/httpd/sites-enabled

Create a file Named example.conf using below command.

touch /etc/httpd/sites-available/example.conf

Add below content in above example.conf file.

< VirtualHost *:80 >
ServerName example.com
ServerAlias example.com

DocumentRoot /var/www/vhosts/example.com/httpdocs
ErrorLog /var/www/vhosts/example.com/logs/error_log
CustomLog /var/www/vhosts/example.com/logs/access.log combined

< FilesMatch \.php$ >
SetHandler “proxy:unix:/var/run/php-fpm/php5-fpm_example.com.sock|fcgi://example.com/”
< /FilesMatch >

< Proxy fcgi://example.com >
ProxySet connectiontimeout=5 timeout=240
< /Proxy >

# Directory indexes
< IfModule dir_module >
DirectoryIndex index.htm index.html index.shtml index.php index.phtml
< /IfModule >

<Directory “/var/www/vhosts/example.com/httpdocs”>
Order allow,deny
Allow from all
AllowOverride FileInfo All
# New directive needed in Apache 2.4.3:
Require all granted
< /Directory >

< /VirtualHost >

< VirtualHost *:443 >
ServerName example.com
ServerAlias example.com www.example.com

DocumentRoot /var/www/vhosts/example.com/httpdocs
ErrorLog /var/www/vhosts/example.com/logs/error_log
CustomLog /var/www/vhosts/example.com/logs/access.log combined
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
SSLCertificateChainFile /etc/letsencrypt/live/example.com/chain.pem

< FilesMatch \.php$ >
SetHandler “proxy:unix:/var/run/php-fpm/php5-fpm_example.com.sock|fcgi://example.com/”
< /FilesMatch >

< Proxy fcgi://example.com >
ProxySet connectiontimeout=5 timeout=240
< /Proxy >

# Directory indexes
< IfModule dir_module >
DirectoryIndex index.htm index.html index.shtml index.php index.phtml
< /IfModule >

< Directory “/var/www/vhosts/example.com/httpdocs” >
Order allow,deny
Allow from all
AllowOverride FileInfo All
# New directive needed in Apache 2.4.3:
Require all granted
< /Directory >

< /VirtualHost >

Create Below Directories  and files.

mkdir -p /var/www/vhosts/example.com/httpdocs
mkdir /var/www/vhosts/example.com/logs/
touch /var/www/vhosts/example.com/logs/error_log  
touch /var/www/vhosts/example.com/logs/access_log
ln -s /etc/httpd/sites-available/example.conf  /etc/httpd/sites-enabled/example.conf

Allow apache  to read the conf files from the /etc/httpd/sites-enabled folder.  For that we are going to add following line at the end of /etc/httpd/conf/httpd.conf file.

IncludeOptional sites-enabled/*.conf

Now go the folder named /etc/php-fpm.d and create a file named example.conf  and add below content.

cd /etc/php-fpm.d/
touch example.conf

Add below content in example.conf file.

; Start a new pool named ‘www’.

[ example.com ]

listen = /var/run/php-fpm/php5-fpm_example.com.sock
listen.allowed_clients = 127.0.0.1

user = example
group = example

pm = ondemand
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.process_idle_timeout = 10s
pm.max_requests = 500

php_admin_value[error_log] = /var/www/vhosts/example.com/logs/www-error.log
php_admin_flag[log_errors] = on
php_value[session.save_handler] = files
php_value[session.save_path] = /var/lib/php/session

Execute Below commands.

mkdir /var/lib/php/session
chmod 777 /var/lib/php/session
touch /var/www/vhosts/example.com/logs/www-error.log
useradd d /var/www/vhosts/example.com/ example
chown R example:example /var/www/vhosts/example.com
systemctl restart httpd
systemctl restart php-fpm

This concludes the installation and configuration apache with php-fpm. Now if you visit the phpinfo page,
we can see FPM/FastCGI in the ServerAPI field.