The other day I was very upset about some extraordinary down times of my unmanaged VPS box at Linode. As it’s unmanaged, support staff at Linode are not responsible for the failures. I contacted them and they told me it’s OOM (Out Of Memory), pointing me to the right documentation to figure out how to get the problem sorted out myself. After a few tweaks and observations for a week, so it seems that I have successfully optimized my VPS server to take on more traffic with less resources such as RAM.

The problem almost always lies in where the user is free to feed stuff to your website or program. Sometimes Convert Hub spikes in memory usage and forces my box to use swap that relies on disk I/O to work. This happens when someone uploads an ultra large picture to be processed or converted. While I may restrict the size of the picture that is allowed to be uploaded, I may also do the following settings to optimize the entire LAMP environment so the other websites enjoy it as well.
Apache 2 Low-Memory Optimization

Use this command to identify the MPM you are using:

apache2 -V | grep ‘MPM’ # for Debian-based systems

httpd -V | grep ‘MPM’ # for Fedora/CentOS systems

Find and change these settings in your Apache 2 configuration file (usually found at /etc/apache2/apache2.conf):

StartServers 1
MinSpareServers 3
MaxSpareServers 6
ServerLimit 24
MaxClients 24
MaxRequestsPerChild 3000

Switch to Lighttpd or Litespeed if possible.

MySQL Low-Memory Optimization

Same as above, find and change these settings of the MySQL configuration file (may be at /etc/mysql/my.cnf) accordingly:

max_connections = 18
thread_cache_size=4M
bulk_insert_buffer_size=128M
query_cache_size=128M
query_cache_limit=48M
query_cache_type=1
key_buffer_size=248M
read_buffer_size=8M
read_rnd_buffer_size=8M
max_allowed_packet=32M
join_buffer_size=4M
innodb_buffer_pool_size=500M
tmp_table_size=8G
max_heap_table_size=8G
table_cache = 275
innodb_thread_concurrency=4
concurrent_insert=2
set-variable=long_query_time=3
log_slow_queries=/var/log/mysql/log-slow-queries.log

PHP Low-Memory Optimization

Find your PHP configuration file (php.ini) and modify the PHP script memory limit to 32M or less (default is 128M):

memory_limit = 32M