Open main menu

Difference between revisions of "Apache/performance"

(initial draft)
 
 
(2 intermediate revisions by one other user not shown)
Line 3: Line 3:
 
=== Cli ===
 
=== Cli ===
 
<source lang='bash'>
 
<source lang='bash'>
# Using wget isn't advised because you'll get html output  
+
# Using wget isn't advised because you'll get html output.  However, if you simply append the ?auto querystring, you'll get machine readable output.
 
wget http://localhost/server-status -O -
 
wget http://localhost/server-status -O -
 +
curl localhost/server-status?auto
 
# Ubuntu
 
# Ubuntu
 
apache2ctl fullstatus
 
apache2ctl fullstatus
Line 13: Line 14:
 
Or you can edit <code>/mods.enabled/status</code> to get it from the browser. For a machine readable, condensed version, just append the '''auto''' querystring argument:
 
Or you can edit <code>/mods.enabled/status</code> to get it from the browser. For a machine readable, condensed version, just append the '''auto''' querystring argument:
 
  e.g. http://example.com/server-status?auto
 
  e.g. http://example.com/server-status?auto
 +
 +
== Killing Stuck Processes ==
 +
[http://giantdorks.org/alain/script-to-individually-terminate-stuck-web-server-threads-without-restarting-apache/ This script from Alain Kelder] shows how you can identify and kill long-running processes without restarting Apache.  I ran into this problem with a misbehaving Mantis plugin.
 +
<source lang="bash">
 +
#/bin/bash
 +
 +
GetAllWorkers()
 +
{
 +
AllWorkers=$(apache2ctl fullstatus | awk '/^Srv /,/^$/ {print}')
 +
}
 +
 +
GetStuckWorkers()
 +
{
 +
StuckWorkers=$(echo "$AllWorkers" | awk '$4 == "W" && $6 > 60 && $7 == 0 && $8 == "0.0" {print}')
 +
header=$(echo "$AllWorkers" | head -n 1)
 +
}
 +
 +
GetStuckPIDs()
 +
{
 +
StuckPIDs=$(echo "$AllWorkers" | awk '$4 == "W" && $6 > 60 && $7 == 0 && $8 == "0.0" {print$2}')
 +
}
 +
 +
Show()
 +
{
 +
echo "--------------------------------"
 +
echo " Stopped on $(date +%F\ %T)"
 +
echo "--------------------------------"
 +
echo "$header"
 +
echo "$StuckWorkers"
 +
}
 +
 +
GetAllWorkers && GetStuckPIDs
 +
if [ -n "$StuckPIDs" ]; then
 +
  for PID in $StuckPIDs; do
 +
    echo stopping $PID with SIGTERM
 +
    kill $PID
 +
  done
 +
  GetStuckWorkers
 +
  Show | mail -s "$(basename $0) executed on $(hostname -s)" root
 +
  Show >> /path/to/ksaw.log
 +
fi
 +
</source>
  
 
== Resources ==
 
== Resources ==
 
# https://httpd.apache.org/docs/2.4/misc/perf-tuning.html
 
# https://httpd.apache.org/docs/2.4/misc/perf-tuning.html
 
# https://www.linode.com/docs/websites/apache-tips-and-tricks/tuning-your-apache-server
 
# https://www.linode.com/docs/websites/apache-tips-and-tricks/tuning-your-apache-server
 +
 +
[[Category:Apache]]
 +
[[Category:Webserver]]
 +
[[Category:Bash]]
 +
[[Category:System Administration]]
 +
[[Category:Performance]]

Latest revision as of 10:01, 2 May 2024

Get Stats

To get statistics, you'll need to potentially modify your Apache configuration. For Ubuntu, it's as easy as a2enmod status (and reload the server with service apache2 reload)

Cli

# Using wget isn't advised because you'll get html output.  However, if you simply append the ?auto querystring, you'll get machine readable output.
wget http://localhost/server-status -O -
curl localhost/server-status?auto
# Ubuntu
apache2ctl fullstatus
# Older/RedHat
apachectl fullstatus

Or you can edit /mods.enabled/status to get it from the browser. For a machine readable, condensed version, just append the auto querystring argument:

e.g. http://example.com/server-status?auto

Killing Stuck Processes

This script from Alain Kelder shows how you can identify and kill long-running processes without restarting Apache. I ran into this problem with a misbehaving Mantis plugin.

#/bin/bash

GetAllWorkers()
{
 AllWorkers=$(apache2ctl fullstatus | awk '/^Srv /,/^$/ {print}')
}

GetStuckWorkers()
{
 StuckWorkers=$(echo "$AllWorkers" | awk '$4 == "W" && $6 > 60 && $7 == 0 && $8 == "0.0" {print}')
 header=$(echo "$AllWorkers" | head -n 1)
}

GetStuckPIDs()
{
 StuckPIDs=$(echo "$AllWorkers" | awk '$4 == "W" && $6 > 60 && $7 == 0 && $8 == "0.0" {print$2}')
}

Show()
{
 echo "--------------------------------"
 echo " Stopped on $(date +%F\ %T)"
 echo "--------------------------------"
 echo "$header"
 echo "$StuckWorkers"
}

GetAllWorkers && GetStuckPIDs
if [ -n "$StuckPIDs" ]; then
  for PID in $StuckPIDs; do
    echo stopping $PID with SIGTERM
    kill $PID
  done
  GetStuckWorkers
  Show | mail -s "$(basename $0) executed on $(hostname -s)" root
  Show >> /path/to/ksaw.log
fi

Resources

  1. https://httpd.apache.org/docs/2.4/misc/perf-tuning.html
  2. https://www.linode.com/docs/websites/apache-tips-and-tricks/tuning-your-apache-server