Installing and Configuring FastCGI and PHP-FPM on CentOS 8

Select distribution:
Traducciones al Español
Estamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
Create a Linode account to try this guide with a $100 credit.
This credit will be applied to any valid services used during your first 60 days.

mod_fcgid is an Apache module that uses the FastCGI protocol to provide an interface between Apache and Common Gateway Interface (CGI) programs. CGI helps a web server handle dynamic content generation and processing for scripting languages like PHP. This dynamic functionality is commonly used when running content management systems like WordPress on a LAMP stack.

This guide will show you how to install mod_fcgid and PHP-FPM on CentOS 8. It will also provide a basic configuration that uses socket based connections, instead of TCP. These steps will enable you to run PHP through mod_fcgid. Running PHP through mod_fcgid helps to reduce the amount of system resources used by forcing the web server to act as a proxy and only pass files ending with the .php file extension to PHP-FPM. Additionally, using PHP-FPM allows each virtual host to be configured to run PHP code as individual users.

This guide assumes that you are familiar and comfortable with setting up a LAMP stack on CentOS 8. If you are new to Linux server administration, you may be interested in reading our Linux System Administration Basics guide.

Before You Begin

  1. Complete the steps in the How to Install a LAMP Stack on CentOS 8 guide. After completing the LAMP stack guide, you should have an Apache virtual hosts configuration for your own website. This guide will continue to refer to the site as example.com.

    Note
    This guide’s examples will use PHP version 7.2. By default, PHP 7.2 is available for installation from the default CentOS 8 repositories. When running commands related to PHP, ensure you replace any version numbers with your own system’s PHP version.

Install mod_fcgid and PHP-FPM

In this section, you will install the mod_fcgid and PHP-FPM modules on your CentOS 8 Linode.

  1. Update your system if you have not already done so and install the wget command line utility.

    sudo yum update && sudo yum install wget -y
    
  2. Install mod_fcgid and PHP-FPM:

    sudo yum install mod_fcgid php-fpm
    
  3. Verify that the configuration is correct:

     sudo httpd -t
    
  4. Restart the Apache web server:

     sudo systemctl restart httpd
    

Configure Apache with PHP-FPM

You will now configure Apache to pass all requests for files with the .php extension, to the PHP wrapper through FastCGI.

  1. Configure PHP-FPM to use UNIX sockets instead of TCP. In this command, you will use grep to determine if the sockets are already being used. This command will search your php-fpm installation’s default pool configuration file for the setting.

    sudo grep -E '^\s*listen\s*=\s*[a-zA-Z/]+' /etc/php-fpm.d/www.conf
    

    You should see the following output:

    listen = /var/run/php-fpm/www.sock
    If you see the above output, skip to step 6, otherwise continue to the next step to manually configure your UNIX sockets.

  2. If no output is returned, you will need to edit your PHP pool configuration file by adding a listen setting with the address on which to accept FastCGI requests. Add the line in the example file.

    File: /etc/php-fpm.d/www.conf
    1
    2
    
    listen = /var/run/php-fpm/www.sock
        
  3. If the listen = 127.0.0.1 is not already uncommented, do so now:

    File: /etc/php-fpm.d/www.conf
    1
    2
    
    listen.allowed_clients = 127.0.0.1
        
  4. Restart the php-fpm daemon for these changes to take effect.

    sudo systemctl restart php-fpm
    
  5. With the text editor of your choice, update your default Apache configuration file with the following basic settings for mod_fcgid. You may consider changing these settings based on your own needs.

    File: /etc/httpd/conf/httpd.conf
    1
    2
    3
    4
    5
    6
    
    AddHandler  fcgid-script .fcgi .php .fpl
    FcgidConnectTimeout 20
    FcgidMaxRequestLen 268435456
    FcgidMaxProcessesPerClass 10
    FcgidIOTimeout 300
          
  6. Check for configuration errors.

    sudo httpd -t
    
  7. Edit your FastCGI module’s configuration file to add the settings in the example file.

    File: /etc/httpd/conf.modules.d/10-fcgid.conf
    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    <IfModule mod_fcgid.c>
      FcgidConnectTimeout 20
      AddType  application/x-httpd-php         .php
      AddHandler application/x-httpd-php .php
      Alias /php7-fcgi /usr/lib/cgi-bin/php7-fcgi
      <IfModule mod_mime.c>
        AddHandler fcgid-script .fcgi
      </IfModule>
    </IfModule>
  8. Check for configuration errors.

    sudo httpd -t
    
  9. If you received Syntax OK for steps 6 and 8, restart the Apache service:

    sudo systemctl restart httpd
    
  10. Check if PHP is working by creating and accessing a page with phpinfo() displayed. Create the info.php file.

     sudo touch /var/www/html/example.com/public_html/info.php
    
  11. Open the info.php file with the editor of your choice and add the following line:

    File: /var/www/html/example.com/public_html/info.php
    1
    2
    
    <?php phpinfo(); ?>
        

    Navigate to www.example.com/info.php to view your system’s information.

