Theoretically, MaxClients = (Total Memory - Operating System Memory - MySQL memory) / Size Per Apache process.
Assume we have a server with 2GB memory
Total memory: is the available physical RAM in the server. In this case 2GB.
Operating System Memory: Stop Apache and MySQL in the server to calculate the actual memory used by OS (rough figure) using the below command:
ps aux | awk '{sum1 +=$4}; END {print sum1}'
In our test VPS:
root@whr [~]# ps aux | awk '{sum1 +=$4}; END {print sum1}'
Then restart Apache and MySQL and wait for a while to get the apt results for Apache and MySQL memory utilization.
Now we will see steps to calculate the MySQL memory utilization:
MySQL memory:
You will come across a lot of calculations for MySQL usage, but the one I found somewhere near was:
mysql_memory= (key_buffer_size + query_cache_size + innodb_buffer_pool_size + innodb_additional_mem_pool_size + innodb_log_buffer_size + max_connections + tmp_table_size) x (sort_buffer_size + read_buffer_size + join_buffer_size + read_rnd_buffer_size + thread_stack + binlog_cache_size)
In test VPS the default values were:
64+64+8+1+1+150+32 * 2+.128+.256+.128+.196+0 = 866.56MB used by MySQL
Size per Apache process: Normally between 20-25MB if you run dynamic websites.
MaxClients is, 2048 - (37.2+ 866.56)/20 ~= 57.
If you increase this value, you will see your server swapping often, so either maintain this value for MaxClients or increase the physical memory (RAM).