I need to use the minimal amount possible of RAM on a VM Server running Ubuntu, using LAMP. Is there known tips for using minimal mem? I already set the MySQL cache/childs but that isn't helping much.
- 1I am currently running 5 word press sites and an Icecast server with one stream and an average of 5 users online on a server with only 512mb of ram. We barely see it go above 50% ram except when we get an influx of listeners.Mike Soule– Mike Soule2011-05-26 21:25:42 +00:00Commented May 26, 2011 at 21:25
- 2Reducing mysql's memory usage is usually done by reducing buffer/cache sizes which WILL affect performance. Don't drop things so low that MySQL can't do its job.Marc B– Marc B2011-05-26 21:26:53 +00:00Commented May 26, 2011 at 21:26
4 Answers
What you should do, is retain Linux, Mysql and PHP, but do away with Apache. Or at least, stop running PHP in-process with Apache.
The chances are you're using the Apache prefork model with an in-process PHP module. This is very bad for memory efficiency on most workloads, because it keeps a heavy PHP process open even for HTTP connections which aren't requesting any dynamic content just now.
What you want to do instead is use another web server (for example Nginx, but Apache would work too) and run PHP as a FastCGI daemon. This is easy to set up and googling for "PHP fastcgi" returns numerous examples.
You can then have a small, fixed number of "heavy" processes running PHP (No more than a couple per core, I reckon), but still have good capacity for running real applications, because "idle" HTTP connections, such as those serving keep-alives or waiting for requests don't use up the "heavy" processes, only the lighter web server processes.
A web server which uses limited forking / few processes is probably better - such as Nginx, or Apache with a different thread model. This is incompatible with mod_php, which is why you need to run it as FastCGI instead.
2 Comments
Make sure you turn off InnoDB in mysql, that will use about 100MB less memory
6 Comments
You can set the memory_limit in php.ini to something smaller. However if your program requests more memory than the limit then the script will crash with a fatal error.
That will only change memory usage in PHP. There may be a similar setting for MySQL in my.cnf but I don't know what it would be off-hand.
Really though the best way to reduce memory usage is to write programs that don't use a lot of memory.
Comments
About MySQL, Apache and others: http://library.linode.com/troubleshooting/memory-networking#sph_diagnosing-and-fixing-memory-issues
For PHP: in php.ini you can set memory_limit.
For Linux: use 32-bit versions (until you have more than 4Gb of RAM).
Don't reduce all these settings until you really need it, or it will be reason of performance degradation. Try to give enough memory to your system first.