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.
Prerequisite:
- Operating System with CentOS 8
- Server IPv4 Address with Superuser Privileges (Root Access)
- Gnome Terminal for Linux Desktop
- PuTTy SSH client for Windows or macOS
- Powershell for Windows 10/11
- Familiar with DNF Commands
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.
sudo dnf update
Step 2. Install Apache.
Apache is available in the default CentOS repositories, you are ready to install Apache:
sudo dnf install httpd httpd-tools mod_ssl
Once the installation is complete, enable and start the Apache service:
sudo systemctl enable httpd sudo systemctl start httpd
You can also check the version of your web server in order to make sure that it was installed correctly:
httpd -v
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:
sudo firewall-cmd --permanent --zone=public --add-service=http sudo firewall-cmd --permanent --zone=public --add-service=https sudo firewall-cmd --reload
Step 3. Install PHP with PHP-FPM.
To install PHP 7.4, you can enable the Remi repository by running the following command:
sudo dnf install dnf-utils http://rpms.remirepo.net/enterprise/remi-release-8.rpm
Next, enable the appropriate version:
sudo dnf module reset php sudo dnf module enable php:remi-7.4
The following command will install PHP-FPM and some of the most common PHP modules:
sudo dnf update sudo apt install php php-fpm php-gd php-mysqlnd
PHP uses FPM is installed as a dependency and used as a FastCGI server. Start the FPM service and enable it to automatically start on boot:
sudo systemctl enable --now php-fpm sudo systemctl start php-fpm
Step 4. Configure PHP-FPM.
Let’s start the configuration process. First, edit PHP-FPM configuration file for Apache:
sudo nano /etc/php-fpm.d/www.conf
; listen = 127.0.0.1:9000 listen = /run/php-fpm/www.sock user = apache group = apache listen.allowed_clients = 127.0.0.1 listen.owner = apache listen.group = apache listen.mode = 0660 pm = dynamic
Restart the php-fpm service using the following command, and you are good to go:
sudo systemctl restart php-fpm
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:
sudo nano /etc/httpd/conf.d/example.com.conf
<VirtualHost *:80> ServerAdmin [email protected] DocumentRoot /var/www/html ServerName example.com ServerAlias www.example.com # Proxy declaration <Proxy "unix:/run/php-fpm/www.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 "pm = ondemand" #then use "ProxySet disablereuse=on" </Proxy> <FilesMatch \.php$> SetHandler proxy:fcgi://php-fpm </FilesMatch> ErrorLog logs/example.com-error.log CustomLog logs/example.com-access.log combined </VirtualHost>
Save the virtual host configuration file and reload Apache to apply changes:
sudo systemctl restart httpd
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:
echo "<?php phpinfo(); ?>" > /var/www/html/info.php
Then, access info.php using the server IP address (for default VirtualHost) for your configured domain in Apache VirtualHost.
http://localhost/info.php
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.