Performance Optimisation in Apache and Queue implementation | Lucideus Research

The addition of the Multi Process Modules(MPM) to Apache have increased its power and flexibility. The MPMs allow users to change the core behavior of apache.To handle large number of simultaneous web request, MPM provides multithreaded approach where each request will be handled by a thread instead of forking an apache process. Apache has the lowest resource requirements when used with the Event MPM.

To use Apache Event MPM with PHP we need following

mod_proxy_fcgi – An Apache module that allows for passing off operations to a FastCGI processor (PHP can be interpreted this way via PHP-FPM).


PHP-FPM – Stands for PHP FastCGI Process Manager, and is exactly what it sounds like: a manager for PHP that’s being interpreted via FastCGI. Unlike mod_php, this means that PHP is being interpreted by FastCGI, and not directly within the Apache application.

Steps Required to configure Event MPM and PHP-FPM

  1. Changes required in apache configuration (httpd.conf or apache2.conf) to send the web request to PHP-FPM is as follows:

ProxyPassMatch ^/(.*\.php(/.*)?)$ unix:/run/php/php7.0-fpm.sock|fcgi://127.0.0.1:9000/var/www/html/

  1. PHP-FPM can listen on unix or tcp socket which is configurable through php-fpm configuration (/etc/php/7.0/fpm/pool.d/www.conf)

;listen = 127.0.0.1:9000
listen = /run/php/php7.0-fpm.sock

To run the php-fpm on different server tcp socket can be configured.

Important parameters to be configured for php-fpm in above configuration are
  • pm.max_children = 2000 (This value sets the limit on the number of simultaneous requests that will be served)
  • pm.start_servers = 50 (The number of child processes created on startup)
  1. Restart php-fpm (service php-fpm restart) and apache (service apache2 restart)

  1. Test your settings
  1. Run Apache Bench tests and see how your server behaves in top.

  1. Open 2 terminals and the following command to test 5000 requests with a concurrency of 100 parallel requests.
ab -n 5000 -c 100  [URL-To-Test]


Queue (RabbitMQ)

By configuring above the problem is shifted to PHP-FPM from apache, now php-fpm is forking php process for each web request, so with 1000's simultaneous requests php will become the bottleneck if php process has to do lot of work or waiting for shared resource like mysql.

To solve this problem we have added queue(RabbitMQ) so that web requests are queued and PHP process invocations are controlled through Consumer processes.

Conclusion

Using this approach we can allow web server to handle large number of simultaneous requests with limited resources. To enhance it further we will be adding caching mechanism so that frequently used MySql queries will return the result from cache instead of disc to further enhance the overall performance of the system.

References