Loading...

Knowledge Base

How to Optimize Apache and PHP Performance on cPanel Servers

When you want to improve website performance on your server. This article explains how to optimize Apache MPM, PHP handlers, and PHP-FPM settings for better speed and resource management.

Apache Multi-Processing Modules or MPMs change the basic functionality of the webserver. They do this by modifying how Apache listens to the network, accepts, and handles requests.

How to check the MPM currently in use?
[root@162-241-127-95 ~]# httpd -V | grep -i 'version\|mpm'
Server version: Apache/2.4.48 (cPanel)
Server MPM:     prefork

Where to find Apache MPM configuration in the WHM panel?
Go to WHM >> EasyApache 4 >> Click Customize
wse_image1

What Apache MPM & PHP handler configuration is the best?
Fastest Apache MPM: mod_mpm_event
Fastest PHP Handler: FCGI

Installing the Event MPM:
wse_image1
Note that only one MPM can be active at a time. We need to uninstall the old MPM to use a new one. Here, note that mod_mpm_prefork is the slowest Apache MPM one could use.

Installing the FCGI PHP Handler:

wse_image1
Then go to Review >> Provision.

 

After provisioning, set the PHP handler by:

WHM >> MultiPHP Manager >> System Settings >> PHP Handlers >> Select FCGI in the dropdown across all PHP versions

wse_image1
Here, note that suphp is the slowest PHP handler one could use.

When to use PHP-FPM then?

If you have a heavy and high-traffic website with limited server resources, PHP-FPM is the recommended choice as it efficiently manages PHP processes, reducing CPU and memory usage. Using PHP-CGI on a high-traffic site can lead to excessive process spawning, increasing resource consumption and potential server slowdowns.


For basic websites with low traffic, PHP-CGI can be a simple and lightweight option as it does not require advanced process management. Note: PHP-FPM is indeed designed to handle high traffic efficiently, but that doesn't mean it requires a high-resource server. Instead, it optimizes resource usage better than PHP-CGI, making it a good choice even for low-resource servers handling heavy traffic.
 


How to calculate the ideal PHP-FPM settings to use?

Process Idle Timeout: The number of seconds after which an idle process is set to be killed so it releases CPU time and RAM. This will be set to 10 as default. Leave it at 10, we don't want idle processes using server resources for a prolonged period of time.

Max Requests: Controls how many requests are served by a process (Max Children) before killing and spawning a new process. A value of between 50 to 100 works fine here. A lower value will cause the server to kill and respawn PHP-FPM pools very frequently and a higher value could result in memory leaks.

Max Children: The number of PHP-FPM processes to spawn to process PHP code for web server requests. The default is 5 which is fine for low-traffic websites but will cause problems if the website gets moderate to high traffic. That being said, we cannot increase this arbitrarily as it corresponds directly to the memory usage of the server.

How to calculate the maximum number of child processes that can be spawned by PHP-FPM?

Determine how much memory, on average, your Apache processes use. You can get this by reviewing the RSS column from the following command and divide it by 1024 to convert it to MB: 
 

[root@162-241-120-206 ~]# ps -ylC php-fpm --sort:rss
S   UID   PID  PPID  C PRI  NI   RSS    SZ WCHAN  TTY          TIME CMD
S  1003  9418  1264  0  80   0  8824 76902 skb_wa ?        00:00:00 php-fpm
S  1003  9407  1264  0  80   0  8988 76902 skb_wa ?        00:00:00 php-fpm
S     0  1264     1  0  80   0 13032 76902 ep_pol ?        00:00:01 php-fpm


Another option is to get the average of one php-fpm process:

[root@162-241-120-206 ~]# ps --no-headers -o "rss,cmd" -C php-fpm | awk '{ sum+=$1 } END { printf ("%d%s\n", sum/NR/1024,"Mb") }'
9Mb

Take the total amount of memory available to the server and subtract the amount of memory that is required for non-Apache processes. The remaining memory is what you will reserve for Apache.

Current memory status:

[root@162-241-120-206 ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:           7.6G        557M        6.3G        9.4M        824M        6.8G
Swap:          4.0G          0B        4.0G

You need to take into account any other services running on the machine while calculating memory usage.

Take the remaining amount of ram and divide that by the average amount of memory that you expect each Apache process to use. Let's say from step (2) above, you determine that Apache can use 3000M RAM. This brings us to 3000M / 9M = 333 max_children. This is the total number of child processes that can be spawned by PHP-FPM for the entire server. Consider the number of websites on the server and set the global value accordingly. Also, we have an option to set individual PHP-FPM settings for websites in MultiPHP Manager >> User Domains >> Manage Configuration option.

Please note that very high values do not mean necessarily anything good. Kindly take into account the resources required by other services and other Cpanel accounts that you will be creating on the server before setting up a value for max children. All the above calculations were performed based on the instructions mentioned in the previous articles shared in this case thread.