Knowledgebase & Downloads

Optimizing Apache/PHP/MySQL for low memory use

Optimizing a server for low memory use

Below are a few tips on how to optimize a server to conserve as much memory as possible.

[edit]System-wide resources

  • Run services under inetd/xinetd - Services such as SSH, SMTP servers, FTP servers, and almost any other service can be spawned by inetd/xinetd. In this case they only use memory when they are actually being used.
  • Use ash or dash instead of bash. Be cautious! These alternative shells can break some scripts. Test all your scripts with the new shell before starting using it instead of bash.


  • Tune MySQL to use less memory for cache. The best is to use the my-small.cnf sample config as /etc/my.cnf. Below is an example of what to put in /etc/my.cnf.
port = 3306
socket = /var/lib/mysql/mysql.sock
set-variable = key_buffer=16K
set-variable = max_allowed_packet=1M
set-variable = thread_stack=64K
set-variable = table_cache=4
set-variable = sort_buffer=64K
set-variable = net_buffer_length=2K
  • MySQL uses 10MBs of RAM for InnoDB tables. Remove support for InnoDB tables if you do not use them. To remove support for InnoDB, put the following lines in /etc/my.cnf


  • Tune Apache to only have a small number of spare children running. An example of the Apache configuration section:
StartServers 1
MinSpareServers 1
MaxSpareServers 5
ServerLimit 64
MaxClients 64
MaxRequestsPerChild 4000

Also, only load the modules you require. If you do do not use PHP, mod_perl, etc then do not install them.

  • Install a PHP Cache such as Alternative PHP Cache. The PHP cache will store compiled PHP scripts so that they can be reused without the overhead of compiling and processing them for each request.