Apache

How to Setup Apache with PHP and FastCGI on CentOS and RHEL

setup-php-fastcgi
mm
Written by Santosh Prasad

FastCGI work as a middle ground between the PHP Apache module and the CGI application. When PHP scripts executed with FastCGI each request is passed from the web server to FastCGI via a communication socket. It Allows for much greater scalability as the web server and the PHP interpreter can be split into their own individual server environments if necessary. However a similar end result can also be achieved using nginx in front of Apache (such that nginx handles basic requests itself and only passes dynamic requests to Apache).

In this tutorial I will show how you can setup Apache with PHP and FastCGI on CentOS and RHEL system.

Setup Apache with PHP and FastCGI

Follow the below steps to setup Apache with PHP and FastCGI.

Step #1 : Install EPEL and REMI Repository

First of all you have to install EPEL and REMI repository on your system. Follow the below link to install EPEL and REMI repository.

Step #2 : Apache Installation

Now install the latest Apache on your system. Follow the bellow command to install latest Apache.

# yum install httpd

Step #3 : PHP and FastCGI Installation

Once installing Apache on your system now install PHP and FastCGI apache module on your system. Follow the bellow command to install the PHP and FastCGI.

# yum install php php-cli mod_fcgid

Step #4 : Disable Default PHP Handler

You have to disable default PHP handler on your system before using PHP/FastCGI handler. Edit the PHP configuration file for Apache /etc/httpd/conf.d/php.conf and comment out the below lines as shown below.

# <FilesMatch \.php$>
# SetHandler application/x-httpd-php
# </FilesMatch>

Step #5 : Enable FastCGI Handler

You have successfully installed Apache FastCGI module, now lets navigate to /var/www/cgi-bin directory, if this directory does not exist now create it. Then create php.fastcgi file and the below lines to this file. Make sure php.ini and php-cgi file should be there on the system.

# vim /var/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

Next change the permission of php.fastcgi script to make it executable by apache user.

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

Step #6 : Setup VirtualHost with FastCGI

Now create a apache virtualhost file with fastcgi support.

# vim /etc/httpd/conf.d/vhost.conf
<VirtualHost *:80> 
	ServerName www.looklinux.com 
	ServerAdmin [email protected] 
	DocumentRoot/var/www/html 
	ScriptAlias/cgi-canister/"/var/www/cgi-receptacle/" 
	<Directory "/var/www/html">
		Alternatives +Indexes +FollowSymLinks +ExecCGI 
		AddHandler php-fastcgi .php 
		Activity php-fastcgi/cgi-canister/php.fastcgi 
		AllowOverride All 
		Request allow,deny 
		Permit from All
       </Directory>
</VirtualHost>

Save and close file.

Step #7 : Restart Apache

Now restart the apache web service to apply changes.

# service httpd restart

Step #8 : Testing

To test your setup create a phpinfo page in your web document root /var/www/html/phpinfo.php and add the below lines.

<?php
phpinfo();
?>

Save and close file.

Now open your web browser and access the below URL and look at the value Server API if you get the CGI/FastCGI value it means your server successfully configured with FastCGI.

http://www.looklinux.com/phpinfo.php

phpinfo-page

 

Thank you! for visiting Look Linux.

If you find this tutorial helpful please share with your friends to keep it alive. For more helpful topic browse my website www.looklinux.com. To become an author at Look Linux Submit Article. Stay connected to Facebook.

About the author

mm

Santosh Prasad

Hi! I'm Santosh and I'm here to post some cool article for you. If you have any query and suggestion please comment in comment section.

Leave a Comment