Last Updated on 1 month by Sachin G

While managing a few CentOS or CentOS Stream and Red Hat Enterprise Linux (RHEL) servers for my web clients, I noticed PHP scripts running slower than expected, even with PHP-FPM in use. I decided to test out FastCGI integration with the Apache HTTP Server to see if performance could be improved—and here’s exactly what happened.

Step 1: Installed FastCGI on CentOS Linux

I am assuming that Apache or a web server is installed on a Linux box (CentOS/RHEL).Use the below command to install the latest available PHP and FastCGI Apache module

# yum install php php-cli mod_fastcgi

Yes, that’s all. This command grabbed the module from the repo and installed it under Apache’s module directory. I double-checked its activation by running:

httpd -M | grep fcgid

And sure enough, fcgid_module was listed. If you’re using RHEL, the process is identical, as long as you have the necessary repos enabled.

Step 2: FastCGI Setup on CentOS with Apache

Once I had mod_fcgid installed, I moved on to configuration.In the next steps, we need to create a file with name /home/www/cgi-bin/php.fastcgi file and should be added content to this file.

# vim /home/www/cgi-bin/php.fastcgi
 
#!/bin/bash
PHPRC="/etc/php.ini"
PHP_FCGI_CHILDREN=4
PHP_FCGI_MAX_REQUESTS=1000
export PHPRC
export PHP_FCGI_CHILDREN
export PHP_FCGI_MAX_REQUESTS
exec /usr/bin/php-cgi

php.fastcgi script should be runnable by Apache server, so we need to set up permission on the file like below.

# chown apache:apache /home/www/cgi-bin/php.fastcgi
# chmod +x /home/www/cgi-bin/php.fastcgi

Step 3: Configure the VirtualHost with FastCGI

Now, create a VirtualHost in our Apache configuration file with FastCGI configuration.

ServerName www.techtransit.org
ServerAdmin contact@techtransit.org
DocumentRoot /home/www/techtransit/
ScriptAlias /cgi-bin/ "/home/www/cgi-bin/"
<Directory "/home/www/techtransit/">
Options +Indexes FollowSymLinks +ExecCGI
AddHandler php5-fastcgi .php
Action php5-fastcgi /cgi-bin/php.fastcgi
AllowOverride All
Order allow,deny
Allow from All
</Directory >


Step 4: Restart Apache and Test Setup

For reading the configuration file, restart the web server. Below is the command to restart the web server.

service httpd restart

Create a file in your document root /var/www/html/info.php and add the following content to check detailed PHP information.

<?php
phpinfo();
?>

Access your file info.php through using ip address for domain name . This will reflect the current configuration of PHP in your system. If you get this value CGI/FastCGI, it means the server is properly configured to use FastCGI.

https://www.techtransit.org/info.php

Setting up FastCGI isn’t difficult once you get the hang of it, but small configuration errors can lead to 500 errors fast. Still, if you’re looking to improve your web hosting stack on CentOS or RHEL, this method is absolutely worth it.