Prerequsities

Shell access to the CentOS 8 system with sudo privileges account. Complete initial server setup for newly installed systems.

Step 1 – Install Apache

The Apache packages are available under the default AppStream repository. You can simply update the DNF cache and install Apache web server packages using the following commands. The mod_ssl package provides the functionality to use an SSL certificate for secure HTTP. After installation, enable the httpd service and start.

Step 2 – Install PHP with PHP-FPM

The Remi repository contains the latest PHP packages for the CentOS 8 Linux system. So first of all, you need to add the REMI repository to your system. Just execute the following command to add the repository. Then enable the required DNF module for PHP installation. Here we are enabling the module for installing PHP 7.4. You can change this to PHP 7.3 or PHP 7.2 as per your requirements. Now, install the PHP on your system. As we are going to use FastCGI Process Manager (FPM) for this setup. So install the php-fpm package as well. You may also require some more PHP modules, So install them before going next. After completing PHP installation enable PHP-FPM service and start it. Make sure the php-fpm service is running.

Step 3 – Configure PHP-FPM

At this step, you have installed all the required packages. Let’s start the configuration process. First, edit PHP-FPM configuration file for Apache: Make the changes like below. The latest versions of Apache can connect to the socket using a proxy. So make sure listen is set to a socket file. Then set user and group the same as Apache server using. If you need to connect FPM from a remote system change listen.allowed_clients to LAN IP instead of 127.0.0.1.

After making changes restart the php-fpm service to apply changes.

Step 4 – Create Apache VirtualHost

Now, the time is to configure Apache to use php-fpm for processing PHP files. Here is two options, Either you use FPM as default for all virtual hosts or you can specifically configure Virtual host to use FPM. In this way, other virtual hosts will not be affected. For this example, I am configuring FPM for a specific virtual host only. So edited configuration file in text editor: Now added proxy configuration using a socket file. Also configured all PHP script to use fpm handler for execution.

Save the virtual host configuration file and reload Apache to apply changes.

Step 5 – Enable Firewall Rules

Your server is ready to serve the application. If there is a firewall enabled on your system, make sure Apache ports are open to access from remote systems. The following commands will open the required ports for you.

Step 6 – Test Setup

All done. To test the environment, create a PHP script with phpinfo() function. Place this file to your server document root. Use the below command to do this. Then access info.php using server IP address (for default VirtualHost) for your configured domain in Apache VirtualHost.

Conclusion

You have successfully configured an Apache with PHP-FPM on your CentOS 8 or RHEL 8 Linux system. You can now host a website from your server.

How to Install Apache with PHP FPM on CentOS 8   TecAdmin - 46