Optimizing Apache2 and PHP-FPM for High Performance: The Easy Way

You have high configuration in your server but still your site load slowly? You have question in your mind, How to optimise Apache2 and php-fpm for high performance – Most Easy Technique is here which helps you to increase your server performance.

Optimizing Apache2 and PHP-FPM for High Performance: The Easy Way

I was worked for so many high traffic sites in my career. I manage so many slow site and server performance issue in my past. In this article, I will let you know about apache configuration for high performance.

My environment

  • AWS EC2 (r5ad.large): 4 *2,4GHz | 16GB RAM
  • Ubuntu 20.04
  • Apache 2.4 using mpm_event
  • PHP FPM (5.6, 7.4)

Before start setup in your apache configuration file, you need to know about your server usage and count some memory usage for some process. So, Let’s count that values in 3 steps.

1. Calculate process size

You need to know how many processes can run on your machine. So calculate the process size of your main CPU/memory drivers is necessary.

There are several calculation methods out there. Personally I prefer this python script as it takes shared memory into account and will give insights into real memory usage. Use the following commands one by one for get memory usage of your server.

wget https://raw.githubusercontent.com/pixelb/ps_mem/master/ps_mem.py
chmod a+x ps_mem.py
sudo python ps_mem.py

It will return output like this,

Here you can see, 4 apache process consume 36 MB, So, each process takes 9MB of RAM. Same as php-fpm7.4 has 27 process which consumes around 419 MB, So, each process takes 15MB.

2. Calculate apache MaxRequestWorkers

For safer side we need to count 1 GB memory for other process also. So, we count 2.4 GB. So, Now let’s count MaxRequestWorkers.

MaxRequestWorkers = (Total RAM - Memory used for Linux, DB, etc.) / process size
MaxRequestWorkers = (16384MB - 2400MB) / 9MB = 1554

3. Calculate php-fpm max-children

To be safe though, I’ll reserve 1 GiB for all other processes and round up php process size to 55MiB.

maxclients = (Total RAM - Memory used for Linux, DB, etc.) / process size
maxclients = (16384MB - 2400MB) / 55MB = 256

Let’s start Configuration Now

Open your mpm worker or event configuration file. /etc/apache2/mods-enabled/mpm-event.conf OR /etc/apache2/mods-enabled/mpm-worker.conf file:

<IfModule mpm_*_module>
  ServerLimit           (Total RAM - Memory used for Linux, DB, etc.) / process size
  StartServers          (Number of Cores)
  MinSpareThreads       25
  MaxSpareThreads       75
  ThreadLimit           64
  ThreadsPerChild       25
  MaxRequestWorkers     (Total RAM - Memory used for Linux, DB, etc.) / process size
  MaxConnectionsPerChild   1000
</IfModule>



So, In my case I used:

<IfModule mpm_event_module>
        ServerLimit              1554
        StartServers             4
        MinSpareThreads          25
        MaxSpareThreads          75
        ThreadLimit              64
        ThreadsPerChild          25
        MaxRequestWorkers        1554
        MaxConnectionsPerChild   1000
</IfModule>

Now open /etc/php/7.4/fpm/pool.d/www.conf file for change the following settings:





pm = dynamic            
pm.max_children         (total RAM - (DB etc) / process size)
pm.start_servers        (cpu cores * 4)
pm.min_spare_servers    (cpu cores * 2)
pm.max_spare_servers    (cpu cores * 4)
pm.max_requests         1000


In my case, I used:

; Optimized for php-fpm request size of 55MB on AWS EC2 r5ad.large (4CPU cores, 16GB RAM)

pm = dynamic
pm.max_children = 256
pm.start_servers = 20
pm.min_spare_servers = 10
pm.max_spare_servers = 20
pm.max_requests = 1000

Save your settings and restart your apache and php-fpm processes, use the following commands for that.

sudo service apache2 restart
sudo service php7.4-fpm restart
Now, You can check your site speed, It will load super fast.