Apache is a most widely used web server in Linux platforms and is one of the best stacks to host PHP applications. With the release of Apache httpd 2.4, there is a Apache proxy module (mod_proxy_fcgi.) introduced and integrated by default with Apache. Also From release 5.3.3 onward, PHP now includes the fastCGI process manager (php-fpm) in the stock source code.
So in short we can install and enable those php and Apache related packages in a Linux server and setup to run Apache with PHP-FPM. In this setup Apache will send all the PHP files for processing to PHP-FPM and PHP-FPM will return the rendered output to the browser. using this stack, a website can load faster and handle more concurrent connections while using fewer resources. Using PHP-FPM we can run each PHP application on a separate process and can assign resource limitations.
In this blog article we configure Apache with php-fpm on Centos7. After that we can run php code using only stock Apache and php binaries. No need to configure anything like suphp, mod_php or suexec. You can also refer this article if you have the newer Centos 8. So lets get started.
Section 1. Prerequisites
In this section we are preparing our CentOS 7 server for the setting up Apache php-fpm. For that the first thing is we need a domain name in which your DNS configured to point to your CentOS 7 server IP. Creating DNS A record depends on our domain DNS. So do it as per your environment.
After that Disable SELinux in the server by issuing below command.
Next is Allow ports 80,443 on firewalld or Iptables. We can use below commands for that.
Next is Updating system packages by using below command.
Next is Install epel repository by using below command. This is necessary for getting latest packages.
Now Install the “Development Tools” group and the “yum-utils” packages using below commands.
Section 2. Install Apache
With the prerequisites completed, we are now proceeding with the installation of Apache web service in this section. For that issue below command.
Section 3. Install PHP
In this section we are installing the php 7.4 packages along with php-fpm.
Section 4. Configure Apache
In this section we are preparing our Apache web service to work with php-fpm. For that first create two directories using below command.
After that create a file Named training.conf using below command.
Add below content in above training.conf file. In that we are using our website name as training.supporthost.in. Replace that name with your own domain name. Save the file content.
After that Create Below Directories and files.
Now 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 Apache configuration file “/etc/httpd/conf/httpd.conf”.
Section 5. Issue SSL certificate.
In this section we are issuing SSL certificate for our website using let’s Encrypt certbot client. For that first install the certbot client package using below command.
Now issue the SSL for our website using below command. The command we followed are certbot with standalone option.
Section 6. Configure php-fpm
In this section we are configuring our php-fpm service to work with our website.
For that go the folder named /etc/php-fpm.d and create a file named training.conf .
Now Add below content in training.conf file.
Now Execute Below commands and create necessary files, website user and folders.
Finally restart the Apache and php-fpm service using below two commands.
This concludes the installation and configuration Apache with php-fpm. Now create a phpinfo page under our website document root and visit the phpinfo page url in the browser , we can see FPM/FastCGI in the ServerAPI field. Take a look at the below screenshot. This means that right now our Apache is configured to use with php-fpm. I hope this article is informative. Leave your thoughts at the comment box.