开发者

How to configure MySQL/PHP to use less memory?

开发者 https://www.devze.com 2023-03-08 20:47 出处:网络
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开发者_JS百科 mem? I already set the MySQL cache/childs but that isn\'t

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开发者_JS百科 mem? I already set the MySQL cache/childs but that isn't helping much.


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.


Make sure you turn off InnoDB in mysql, that will use about 100MB less memory


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消