Skip to content

Installing and Configuring FastCGI and PHP-FPM (New and updated guides) #6474

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ published: 2020-02-27
modified: 2021-12-29
modified_by:
name: Linode
title: "Installing and Configuring FastCGI and PHP-FPM on CentOS 8"
title_meta: "How to Install and Configure FastCGI and PHP-FPM on CentOS 8"
title: "Installing and Configuring FastCGI and PHP-FPM Using CentOS 8 and PHP 7.2"
title_meta: "How to Install and Configure FastCGI and PHP-FPM Using CentOS 8 and PHP 7.2"
relations:
platform:
key: install-fastcgi-php-fpm
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
---
slug: how-to-install-and-configure-fastcgi-and-php-fpm-on-debian-10
description: "This guide will show you how to install mod_fcgid and PHP-FPM on Debian 10 with a basic configuration that uses socket connections instead of TCP."
og_description: "This guide will show you how to install mod_fcgid and PHP-FPM on Debian 10. It will also provide a basic configuration that uses socket based connections, instead of TCP."
og_description: "This guide will show you how to install mod_fcgid and PHP-FPM using Debian 10 and PHP 7.3. It will also provide a basic configuration that uses socket based connections, instead of TCP."
keywords: ['list','of','keywords','and key phrases']
tags: ["web server","apache","debian","php"]
license: '[CC BY-ND 4.0](https://creativecommons.org/licenses/by-nd/4.0)'
published: 2020-02-27
modified: 2023-08-21
modified_by:
name: Linode
title: "Installing and Configuring FastCGI and PHP-FPM on Debian 10"
title_meta: "How to Install FastCGI and PHP-FPM on Debian 10"
title: "Installing and Configuring FastCGI and PHP-FPM Using Debian 10 and PHP 7.3"
title_meta: "How to Install FastCGI and PHP-FPM Using Debian 10 and PHP 7.3"
relations:
platform:
key: install-fastcgi-php-fpm
Expand Down Expand Up @@ -45,7 +46,7 @@ In this section, you will install the `mod_fcgid` and `PHP-FPM` modules on your

sudo apt-get install libapache2-mod-fcgid php-fpm htop

1. Load the `mod_proxy` and `mod_proxy_fcgi` modules by editing your main Apache configuration to add the lines included in the example. Both these modules are included by default in your Apache installation, but the must be explicitly loaded in order to use them. You will need these modules to proxy requests through `mod_fcgid` to your socket.
1. Load the `mod_proxy` and `mod_proxy_fcgi` modules by editing your main Apache configuration to add the lines included in the example. Both these modules are included by default in your Apache installation, but they must be explicitly loaded in order to use them. You will need these modules to proxy requests through `mod_fcgid` to your socket.

{{< file "/etc/apache2/apache2.conf" >}}
LoadModule proxy_module /usr/lib/apache2/modules/mod_proxy.so
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,253 @@
---
slug: how-to-install-and-configure-fastcgi-and-php-fpm-on-debian-11
description: "This guide will show you how to install mod_fcgid and PHP-FPM on Debian 11 with a basic configuration that uses socket connections instead of TCP."
og_description: "This guide will show you how to install mod_fcgid and PHP-FPM using Debian 11 and PHP 7.4. It will also provide a basic configuration that uses socket based connections, instead of TCP."
keywords: ['list','of','keywords','and key phrases']
tags: ["web server","apache","debian","php"]
license: '[CC BY-ND 4.0](https://creativecommons.org/licenses/by-nd/4.0)'
published: 2023-08-18
modified_by:
name: Linode
title: "Installing and Configuring FastCGI and PHP-FPM Using Debian 11 and PHP 7.4"
title_meta: "How to Install FastCGI and PHP-FPM Using Debian 11 and PHP 7.4"
relations:
platform:
key: install-fastcgi-php-fpm
keywords:
- distribution: Debian 11
aliases: ['/web-servers/apache/how-to-install-and-configure-fastcgi-and-php-fpm-on-debian-11/']
authors: ["Linode"]
---

`mod_fcgid` is an [Apache module](https://httpd.apache.org/mod_fcgid/) that uses the [FastCGI](https://en.wikipedia.org/wiki/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 Debian 11. It also provides 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 Debian 11](/docs/guides/how-to-install-a-lamp-stack-on-debian-11/). If you are new to Linux server administration, you may be interested in reading our [Linux System Administration Basics](/docs/guides/linux-system-administration-basics/) guide.

## Before You Begin

1. Complete the steps in the [How to Install a LAMP Stack on Debian 11](/docs/guides/how-to-install-a-lamp-stack-on-debian-11/) guide. After completing the LAMP stack guide, you should have an Apache virtual hosts configuration for your own website. This guide will refer to the site as `example.com`.

{{< note >}}
This guide's examples use PHP version 7.4. When running commands related to PHP, ensure you replace any version numbers with your own system's PHP version.
{{< /note >}}

## Install mod_fcgid and PHP-FPM

In this section, you will install the `mod_fcgid` and `PHP-FPM` modules on your Debian 10 Linode.

1. Update your system's [Apt repositories](https://wiki.debian.org/SourcesList).

```command
sudo apt-get update && sudo apt-get upgrade --show-upgraded
```

1. Install `mod_fcgid`, `PHP-FPM`, and `htop`. You will need the htop command line utility in a later section of this guide.

```command
sudo apt-get install libapache2-mod-fcgid php-fpm htop
```

1. To load the `mod_proxy` and `mod_proxy_fcgi` modules, edit your main Apache configuration, and add the lines included in the example. Both modules are included by default in your Apache installation, but they must be explicitly loaded in order to use them. You will need these modules to proxy requests through `mod_fcgid` to your socket.

```file {title="/etc/apache2/apache2.conf"}
LoadModule proxy_module /usr/lib/apache2/modules/mod_proxy.so
LoadModule proxy_fcgi_module /usr/lib/apache2/modules/mod_proxy_fcgi.so
```

1. Verify that the configuration is correct:

```command
sudo apache2ctl configtest
```

1. Restart the Apache web server:

```command
sudo systemctl restart apache2
```

## 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:

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

You should see the example output:

```output
listen = /run/php/php7.4-fpm.sock
```
If you see the above output, skip to step 6, otherwise continue to the next step to manually configure your UNIX steps.

1. If no output is returned, you will need to edit your [PHP pool configuration file](https://www.php.net/manual/en/install.fpm.configuration.php) by adding a `listen` setting with the address on which to accept FastCGI requests. Add the line in the example file.

```file {title="/etc/php/7.4/fpm/pool.d/www.conf"}
listen = /var/run/php/php7.4-fpm.sock
```

1. If the `listen.allowed_cclients = 127.0.0.1` is not already uncommented, do so now.

```file {title="/etc/php/7.4/fpm/pool.d/www.conf"}
listen.allowed_clients = 127.0.0.1
```

1. Restart the `php-fpm` daemon for these changes to take effect.

```command
sudo systemctl restart php7.4-fpm
```

1. Update your default Apache configuration file with the following basic settings for `mod_fcgid`. You may consider [changing these settings](https://httpd.apache.org/mod_fcgid/mod/mod_fcgid.html) based on your own needs.

```file {title="/etc/apache2/apache2.conf"}
AddHandler fcgid-script .fcgi .php .fpl
FcgidConnectTimeout 20
FcgidMaxRequestLen 268435456
FcgidMaxProcessesPerClass 10
FcgidIOTimeout 300
```

1. Check for configuration errors.

```command
sudo apache2ctl configtest
```

1. Edit your FastCGI module's configuration file to add the settings in the example file. Some of the example settings may already be included in your configuration. Add the missing settings.

```file {title="/etc/apache2/mods-available/fcgid.conf"}
<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>
```

1. Check for configuration errors.

```command
sudo apache2ctl configtest
```

1. If you received _Syntax OK_ for steps 6 and 8, restart the Apache service.

```command
sudo systemctl restart apache2
```

1. Check if PHP is working by creating and accessing a page with `phpinfo()` displayed. The following command will create a new file `info.php` in `/var/www/example.com/html/public_html/info.php`. Replace `example.com` with your own domain's root directory name.

```command
sudo echo "<?php phpinfo(); ?>" > /var/www/html/example.com/public_html/info.php
```

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

## Configuring PHP Pools

[PHP-FPM](https://php-fpm.org/) brings in the concept of [pools](https://www.php.net/manual/en/class.pool.php). 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 **www-data**.

{{< note >}}
To create the example `www-data` user, you can follow the steps outlined in our [Setting Up and Securing a Compute Instance](/docs/products/compute/compute-instances/guides/set-up-and-secure/#debian) guide.
{{< /note >}}

1. Create a copy of your original pool file to use as the foundation for your `example.com` pool configuration.

```command
sudo cp /etc/php/7.4/fpm/pool.d/www.conf /etc/php/7.4/fpm/pool.d/example.com.conf
```

1. 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/php7.4-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 {title="/etc/php/7.4/fpm/pool.d/example.com.conf"}
; 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 = www-data
group = www-data

...

listen = /var/run/php/php7.4-fpm_example.com.sock

```

1. Restart the `php7.4-fpm` process for the new pool to be created.

```command
sudo systemctl restart php7.4-fpm
```

1. Edit the virtual host file of `example.com` to use your new PHP-FPM pool. What you need to add or edit may differ depending on the current configuration of your virtual hosts file. 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 {title="/etc/apache2/sites-available/example.com.conf"}
<Directory /var/www/html/example.com/public_html>
Require all granted
</Directory>
<VirtualHost *:80>
ServerAdmin [email protected]
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/php7.4-fpm_example.com.sock|fcgi://localhost/var/www/html/example.com/public_html/"
</IfModule>
</VirtualHost>
```

1. Check the configuration file for errors.

```command
sudo apache2ctl configtest
```

{{< note >}}
If Apache continues to use apache handler, then Pass use `apache: a2enconf php7.4-fpm` and `a2dismod php7.4`.
{{< /note >}}

1. If there were no errors, restart Apache.

```command
sudo systemctl restart apache2
```

1. Use the command line tool top to verify that PHP-FPM is running the `example.com` pool as the `www-data` user and group. Replace `www-data with the user that you defined in your pool configuration file.

```command
top -c -u www-data
```

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

```output
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1978 www-data 20 0 3492 164 0 S 0.0 0.0 0:00.10 /usr/bin/htcacheclean -d 120 -p /var/cache/apache2/mod_cache_disk +
16234 www-data 20 0 207256 10348 4100 S 0.0 0.3 0:00.00 php-fpm: pool www
16235 www-data 20 0 207256 10348 4100 S 0.0 0.3 0:00.00 php-fpm: pool www
16255 www-data 20 0 52328 5748 3228 S 0.0 0.1 0:00.00 /usr/sbin/apache2 -k start
```
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ keywords: ['list','of','keywords','and key phrases']
tags: ["web server","apache","ubuntu","php"]
license: '[CC BY-ND 4.0](https://creativecommons.org/licenses/by-nd/4.0)'
published: 2020-02-27
modified: 2023-08-21
modified_by:
name: Linode
title: "Installing and Configuring FastCGI and PHP-FPM on Ubuntu 18.04"
Expand All @@ -15,6 +16,7 @@ relations:
key: install-fastcgi-php-fpm
keywords:
- distribution: Ubuntu 18.04
deprecated: true
aliases: ['/web-servers/apache/how-to-install-and-configure-fastcgi-and-php-fpm-on-ubuntu-18-04/']
authors: ["Linode"]
---
Expand Down Expand Up @@ -45,7 +47,7 @@ In this section, you will install the `mod_fcgid` and `PHP-FPM` modules on your

sudo apt-get install libapache2-mod-fcgid php-fpm htop

1. Load the `mod_proxy` and `mod_proxy_fcgi` modules by editing your main Apache configuration to add the lines included in the example. Both these modules are included by default in your Apache installation, but the must be explicitly loaded in order to use them. You will need these modules to proxy requests through `mod_fcgid` to your socket.
1. Load the `mod_proxy` and `mod_proxy_fcgi` modules by editing your main Apache configuration to add the lines included in the example. Both these modules are included by default in your Apache installation, but they must be explicitly loaded in order to use them. You will need these modules to proxy requests through `mod_fcgid` to your socket.

{{< file "/etc/apache2/apache2.conf" >}}
LoadModule proxy_module /usr/lib/apache2/modules/mod_proxy.so
Expand Down