There are basically two ways to run PHP code with Apache web server:
- Apache PHP module
- PHP-FPM
In most situations the Apache PHP7.4 module is used to handle PHP code, which is usually fine. But in some cases, you need to run PHP code with PHP-FPM instead. Here’s how.
Disable the Apache PHP7.4 module.
sudo a2dismod php7.4
Install PHP-FPM.
sudo apt install php7.4-fpm
Enable proxy_fcgi and setenvif module.
sudo a2enmod proxy_fcgi setenvif
Enable the /etc/apache2/conf-available/php7.4-fpm.conf configuration file.
sudo a2enconf php7.4-fpm
Restart Apache for the changes to take effect.
sudo systemctl restart apache2
To test PHP scripts with Apache server, we need to create a info.php file in the document root directory.
sudo nano /var/www/html/info.php
Paste the following PHP code into the file.
<?php phpinfo(); ?>
Now in the browser address bar, enter server-ip-address/info.php. Replace server-ip-address with your actual IP. If you followed this tutorial on your local computer, then type 127.0.0.1/info.php or localhost/info.php.
You should see your server’s PHP information is FPM/FastCGI, which means Apache web server will pass PHP requests to PHP-FPM.