Skip to content

Instantly share code, notes, and snippets.

@markcaudill
Created June 22, 2012 19:09
Show Gist options
  • Save markcaudill/2974563 to your computer and use it in GitHub Desktop.
Save markcaudill/2974563 to your computer and use it in GitHub Desktop.
Determine the best Apache MaxClients parameter.
#!/bin/bash
# Copyright (c) 2012 Mark Caudill <mark@markcaudill.me>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
cat <<ABOUT
Apache processes usually get larger as time goes on so it's best to
run this after Apache has been running for a while or after a load
test (http://tools.pingdom.com/fpt/). This also means that running
this test multiple times in rapid succession will will yield drastically
different results.
ABOUT
# Get amount of free memory.
free_memory_apache_running=$(free -b | head -n2 | tail -n1 | awk '{print $4}')
# Get count of Apache child processes.
num_apache_procs=$(($(pgrep apache | wc -l) - 1))
if [[ $apache_procs -eq -1 ]]; then
num_apache_procs=$(($(pgrep httpd | wc -l) - 1))
fi
if [[ $apache_procs -eq -1 ]]; then
echo 'It seems that Apache may not be running. Unable to find any Apache processes.'
exit 1
fi
# Stop Apache.
/etc/init.d/apache2 stop 2&>/dev/null || \
/etc/init.d/httpd stop 2&>/dev/null || \
{ echo 'Unable to stop Apache. Exiting.' && exit 1; }
# Get amount of free memory.
free_memory_apache_shutdown=$(free -b | head -n2 | tail -n1 | awk '{print $4}')
# Start Apache.
/etc/init.d/apache2 start 2&>/dev/null || \
/etc/init.d/httpd start 2&>/dev/null || \
echo 'Unable to start Apache. Oops.'
# Calculate results.
memory_per_apache_proc=$(( ( $free_memory_apache_shutdown - $free_memory_apache_running ) / $num_apache_procs ))
max_clients=$(( $free_memory_apache_shutdown / $memory_per_apache_proc ))
echo "Memory Per Apache Process: ${memory_per_apache_proc}B"
echo "MaxClients = FreeMemoryApacheShutdown / MemoryPerApacheProcess = $free_memory_apache_shutdown / $memory_per_apache_proc = $max_clients"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment