Skip to main content

Setting Up PHP-FPM with Nginx and Apache2

This guide will show you how to configure PHP-FPM (FastCGI Process Manager) with Nginx and Apache2 to handle PHP processing. PHP-FPM offers performance improvements over traditional CGI and mod_php, and it allows for a more efficient and flexible PHP environment.

We will be setting up PHP-FPM to work with both Nginx and Apache2, using Unix sockets for better performance, although TCP sockets are also an option.

Prerequisites

  • A Linux server with Nginx or Apache2 installed.
  • PHP-FPM installed on your server.
  • Basic knowledge of server administration and command-line usage.

Step 1: Install PHP-FPM

If you don't have PHP-FPM installed, you can install it by running the following commands for your respective Linux distribution:

On Ubuntu/Debian:

sudo apt update
sudo apt install php-fpm php-mysql -y

On CentOS/RHEL:

sudo yum install php-fpm php-mysqlnd -y

Step 2: Configuring PHP-FPM to Use a Unix Socket

By default, PHP-FPM communicates over a TCP socket, but using a Unix socket can provide better performance and is more secure for local communication.

Edit PHP-FPM Configuration

  1. Open the PHP-FPM configuration file:

    sudo nano /etc/php/7.x/fpm/pool.d/www.conf

    Replace 7.x with your installed PHP version.

  2. Change the listen directive to use a Unix socket:

    listen = /var/run/php/php7.x-fpm.sock
    • This tells PHP-FPM to listen on a Unix socket at /var/run/php/php7.x-fpm.sock.
  3. Adjust the listen.owner and listen.group directives to ensure Nginx or Apache can access the socket:

    listen.owner = www-data
    listen.group = www-data
    • www-data is the default user for both Nginx and Apache on many Linux systems. If your web server runs under a different user, use that instead.
  4. Save the changes and close the file.

  5. Restart PHP-FPM to apply the changes:

    sudo systemctl restart php7.x-fpm

Step 3: Configuring Nginx to Use PHP-FPM

Now that PHP-FPM is set up, let's configure Nginx to handle PHP requests.

  1. Open your Nginx site configuration file:

    sudo nano /etc/nginx/sites-available/default
  2. Add the following location block within your server configuration:

    server {
    listen 80;
    server_name yourdomain.com;

    root /var/www/html;
    index index.php index.html index.htm;

    location / {
    try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php/php7.x-fpm.sock; # Use the Unix socket path
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
    }
    }
  3. Save and close the file.

  4. Test the Nginx configuration:

    sudo nginx -t
  5. Reload Nginx to apply the changes:

    sudo systemctl reload nginx

Step 4: Configuring Apache2 to Use PHP-FPM

To configure Apache2 with PHP-FPM, you’ll need to use mod_proxy_fcgi to pass PHP requests to PHP-FPM.

  1. Enable the necessary Apache modules:

    sudo a2enmod proxy_fcgi setenvif
    sudo a2enconf php7.x-fpm # Enable the PHP-FPM config

    Replace 7.x with the installed PHP version.

  2. Open the Apache site configuration file:

    sudo nano /etc/apache2/sites-available/000-default.conf
  3. Add the following lines to configure the PHP-FPM proxy settings:

    <VirtualHost *:80>
    DocumentRoot /var/www/html
    ServerName yourdomain.com

    <Directory /var/www/html>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Require all granted
    </Directory>

    # Pass PHP requests to PHP-FPM
    SetHandler proxy:unix:/var/run/php/php7.x-fpm.sock|fcgi://localhost
    </VirtualHost>
  4. Save and close the file.

  5. Restart Apache2 to apply the changes:

    sudo systemctl restart apache2

Step 5: Verifying PHP-FPM with Nginx and Apache

Create a phpinfo.php file to verify that PHP is working correctly with both web servers.

  1. Create the phpinfo.php file in the web root:

    sudo nano /var/www/html/phpinfo.php
  2. Add the following content:

    <?php
    phpinfo();
    ?>
  3. Visit http://yourdomain.com/phpinfo.php in your browser. If PHP-FPM is working, you should see the PHP information page.


Troubleshooting

  • PHP not processing: Ensure that the listen directive in your PHP-FPM configuration points to the correct Unix socket and that the correct user permissions are set for the socket.
  • Permission issues: If there are permission issues with the socket file, ensure that the web server user (e.g., www-data) has access to the socket.

Conclusion

You’ve successfully set up PHP-FPM with Nginx and Apache2 using a Unix socket for PHP processing. This setup ensures better performance and security for handling PHP requests.

With this configuration, your web server will forward PHP requests to PHP-FPM, allowing PHP scripts to be executed efficiently. Happy coding! ✨