Apache and Kernel Config

The following relates to running an Apache Web Server on Ubuntu Linux using MPM Prefork. The below assume a limit of 350 request workers. To monitor the currrent number of request workers use:
watch -n1 "ps -ylC apache2 | wc -l"

MPM Prefork Configuration

The MPM prefork configuration is located in the following file. Below is shown the default configuration from a Ubuntu 20.04 LTS VM.
cat /etc/apache2/mods-enabled/mpm_prefork.conf 
# prefork MPM
# StartServers: number of server processes to start
# MinSpareServers: minimum number of server processes which are kept spare
# MaxSpareServers: maximum number of server processes which are kept spare
# MaxRequestWorkers: maximum number of server processes allowed to start
# MaxConnectionsPerChild: maximum number of requests a server process serves

{IfModule mpm_prefork_module}
    StartServers               5
    MinSpareServers            5
    MaxSpareServers           10
    MaxRequestWorkers        150
    MaxConnectionsPerChild     0
{/IfModule}

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
The following numbers were recommened by ChatGPT for a 4 vCPU 16 GB VM:
$ sudo nano /etc/apache2/mods-enabled/mpm_prefork.conf
...
{IfModule mpm_prefork_module}
    ServerLimit              350

    StartServers              15
    MinSpareServers           20
    MaxSpareServers           40
    MaxRequestWorkers        350
    MaxConnectionsPerChild  5000
{/IfModule}
...

Ulimit

'ulimit' limits the number of a files/sockets a process can open. Assuming your Apache process is running as the user 'www-data', you can find the number of current open files using:
sudo lsof -u www-data | wc -l
You can check the current limit by running the below:
systemctl show apache2 | grep LimitNOFILE
To permanently change the ulimit associated with Apache run:
sudo systemctl edit apache2
Then add the following near the top of file. Note the comment that says any changes below that line will be discarded.
[Service]
LimitNOFILE=65536
Then reload related services:
sudo systemctl daemon-reexec
sudo systemctl restart apache2

Kernel Configuration

It is necessary to increase the following configuration items. 'sysctl' values can be retrieved or set using the 'sysctl' command; and values can be permanently set by editing the '/etc/sysctl.conf' file. To see current values: net.core.somaxconn net.ipv4.tcp_max_syn_backlog net.core.netdev_max_backlog
sysctl net.core.somaxconn
sysctl net.ipv4.tcp_max_syn_backlog
sysctl net.core.netdev_max_backlog
sysctl net.ipv4.ip_local_port_range
sysctl net.ipv4.tcp_fin_timeout
sysctl net.ipv4.tcp_tw_reuse
sysctl fs.file-max
To permanently change values, edit the file '/etc/sysctl.conf':
sudo nano /etc/sysctl.conf
By default, this value should only contain comments. Set the following near the top of the file.
net.core.somaxconn           = 4096
net.ipv4.tcp_max_syn_backlog = 4096
net.core.netdev_max_backlog  = 4096
net.ipv4.ip_local_port_range = 10240 65535
net.ipv4.tcp_fin_timeout     = 15
net.ipv4.tcp_tw_reuse        = 1
fs.file-max                  = 9223372036854775807
To apply the new values:
sudo sysctl -p