Configuring PHP Pools

PHP-FPM brings in the concept of pools. With pools, PHP-FPM can create and manage a pool of php processes to run PHP files from a site’s root directory. Each pool that is run by PHP-FPM can be run with separate user and group ID’s. Pools are a great way to provide more security when you are running multiple sites on one server. Running your site’s PHP scripts using dedicated user and group IDs, means that no one user can execute scripts on all sites running on your Linode. In this section you will create a pool for the domain example.com which is owned by the user bob.

Note
To create the example bob user, you can follow the steps outlined in our Securing Your User guide.
  1. Create a copy of your original pool file to use as the foundation for your example.com pool configuration.

     sudo cp /etc/php-fpm.d/www.conf /etc/php-fpm.d/example.com.conf
    
  2. Edit the file to change the socket name, user and group, and socket listen address. Ensure that the listen address is different from the listen address that you set in the main PHP pool configuration file. You can append the name of your site as part of the file name, for example, listen = /var/run/php-fpm/example.com.sock. Also, ensure that you comment out or replace any existing user and group and add your own user and group settings as shown in the example.

    File: /etc/php-fpm.d/example.com.conf
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    
    ; Start a new pool named 'www'.
    ; the variable $pool can be used in any directive and will be replaced by the
    ; pool name ('www' here)
    [example.com]
    
    ...
    
    ; Unix user/group of processes
    ; Note: The user is mandatory. If the group is not set, the default user's group
    ;       will be used.
    user = bob
    group = bob
    
    ...
    listen = /var/run/php-fpm/example.com.sock
  3. Restart the php7.2-fpm process for the new pool to be created.

    sudo systemctl restart php-fpm
    
  4. Edit the virtual host file of example.com to use your new PHP-FPM pool. Depending on your current virtual hosts file what you need to add and edit may differ. The <IfModuel mod_fcgid.c> directive and its contents is what you should add to your file. Ensure you replace any instance of example.com with your own domain name.

    File: /etc/httpd/sites-available/example.com.conf
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    
    <Directory /var/www/html/example.com/public_html>
            Require all granted
    </Directory>
    <VirtualHost *:80>
         ServerAdmin webmaster@example.com
         ServerName example.com
         ServerAlias www.example.com
         DocumentRoot /var/www/html/example.com/public_html
         ErrorLog /var/www/html/example.com/logs/error.log
         CustomLog /var/www/html/example.com/logs/access.log combined
         DirectoryIndex index.php
         <IfModule mod_fcgid.c>
             Options +ExecCGI
             FcgidConnectTimeout 20
             AddType  application/x-httpd-php         .php
             AddHandler application/x-httpd-php .php
             Alias /php7-fcgi /usr/lib/cgi-bin/php7-fcgi
             ProxyPassMatch " ^/(.*\.php(/.*)?)$" "unix:/run/php-fpm/example.com.sock|fcgi://localhost/var/www/html/example.com/public_html/"
         </IfModule>
    </VirtualHost>
  5. Check the configuration file for errors.

    sudo httpd -t
    
  6. If there were no errors, restart Apache.

    sudo systemctl restart httpd
    
  7. Use the command line tool, ps, to verify that PHP-FPM is running the example.com pool as the bob user and group. Replace bob with the user that you defined in your pool configuration file.

     ps aux | grep 'example.com'
    

    Your output should display bob as the user corresponding to the command that started the listed process php-fpm: pool example.com.

    bob      30425  0.0  0.2 179588 10156 ?        S    17:28   0:00 php-fpm: pool example.com
    bob      30426  0.0  0.2 179588 10160 ?        S    17:28   0:00 php-fpm: pool example.com
    bob      30427  0.0  0.2 179588 10160 ?        S    17:28   0:00 php-fpm: pool example.com
    bob      30428  0.0  0.2 179588 10160 ?        S    17:28   0:00 php-fpm: pool example.com
    bob      30429  0.0  0.2 179588 10160 ?        S    17:28   0:00 php-fpm: pool example.com

This page was originally published on


Your Feedback Is Important

Let us know if this guide made it easy to get the answer you needed.


Join the conversation.
Read other comments or post your own below. Comments must be respectful, constructive, and relevant to the topic of the guide. Do not post external links or advertisements. Before posting, consider if your comment would be better addressed by contacting our Support team or asking on our Community Site.