How To Set Up Apache with MPM Event and PHP-FPM on Ubuntu 20.04 - MrLiambi's blog

Breaking

My tweets

Advertisement

How To Set Up Apache with MPM Event and PHP-FPM on Ubuntu 20.04

 Apache is free, open-source, and the most widely used web server, largely because it is capable of working in different environments. Apache has the ability to manage different processes to serve an HTTP protocol request including processing the request, opening a socket, and handling new events.

These tasks are performed by the Apache Multi-Processing Module (MPM).

Apache MPM (Multi-Processing Modules) are Apache modules used for creating child processes in Apache. They allow more requests to be served simultaneously by passing off some processing work to listener threads, freeing up worker threads to serve new requests. Using PHP-FPM with MPM Event in an Apache webserver decreases the website page loading time and allows the webserver to handle more concurrent connections.

In this tutorial, we will learn how to configure Apache with MPM Event and PHP-FPM on Ubuntu 20.04.

Prerequisites

  • A fresh Ubuntu 20.04 VPS on the Atlantic.Net Cloud Platform
  • A root password configured on your server

Step 1 – Create a programage.com Cloud Server

First, log in to your Programage.com Cloud Server.  Create a new server, choosing Ubuntu 20.04 as the operating system with at least 1GB RAM. Connect to your Cloud Server via SSH and log in using the credentials highlighted at the top of the page.

Once you are logged in to your Ubuntu 20.04 server, run the following command to update your base system with the latest available packages.

apt-get update -y

Step 2 – Install LAMP Server

First, install the Apache web server, MariaDB, and PHP with the following command:

apt-get install apache2 mariadb-server php7.4 libapache2-mod-php7.4 -y

Once the LAMP server is installed, you can proceed to the next step.

Step 3 – Change the Multi-Processing Module

Before starting, you will need to switch the MPM from pre-fork to event and remove the php7.4 module connection between PHP and Apache.

First, stop the Apache service and disable the php7.4 module with the following command:

systemctl stop apache2
a2dismod php7.4

Next, disable the Pre-fork MPM module with the following command:

a2dismod mpm_prefork

Next, enable the Event MPM module with the following command:

a2enmod mpm_event

Once you are finished, you can proceed to the next step.

Step 4 – Configure Apache to Use the FastCGI Process Manager

In this section, we will install the PHP-FPM processor and proxy modules so Apache can communicate with PHP.

First, install the PHP-FPM with the following command:

apt-get install php7.4-fpm -y

Once installed, you will need to install libapache2-mod-fcgid library in order to communicate Apache and PHP.

You can install it with the following command:

apt-get install libapache2-mod-fcgid -y

Once installed, you will need to enable the PHP-FPM, Proxy and FastCGI Proxy module in Apache webserver.

You can enable them with the following command:

a2enconf php7.4-fpm
a2enmod proxy
a2enmod proxy_fcgi

Next, restart the Apache service to apply the changes:

systemctl restart apache2

Now, verify the MPM module with the following command:

apachectl -M | grep 'mpm'

You should get the following output:

mpm_event_module (shared)

Next, verify the Proxy and FastCGI Proxy module with the following command:

apachectl -M | grep 'proxy'

You should get the following output:

 proxy_module (shared)
 proxy_fcgi_module (shared)

Step 5 – Verify FastCGI Process Manager

At this point, Apache web server is configured to use the FastCGI Process Manager. It’s time to verify if PHP is using the FastCGI Process Manager.

First, create an info.php file inside the Apache document root directory:

nano /var/www/html/info.php

Add the following lines:

<?php phpinfo(); ?>

Save and close the file when you are finished.

Next, open your web browser and type the URL http://your-server-ip/info.php. You should see the following screen:

In the above page, you should see that Apache web server is using FPM/FastCGI.

Conclusion

In the above guide, we learned how to configure Apache with MPM Event and PHP-FPM on Ubuntu 20.04. Now, PHP-FPM will handle the PHP code and improve overall resource utilization. Give it a shot on your VPS Hosting account programage.com!

2 comments:

  1. In case of somebody is doing this on Ubuntu 16.04 and has downgraded from php 7 to php 5.6 (Ondrej), you have to replace some lines (based on JDawgg's answer):

    sudo apt-get install libapache2-mod-fastcgi php5-fpm to:

    sudo apt-get install libapache2-mod-fastcgi php5.6-fpm
    sudo a2dismod php5 mpm_prefork to:

    sudo a2dismod php5.6 mpm_prefork
    sudo nano /etc/apache2/conf-available/php5-fpm.conf to:

    sudo vim /etc/apache2/conf-available/php5.6-fpm.conf
    Installation process of php5.6-fpm already created the file, so there is no need to create one. However, I removed all existing text on that file as it was meant for mod_php5.


    AddHandler php5.fcgi .php
    Action php5.fcgi /php5.fcgi
    Alias /php5.fcgi /usr/lib/cgi-bin/php5.fcgi
    FastCgiExternalServer /usr/lib/cgi-bin/php5.fcgi -socket /var/run/php/php5.6-fpm.sock -pass-header Authorization -idle-timeout 3600

    Require all granted


    I changed path from /var/run/php5-fpm.sock to /var/run/php/php5.6-fpm.sock as the file was located there.

    sudo a2enconf php5-fpm to:

    sudo a2enconf php5.6-fpm
    sudo service apache2 restart && sudo service php5-fpm restart to:

    sudo service apache2 restart && sudo service php5.6-fpm restart
    I hope this helps other newbies like me :)

    ReplyDelete
  2. In order for this to work, also remember to enable : apache2 actions

    sudo a2enmod actions

    And download and install this apache module : libapache2-mod-fcgi like this :

    wget https://mirrors.edge.kernel.org/ubuntu/pool/multiverse/liba/libapache-mod-fastcgi/libapache2-mod-fastcgi_2.4.7~0910052141-1.2_amd64.deb
    sudo dpkg -i libapache2-mod-fastcgi_2.4.7~0910052141-1.2_amd64.deb

    ReplyDelete