Making it Better: Basic Apache performance tuning (httpd)

  • Applies to: VPS Hosting
    • Difficulty: Easy
    • Time Needed: 10
    • Tools Required: Account Center access
  • Applies to: Legacy DV & VPS Hosting
    • Difficulty: Easy
    • Time Needed: 10
    • Tools Required: Account Center access


This article will provide some basic Apache (httpd) performance tuning. The objective of this article is to perform simple edits that will allow your VPS server to run with less memory and to ensure higher availability under heavier loads.

Please keep in mind that troubleshooting the configuration/functionality of third-party applications is not covered by our statement of support. These resources were provided as a courtesy to assist you to the extent of our abilities. For more information on our statement of support, feel free to click here.

Advanced Support can help!

If you're having trouble with the steps in this article, additional assistance is available via Advanced Support, our premium services division. For more information on what Advanced Support can do for you, please click here.

Adjust prefork settings

The VPS server is a fairly forgiving environment for Apache. The prefork settings included in httpd.conf follow a standard linear progression with regards to available RAM and will normally produce suitable results for users. However, servers with heavy resource usage may see improved performance by scaling these settings more conservatively. Additionally, the default Apache build includes a number of modules that are most likely not in use. Removing these from the configuration will free up RAM to be available elsewhere on the server.

The information/suggestions in this article are not steps that need to be done in order. Each set of commands will make changes that may or may not improve performance. However, before proceeding, you will want to make a backup of the files that you modify:

cp -p /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.YYYY-MM-DD.bak

The following change simply scales Apache settings against the available RAM that is outlined by server beancounters. As with any change of this nature, there is no way to guarantee with certainty that it will improve server performance.

This command can be used to lessen the amount of resources that Apache can "run away" with at any one time. For busier servers, this allows you to observe its memory levels change, or if other processes immediately start to make use of the memory which is now available. From there, additional troubleshooting would be necessary to determine if those other processes were problematic in their own right or not.

First, a few variable calculations are required based on current server beancounters. The following commands take the available information from /proc/user_beancounters and returns a number. If your VPS has 1G RAM, this number is 2; for 2G RAM, it is 3; so on and so forth:

ramCount=`awk 'match($0,/vmguar/) {print $4}' /proc/user_beancounters`
ramBase=-16 && for ((;ramCount>1;ramBase++)); do ramCount=$((ramCount/2)); done

With this information, adjustments can now be made to the prefork settings in httpd.conf. The following is a single command:

perl -0 -p -i -e 's/(\<IfModule\sprefork\.c\>(\n|[^\n])*?StartServers\s*?)\s\d+/\1\ '"$ramBase"'/;' \
-e 's/(\<IfModule\sprefork\.c\>(\n|[^\n])*?MinSpareServers\s*?)\s\d+/\1\ '"$ramBase"'/;' \
-e 's/(\<IfModule\sprefork\.c\>(\n|[^\n])*?MaxSpareServers\s*?)\s\d+/\1\ '"$(($ramBase*2 + 1))"'/;' \
-e 's/(\<IfModule\sprefork\.c\>(\n|[^\n])*?ServerLimit\s*?)\s\d+/\1\ '"$(( 50 + (($ramBase**2)*10) + (($ramBase-2)*10) ))"'/;' \
-e 's/(\<IfModule\sprefork\.c\>(\n|[^\n])*?MaxClients\s*?)\s\d+/\1\ '"$(( 50 + (($ramBase**2)*10) + (($ramBase-2)*10) ))"'/;' \
-e 's/(\<IfModule\sprefork\.c\>(\n|[^\n])*?MaxRequestsPerChild\s*?)\s\d+/\1\ '"$(( 2048 + ($ramBase*256) ))"'/;' /etc/httpd/conf/httpd.conf

Again, these changes are only suggested settings. Particular users may find better results comparing and contrasting the original backup file with the values that this provides and creating customized settings.

Disabling Unnecessary Apache Modules

These first few commands disable three modules that are loaded into Apache by default with their own configuration files. For users with standard PHP-based applications, these should not be needed, and so they can be prevented from loading into Apache.

mv /etc/httpd/conf.d/perl.conf /etc/httpd/conf.d/perl.conf-disabled
mv /etc/httpd/conf.d/python.conf /etc/httpd/conf.d/python.conf-disabled

The following change removes a number of other modules from Apache. For most users, nothing in this list is needed. However, certain applications or server configurations may require that one or more of these modules be made available.

This command will lessen the resources Apache is claiming each time it needs to spawn a process. Each module loaded requires a slightly bulkier process, which means a higher idle RAM usage. For servers using a large number of Apache processes without any need for these modules, this will free up this RAM for use elsewhere.

The following command is a single command. If you see a module listed that you do not want disabled, simply remove that line from the command before executing it. If you need to re-enable a module afterward, you can manually edit /etc/httpd/conf/httpd.conf and remove the comment (# symbol) in any LoadModule directive that needs to take effect.

perl -0 -p -i \
-e 's/#?(LoadModule\ authn_alias_module\ modules\/mod_authn_alias\.so)/#\1/;' \
-e 's/#?(LoadModule\ authn_anon_module\ modules\/mod_authn_anon\.so)/#\1/;' \
-e 's/#?(LoadModule\ authn_dbm_module\ modules\/mod_authn_dbm\.so)/#\1/;' \
-e 's/#?(LoadModule\ authnz_ldap_module\ modules\/mod_authnz_ldap\.so)/#\1/;' \
-e 's/#?(LoadModule\ authz_dbm_module\ modules\/mod_authz_dbm\.so)/#\1/;' \
-e 's/#?(LoadModule\ authz_owner_module\ modules\/mod_authz_owner\.so)/#\1/;' \
-e 's/#?(LoadModule\ cache_module\ modules\/mod_cache\.so)/#\1/;' \
-e 's/#?(LoadModule\ disk_cache_module\ modules\/mod_disk_cache\.so)/#\1/;' \
-e 's/#?(LoadModule\ ext_filter_module\ modules\/mod_ext_filter\.so)/#\1/;' \
-e 's/#?(LoadModule\ file_cache_module\ modules\/mod_file_cache\.so)/#\1/;' \
-e 's/#?(LoadModule\ info_module\ modules\/mod_info\.so)/#\1/;' \
-e 's/#?(LoadModule\ ldap_module\ modules\/mod_ldap\.so)/#\1/;' \
-e 's/#?(LoadModule\ mem_cache_module\ modules\/mod_mem_cache\.so)/#\1/;' \
-e 's/#?(LoadModule\ status_module\ modules\/mod_status\.so)/#\1/;' \
-e 's/#?(LoadModule\ speling_module\ modules\/mod_speling\.so)/#\1/;' \
-e 's/#?(LoadModule\ usertrack_module\ modules\/mod_usertrack\.so)/#\1/;' -e 's/#?(LoadModule\ version_module\ modules\/mod_version\.so)/#\1/;' /etc/httpd/conf/httpd.conf

Any time you make changes to Apache configurations, you need to restart the server in order for them to take effect:

/etc/init.d/httpd restart

Additional Resources

Please keep in mind that scripted suggestions and automated changes can only go so far. Some level of familiarity with the official Apache documentation can go a long way when addressing web server performance.

Was this article helpful?
0 out of 0 found this helpful