HostOnNet Blog

Apache Optimization

Looking for Linux Server Admin or WordPress Expert? We can help.

apache

The default Apache settings that cPanel sets upon install are definitely something that can be improved on. With a few small tweaks, the efficiency with which Apache runs with can be greatly improved.

Check Apache Mode

root@serverxx [~]# httpd -l
Compiled in modules:
  core.c
  mod_authn_file.c
  mod_authn_default.c
  mod_authz_host.c
  mod_authz_groupfile.c
  mod_authz_user.c
  mod_authz_default.c
  mod_auth_basic.c
  mod_include.c
  mod_filter.c
  mod_deflate.c
  mod_log_config.c
  mod_logio.c
  mod_env.c
  mod_expires.c
  mod_headers.c
  mod_unique_id.c
  mod_setenvif.c
  mod_version.c
  mod_proxy.c
  mod_proxy_connect.c
  mod_proxy_ftp.c
  mod_proxy_http.c
  mod_proxy_scgi.c
  mod_proxy_ajp.c
  mod_proxy_balancer.c
  mod_ssl.c
  prefork.c
  http_core.c
  mod_mime.c
  mod_dav.c
  mod_status.c
  mod_autoindex.c
  mod_asis.c
  mod_info.c
  mod_suexec.c
  mod_cgi.c
  mod_dav_fs.c
  mod_negotiation.c
  mod_dir.c
  mod_actions.c
  mod_userdir.c
  mod_alias.c
  mod_rewrite.c
  mod_so.c
root@serverxx [~]#

prefork.c => Apache running as prefork.

Check Memory Usage By Apache

We can check Memory Usage by following command:

root@serverxx [~]# ps -ylC httpd | awk '{x += $8;y += 1} END {print "Apache Memory Usage (MB): "x/1024; print "Average Proccess Size (MB): "x((y-1)*1024)}'
Apache Memory Usage (MB): 636.324
Average Proccess Size (MB): 23.5676
root@serverxx [~]#

You can find number of Apache processes currently running by following command.

root@serverxx [~]# ps auwx |grep httpd |wc -l
27
root@serverxx [~]# 

Apache configuration

httpd.conf is a file file, usually located at /etc/httpd/conf/httpd.conf path containing configuration settings for apache server.

Apache MPM Prefork Module

This module controls the number of processes and spare processes Apache will start and run.

Find these lines in your httpd.conf file:

# 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
# ServerLimit: maximum value for MaxClients for the lifetime of the server
# MaxClients: maximum number of server processes allowed to start
# MaxRequestsPerChild: maximum number of requests a server process serves
<IfModule prefork.c>
StartServers       8
MinSpareServers    5
MaxSpareServers   20
ServerLimit      255
MaxClients       255
MaxRequestsPerChild  4000
</IfModule>

ServerLimit = MaxClients, that you need to set.

If you have 8 GB RAM on server, lets say we give Apache 4 GB RAM, then

MaxClients = (4 * 1024) / Average Apache Proccess Size.

Optimize Your KeepAlive

If you have a site with lots of images and javascripts it is usually better leave KeepAlive turned on and make some additional tweaks.

Timeout 300
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 15
MinSpareServers 5
MaxSpareServers 10
StartServers 8
MaxClients 255
MaxRequestsPerChild 0

KeepAliveTimeout 20

Also you can set "MaxKeepAliveRequests" and "Timeout" values. Reduced Timeout value prevent DOS attack.

About Annie

I've been working in Technical Section for over 10 years in a wide range of tech jobs from Tech Support to Software Testing. I started writing blog for my future reference and useful for all.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.