How to configure Apache with MPM Event and PHP-FPM

Last updated December 5th, 2023 23:57

Apache HTTP web server has been developed over many years to work in various environments and address different user needs. One important issue that Apache, like any web server, must address is how to handle different processes for serving HTTP requests. Apache server includes a total of three different MPM (Multi-Processing Module) modules, and in this article, we will show you how to configure Apache with MPM Event and PHP-FPM on Ubuntu, specifically how to change the default Pre-fork variant to Event.

How to configure Apache with MPM Event and PHP-FPM on Ubuntu

The Apache web server includes three different MPM modules: Pre-fork, Worker, and Event. Let’s take a look at them:

Pre-fork MPM:

  • Pre-fork is the basic way Apache handles requests.
  • Each incoming request creates a new process (similar to a server copy) that handles that request.
  • These processes are independent and each has its own memory space.
  • The advantage of this approach is that an issue in one process doesn’t affect the other processes.
  • The disadvantage is that each process consumes memory and computational resources, which can limit the server’s performance.

Worker MPM:

  • Worker MPM is a more advanced way of processing requests in Apache.
  • Instead of creating new processes for each request, Apache uses a single main process and multiple threads.
  • Each thread handles one request at a time.
  • Threads share memory space, reducing memory consumption and computational resources compared to pre-fork.
  • By using threads, the server can efficiently handle multiple requests simultaneously and achieve higher performance.

Event MPM:

  • Event MPM is even more advanced than Worker MPM.
  • It uses both threads and asynchronous I/O operations (read or write operations without waiting for completion) for efficient request processing.
  • Event MPM uses an event-driven model that allows the server to handle multiple requests at once.
  • This approach is highly efficient for handling a large number of requests when the server is heavily loaded.
  • Event MPM also minimizes “keep-alive” connections, where the connection between the client and server remains open for reuse.

Let’s explain it in simpler terms:

Imagine that the Apache HTTP Server is a large restaurant team that processes orders from customers.

Pre-fork MPM:

  • We have a restaurant with a large number of chefs, and each chef works independently.
  • When a customer comes in and places an order, the restaurant assigns a new chef to the customer to process that one order.
  • If there are multiple customers in the restaurant, each of them gets their own chef.
  • Each chef has their own workspace (memory) and works independently of others.
  • This ensures that an issue with one chef doesn’t affect the others, but it requires more resources (chefs).

Worker MPM:

  • This time, we have a restaurant with a smaller number of chefs, but they work as a team.
  • Instead of each customer having their own chef, the chefs collaborate to prepare all the orders together.
  • When a customer comes in with an order, one available chef from the team takes it and starts processing it.
  • Other chefs in the team can work on processing other orders at the same time.
  • The chefs share workspace (memory) and efficiently work together to handle multiple orders simultaneously.

Event MPM:

  • Now we have a modern restaurant with a small but highly efficient team of chefs.
  • The chefs use special techniques that allow them to process orders quickly.
  • When a customer arrives, one chef immediately takes the order and starts processing it.
  • At the same time, other chefs can prepare additional ingredients or take care of other orders.
  • The team of chefs collaborates and effectively handles many orders at once.

How to configure Apache with MPM Event and PHP-FPM on Ubuntu

Now, let’s walk through the steps to change the MPM module from the default Pre-fork to Event on a Ubuntu server running Apache with PHP version 8.2.

First, stop the Apache HTTP service.

				
					sudo systemctl stop apache2
				
			

Now, disable the PHP 8.2 module that is associated with the Pre-fork module.

				
					sudo a2dismod php8.2
				
			

Now, deactivate the Pre-fork MPM module.

				
					sudo a2dismod mpm_prefork
				
			

At this point, you can now activate the MPM Event module.

				
					sudo a2enmod mpm_event
				
			

Configuring Apache HTTP to use FastCGI Process Manager

At this stage, you have now changed the way Apache HTTP handles connections by transitioning from the original Pre-fork MPM to the new Event MPM. However, in the process, you have deactivated the PHP module that connected Apache HTTP with any PHP program running. In the following step, you will install the PHP-FPM processor, so Apache will be able to process any PHP programs again. Additionally, you will install dependency libraries and enable the necessary modules for them to work together faster than before.

First, install PHP-FPM.

				
					sudo apt install php-fpm
				
			

In order for Apache and PHP to communicate with each other, they need a library that enables this functionality. Therefore, install libapache2-mod-fcgid, which serves as the interface between web programs and is specific to Apache. This communication will take place via a UNIX socket. Install the library using the following command:

				
					sudo apt install libapache2-mod-fcgid
				
			

Now, activate php-fpm using this command:

				
					sudo a2enconf php8.2-fpm
				
			

Now, activate the Apache HTTP proxy module using the command below:

				
					sudo a2enmod proxy
				
			

Also, activate the FastCGI proxy module:

				
					sudo a2enmod proxy_fcgi
				
			

Now you can start Apache

You have everything prepared, so you can start Apache again. However, for safety, perform a configuration test first:

				
					sudo apachectl configtest
				
			

You should receive output similar to this:

				
					Syntax OK
				
			

If everything is fine, restart Apache:

				
					sudo systemctl restart apache2
				
			

Now you have installed the php-fpm module and configured Apache to work with this module. You have also enabled the necessary modules for the FastCGI protocol and started the corresponding services.

Test your configuration

If you want to test whether the configuration changes have been applied, you can perform a few tests. First one checks which Apache module is currently in use. The second command verifies that PHP is using FPM as the handler:

				
					sudo apachectl -M | grep 'mpm'
				
			

You should now receive the following output:

				
					mpm_event_module (shared)
				
			

You can use the same for the proxy module as well.

				
					sudo apachectl -M | grep 'proxy'
				
			

The current output looks like this:

				
					proxy_module (shared)
proxy_fcgi_module (shared)
				
			

The last test you can perform is using the phpinfo function. Create a file called info.php on your running website and insert this small code into it:

				
					<?php phpinfo(); ?>
				
			

Once you access the info.php file through a web browser, it should display a complete listing of PHP settings on your server. The line you will be most interested in is the Server API, where you should see FPM/FastCGI.

How to Configure Apache with MPM Event and PHP-FPM on Ubuntu

Conclusion

Congratulations! You have successfully completed the configuration of Apache with PHP-FPM and the Event module. PHP-FPM will now process PHP code much more efficiently, and you will experience improved resource utilization.

The website is created with care for the included information. I strive to provide high-quality and useful content that helps or inspires others. If you are satisfied with my work and would like to support me, you can do so through simple options.

Byl pro Vás tento článek užitečný?

Klikni na počet hvězd pro hlasování.

Průměrné hodnocení. 3 / 5. Počet hlasování: 2

Zatím nehodnoceno! Buďte první

Jak užitečný vidíte tento článek.

Sledujte mě na sociálních médiích.

Je mi líto, že pro Vás nebyl článek užitečný.

Jak mohu vylepšit článek?

Řekněte mi, jak jej mohu zlepšit.

newsletter

Subscribe to the Newsletter

Stay informed! Join our newsletter subscription and be the first to receive the latest information directly to your email inbox. Follow updates, exclusive events, and inspiring content, all delivered straight to your email.

Odebírat
Upozornit na
guest
0 Komentáře/ů
Vložené zpětné vazby.
Zobrazit všechny komentáře.

Pokud mi chcete napsat rychlou zprávu, využije, prosím, níže uvedený
kontaktní formulář. Děkuji.

Další Kontaktní údaje