Install Apache with php-fpm on Ubuntu 16

Introduction

PHP-FPM (FastCGI Process Manager) is an alternative to the traditional mod_php method for serving PHP applications through Apache. It provides better process management and can improve PHP application performance, especially when handling multiple requests.

In this article, we will explain how to install and configure Apache with PHP-FPM on an Ubuntu 16 server and verify that PHP requests are being processed through FPM/FastCGI.

Prerequisites

Before starting, make sure you have:

  1. An Ubuntu 16 server.
  2. Root or sudo access to the server.
  3. Basic knowledge of Apache and Linux commands.
  4. A valid server hostname or IP address.

Implementation

Step 1: Install Apache

Install Apache using the following command:

apt-get install apache2

By default, the ServerName directive may not be configured. This can result in a warning when restarting Apache.

Open the Apache configuration file:

vi /etc/apache2/apache2.conf

Add the following line:

ServerName 127.0.0.1

If the server has a hostname, you can specify the hostname instead.

Check the Apache configuration for syntax errors:

apachectl -t

Enable Apache to start automatically after a reboot:

systemctl enable apache2

Restart Apache:

systemctl restart apache2

Step 2: Install PHP-FPM

Install PHP 7.0, PHP-FPM, and the required Apache FastCGI module:

apt-get -y install libapache2-mod-fastcgi php7.0-fpm php7.0

Enable the required Apache modules:

a2enmod actions fastcgi alias

Step 3: Configure Apache with PHP-FPM

Open the PHP-FPM Apache configuration file:

vi /etc/apache2/conf-enabled/php7.0-fpm.conf

Add the following configuration:

<IfModule mod_fastcgi.c>
    AddHandler php7-fcgi .php
    Action php7-fcgi /php7-fcgi
    Alias /php7-fcgi /usr/lib/cgi-bin/php7-fcgi
    FastCgiExternalServer /usr/lib/cgi-bin/php7-fcgi -socket /var/run/php/php7.0-fpm.sock -idle-timeout 900 -pass-header Authorization

    <Directory /usr/lib/cgi-bin>
        Require all granted
    </Directory>
</IfModule>

The same configuration can also be added inside an individual <VirtualHost> block if PHP-FPM needs to be configured only for a specific virtual host.

The default Apache virtual host configuration is:

/etc/apache2/sites-enabled/000-default.conf

Note: The exact Apache/PHP-FPM package and configuration can vary depending on the Ubuntu/PHP version being used.

Step 4: Verify the Apache Configuration

Check the Apache configuration for syntax errors:

apachectl -t

If the syntax is correct, restart Apache:

systemctl restart apache2

Restart the PHP-FPM service:

systemctl restart php7.0-fpm

Step 5: Create a PHP Information Page

Create a PHP information file in the Apache document root:

vi /var/www/html/info.php

Add:

<?php
phpinfo();
?>

Change the ownership of the file:

chown www-data:www-data /var/www/html/info.php

Now access the following URL from a browser:

http://IPaddress/info.php

On the PHP information page, search for Server API.

If PHP-FPM is configured correctly, it should display:

FPM/FastCGI

This confirms that PHP is being processed through PHP-FPM instead of mod_php.

Conclusion

In this article, we explained how to install Apache with PHP-FPM on Ubuntu 16. We configured Apache to communicate with PHP-FPM through the FastCGI interface and verified the configuration using a PHP information page.

Using PHP-FPM with Apache provides an efficient way to process PHP requests and gives administrators better control over PHP worker processes.

FAQs

1. What is PHP-FPM?

PHP-FPM stands for PHP FastCGI Process Manager. It is used to process PHP requests through the FastCGI protocol.

2. Why use PHP-FPM with Apache?

PHP-FPM provides improved process management and can handle PHP requests efficiently compared with the traditional mod_php approach.

3. How can I verify that PHP-FPM is working?

Create a PHP information page using phpinfo() and check the Server API parameter. It should show FPM/FastCGI.

Related Article

Setup password authentication for Apache2 in Centos – Pheonix Solutions

How to install and configure PHP with Nginx on centos7 – Pheonix Solutions

Talk to our experts

Have a technology challenge or looking for the right solution for your business? Our team can help you with cloud, DevOps, development, infrastructure, design, and more. Feel free to reach out to our experts here.

admin

Our team has expertise across software and web development, WordPress, e-commerce, mobile applications, UI/UX, cloud and infrastructure, DevOps, CI/CD, API integration, security, testing, automation, and technical support. The team also works with AI-based software solutions, LLMs, AI workflows, AI agents, and intelligent application development to help businesses automate processes and build smarter digital solutions. We focus on developing, deploying, maintaining, and optimising secure, scalable, and reliable technology solutions while helping businesses adopt modern technologies and drive digital transformation.

Leave a Comment

Scroll to Top