Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Web Servers»How to Setup Apache with PHP/FastCGI on CentOS/RHEL 7/6

    How to Setup Apache with PHP/FastCGI on CentOS/RHEL 7/6

    RahulBy RahulOctober 13, 20173 Mins ReadUpdated:March 13, 2020

    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.

    • Install Apache with PHP-FPM/FastCGI on Ubuntu

    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.

    • Enable EPEL and REMI Repository

    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.

    Apache With PHP/FastCGI

    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.

    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
    

    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.

    1
    2
    3
    <?php
      phpinfo();
    ?>

    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.

    http://svr1.tecadmin.net/info.php
    

    Apache With PHP/FastCGI

    Apache2 CGI FastCGI FastCGI Apache mod_fastcgi PHP with CGI PHP with FastCGI
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleHow To Install NRPE on CentOS/RHEL 7/6
    Next Article How to Install Skype on Fedora 35/34

    Related Posts

    How To Install Linux, Nginx, MySQL, & PHP (LEMP Stack) on Ubuntu 22.04

    Updated:April 7, 20227 Mins Read

    How to Increase Request Timeout in NGINX

    Updated:January 13, 20222 Mins Read

    How To Disable HTTP Methods in Apache

    Updated:December 31, 20212 Mins Read

    How to Install and Secure Apache on Debian11

    6 Mins Read

    How To Enable Brotli Compression in Apache

    2 Mins Read

    How to Install Tomcat 10 on Debian 10

    Updated:February 18, 20225 Mins Read

    19 Comments

    1. Simon on July 22, 2021 8:27 am

      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
      restorecon /var/www/cgi-bin/php.fastcgi

      Reply
    2. Isuru on July 11, 2021 6:21 pm

      Thank you. You saved my day! This was the missing part I needed.

      Reply
    3. Israel M on May 15, 2019 10:50 pm

      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!

      Reply
      • vamsi on October 21, 2021 5:41 am

        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 ?

        Reply
    4. Juan Manuel on April 2, 2019 1:54 am

      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.

      Reply
    5. Nikolaj on October 19, 2018 1:12 pm

      what settings should be made to

      Reply
    6. Johan on July 7, 2018 5:16 pm

      this is a great tutorial, thnx
      it help me to me
      works great

      Reply
    7. Samir on January 9, 2018 10:51 pm

      Hello Rahul,

      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

      Reply
      • Rahul K. on January 10, 2018 3:41 am

        Yes, we comment these lines, So that Apache use FastCGI as default Server API. What issue are you facing with CGI/FastCGI?

        Reply
    8. Anju on March 5, 2016 7:17 am

      How to convert it to DSO

      Reply
    9. JOSH on September 22, 2014 7:49 am

      Hi,

      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

      Thanks,

      Reply
    10. Gio Arteaga on September 8, 2014 7:16 pm

      Hello,

      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.

      Reply
    11. menomime on August 8, 2014 8:34 am

      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:
      —————————————————————————-
      Forbidden

      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
      Any tips?

      Reply
      • menomime on August 11, 2014 6:20 am

        Hiya!

        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:
        http://www.inmotionhosting.com/support/community-support/website-troubleshooting/403-error-pcfgopenfile-unable-to-check-htaccess-file

        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!

        Reply
    12. Harry on May 18, 2014 5:23 pm

      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.

      Reply
      • Rahul on May 19, 2014 7:48 am

        Thanks Harry for your appreciation…
        We always welcome you to share your views, ideas or articles on tecadmin.net…

        Reply
    13. francesco on May 9, 2014 2:40 pm

      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:

      ” !/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 ”

      here i make one screenshot for show you where i’m stuck
      http://img235.imagevenue.com/img.php?image=645236368_screen_1_122_414lo.jpg

      how do I confirm the replacement?

      thank you for help
      best regards

      Reply
    14. francesco on May 9, 2014 10:22 am

      hello thanks for your guide i have only one problem after enter this :
      #!/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

      how i can confirm this edit ? in order to continue
      thank you so much
      regards

      Reply
      • Rahul on May 9, 2014 11:03 am

        Hi francesco,

        Are you facing any error with this configuration ?

        Reply

    Leave A Reply Cancel Reply

    Recent Posts
    • What is CPU? – Definition, Types and Parts
    • What is the /etc/aliases file
    • What is the /etc/nsswitch.conf file in Linux
    • How to Install Ionic Framework on Ubuntu 22.04
    • What is the /etc/hosts file in Linux
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.