FastCGI functionality is very similar to working of CGI. FastCGI makes differences at few places than CGI like FastCGI processes are persistent and they can handle more than one request per process. FastCGI allows running programs on remote machines by multiplexes the environment information, standard input, output and error over a single full-duplex connection. Read more about FastCGI.
This tutorial will help you to set up Apache webserver with PHP and FastCGI on RedHat based systems.
Step 1 – Prerequsitis
Fast of all, enable REMI and EPEL yum repositories on your system. These repositories provide the lastest packages for RedHat based systems.
Step 2 – Install Apache2
Apache2 packages are available with the name HTTPD for Redhat based systems. You can use the following commands to install the latest available Apache2 (HTTPD) packages in configured repositories on your system.
yum install httpd
Step 3 – Install PHP and FastCGI
After installing the Apache web server, let’s install PHP and FastCGI Apache module on your system. You can install any version of the required PHP or simply use the following command to install available PHP packages. This tutorial doesn’t include installing PHP modules, So you can also install required PHP modules.
yum install php php-cli mod_fcgid
Step 4 – Disable Default PHP Handler
Before using PHP/FastCGI handler, you have to disable the default PHP handler on your system. Edit PHP configuration file for Apache (/etc/httpd/conf.d/php.conf) in your favorite text editor and comment following lines showing in below screenshot by adding the hash (#) sign at the start of the lines.
Step 5 – Setup FastCGI Handler
At this point we have successfully installed Apache FastCGI Module. Now nagigate to /var/www/cgi-bin directory, If not exists create directory. Then create a php.fastcgi file and add the following content to this file. Also make sure the php.ini file and php-cgi exist on your system.
#!/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
Change permissions of php.fastcgi script to make it executable by Apache server.
chown apache:apache /var/www/cgi-bin/php.fastcgi chmod +x /var/www/cgi-bin/php.fastcgi
Step 6 – Setup VirtualHost with FastCGI
Finally, create a VirtualHost in our Apache configuration file with FastCGI support. VirtualHosts are used to configure multiple sites with a single IP. Below configuration will allow siting svr1.tecadmin.net with any system IP on port 80.
<VirtualHost *:80> ServerName svr1.tecadmin.net ServerAdmin [email protected] DocumentRoot /var/www/html ScriptAlias /cgi-bin/ "/var/www/cgi-bin/" <Directory "/var/www/html"> Options +Indexes +FollowSymLinks +ExecCGI AddHandler php-fastcgi .php Action php-fastcgi /cgi-bin/php.fastcgi AllowOverride All Order allow,deny Allow from All </Directory> </VirtualHost>
Step 7 – Restart Apache and Test Setup
At this point, you have completed the Apache configuration with FastCGI support. Let’s restart the Apache server using the following command.
service httpd restart
Now create a file in your document root /var/www/html/info.php and add following content to check detailed php information.
Access your Apache server using an IP address for domain name followed by php.info file in your web browser like below. This will show the current configuration of PHP in your system. Look the value of Server API option, if you get this value CGI/FastCGI, it means server is properly configured to use FastCGI.
Can you update the “make php.fastcgi executable” section to cover installations that have SELinux enabled?
semanage boolean -m –on httpd_enable_cgi
semanage fcontext -a -t httpd_sys_script_exec_t /var/www/cgi-bin/php.fastcgi
Thank you. You saved my day! This was the missing part I needed.
Hello, i am just writing you to say thank you. This tutorial was extremely helpful for me to make php-fpm work. Your configuration of the Virtual Host and the php.fastcgi was incredible helpful.
Thanks a million!
Hello, How to make php-fpm work, im also having the same problem, here he’s installing fcgi, so what steps do we need to do after following this blog ?
Hi RAHUL I have proved your steps and I have got accomplished it, however I was asking me how could run multiple PHP versions on the same Apache instance in an server with ServerName established? I hope you could help me, because I have proved some tutorials in the web but none helped me to acommplished it.
what settings should be made to
this is a great tutorial, thnx
it help me to me
Step 4 is reason for not showing .php files?! … when I uncomment the lines it works but then Server API is Apache and not CGI/FastCGI.
Some must be wrong with the above tutorial.
ps, SAPI Modules does show CGI/FastCGI and FastCGI Process Manager
Yes, we comment these lines, So that Apache use FastCGI as default Server API. What issue are you facing with CGI/FastCGI?
How to convert it to DSO
I am trying to make FastCGI work in our CentOS 6. I’m following your guide but something off in my part
I notice using “AddHandler php5-fastcgi.php” and “Action php5-fastcgi /cgi-bin/php.fastcgi” doesn’t work in me. It always gives me an Internal Server Error when running a PHP.
I wonder if I miss something here. Hope you can help
I have been struggling for days on trying to get this method working on Centos 7.0. When you have a moment, can you try? mod_fastcgi is not available on Centos 7 even with EPEL repo installed. So, I decided to install mod_fcgid and sort of follow your guide. It works but phpinfo page still shows as below
Server API Apache 2.0 Handler
Your insight on this topic and help will be appreciated by me and many others.
Hi, being around in the internet long enough makes you aware that tons and tons of tutorials out there won’t really work, probably cause they weren’t clear enough about their environment.
But this one, this one worked! Thanks for the guide! It worked, i put a phpinfo() in the index page then everything i wanted to see was there. Kudos!
But now that i uploaded a full site, this showed up:
You don’t have permission to access / on this server.
Apache/2.2.15 (CentOS) Server at sitename.com Port 80
i have an htaccess file and the site runs on CodeIgniter 2.0.3
No reply like the other question above? Nvm, i found the answer and fixed it. all i did was change ownership and permissions on the folders.
I checked the error log file via watch tail error.log and i found this:
“(13)Permission denied: /home/USER/.htaccess pcfg_openfile: unable to check htaccess file, ensure it is readable”
Googling the error message, i found this site:
So this is the solution i came up with:
# cd /var/www/sitename/
# chown sitenameuser:root public_html
# chmod -R 755 public_html
*For the paranoid and suspicious developer (in a good way), the solution above was tested.
Hope this helps someone out there who was just as stumped as meh!
This guide really helped me. I was able to put multiple php’s on my apache. thanks a MILLION!!! I am going to create a guide for what I did, I can send you a copy if you would like. I have some other things I want to try but that can wait for later.
Thanks Harry for your appreciation…
We always welcome you to share your views, ideas or articles on tecadmin.net…
hello thank you for your reply..
no all correct but I’m stuck in step 4 :/
pratically i enter this command “vim /var/www/cgi-bin/php.fastcgi” and after open file to edit and i write:
exec /usr/bin/php-cgi ”
here i make one screenshot for show you where i’m stuck
how do I confirm the replacement?
thank you for help
hello thanks for your guide i have only one problem after enter this :
how i can confirm this edit ? in order to continue
thank you so much
Are you facing any error with this configuration ?