X Marks the Spot

random commentary on life, the universe, and anything

Memory issue with LAMP on CentOS 6


So getting a bit of systems administration housecleaning done. Have had a persistent memory issue on the server (LAMP, CentOS 6) likely due to poorly tuned Apache and mysql. The virtual server has 512MB RAM and I set a cron job to restart httpd twice daily to avoid dealing with the problem.

Apache – prefork vs. worker mode, how to check mode and more

First, I looked at apache.

1. Check what MPM is in use (prefork vs worker)
Prefork MPM
– prefork MPM uses multiple child processes with one thread each.
– Each process handles one connection at a time and uses more memory.
– Good for non-thread-safe third party modules.

Worker MPM
– worker MPM uses multiple child processes with many threads each.
– Each thread handles one connection at a time.
– Good for high-traffic, smaller memory footprint.

$ ./usr/sbin/apachectl -V | grep MPM
Server MPM:     Prefork
 -D APACHE_MPM_DIR="server/mpm/prefork"

2. Edit httpd.conf (/etc/httpd/conf/httpd.conf) so not configured to start too many servers, or have to many spare servers sitting around.

# 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

StartServers       1
MinSpareServers    1
MaxSpareServers    5 
ServerLimit       25
MaxClients        25
MaxRequestsPerChild  3000

Next, mysql.

3. Edit my.cnf (/etc/my.cnf) for a low memory environment.

$ sudo vi /etc/my.cnf
key_buffer = 16K
max_allowed_packet = 1M
table_cache = 4
sort_buffer_size = 64K
read_buffer_size = 256K
read_rnd_buffer_size = 256K
net_buffer_length = 2K
thread_stack = 64K

max_allowed_packet = 16M


key_buffer = 8M
sort_buffer_size = 8M

key_buffer = 8M
sort_buffer_size = 8M



Now to see if it makes a difference.

Leave a Reply

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