How to Install Apache with PHP-FPM on CentOS 8

Install Apache with PHP-FPM on CentOS 8

In this article, we will have explained the necessary steps to install and configure Apache with PHP-FPM on CentOS 8. Before continuing with this tutorial, make sure you are logged in as a user with sudo privileges. All the commands in this tutorial should be run as a non-root user.

Install Apache with PHP-FPM on CentOS 8

Step 1. The first command will update the package lists to ensure you get the latest version and dependencies.

Step 2. Install Apache.

Apache is available in the default CentOS repositories, you are ready to install Apache:

Once the installation is complete, enable and start the Apache service:

You can also check the version of your web server in order to make sure that it was installed correctly:

During the installation, Apache creates firewalld service files with predefined rules for allowing access to HTTP (80) and HTTPS (443) ports. The following commands will permanently open the necessary ports:

Step 3. Install PHP with PHP-FPM.

To install PHP 7.4, you can enable the Remi repository by running the following command:

Next, enable the appropriate version:

The following command will install PHP-FPM and some of the most common PHP modules:

PHP uses FPM is installed as a dependency and used as FastCGI server. Start the FPM service and enable it to automatically start on boot:

Step 4. Configure PHP-FPM.

Let’s start the configuration process. First, edit PHP-FPM configuration file for Apache:

Restart the php-fpm service using the following command, and you are good to go:

Step 5. Create Apache VirtualHost.

Now, the time is to configure Apache to use php-fpm for processing PHP files. For this example, I am configuring FPM for a specific virtual host only:

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

Step 6. Test Setup.

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.

That’s all you need to do to install LAMP Stack on CentOS 8. I hope you find this quick tip helpful. If you have questions or suggestions, feel free to leave a comment below.