How much free memory should I have on my webserver? [duplicate]
The answer is basically: It depends. In your specific case, the quality & efficiency of the sites you have installed can come into question.
For example, this week I was working on a server that is relatively moderate traffic, yet eating up 4GB of RAM. After doing a code review—the whole site is using an off-the-shelf CMS similar to WordPress but with customizations—we discovered a major bottleneck. Once we recoded the issue away, the server now uses about 2GB of RAM on an average day. A 50% drop in RAM usage!
I would not recommend doing something like split the server to use Ngnix as well as Apache. Just use Apache. The headaches of balancing the configs of two different servers is not worth the marginal benefit. Apache is fine. But that said, you should consider your WordPress PHP sites versus Java app versus MySQL in on the same box. In my experience, you should do the following:
-
Properly configure Apache: Apache is a good piece of software, but right out of the box it’s a memory hog. For example, I believe the default is to allow 255 connections per second? I can assure you most simple sites barely get 40 connections per second on a good day. So adjusting Apache to be realistic to your traffic will help. Also, there is a
KeepAlive
setting in Apache that works great! But out of the box, I believe it’s set to aMaxKeepAliveRequests
of 100 which is fairly nuts. I usually set this to about 30 connections with a smallKeepAliveTimeout
of 2 to 3 seconds. The key is to have theKeepAliveTimeout
to match speed it takes for an average page to download with a little bit of room for overhead/slowness. So if a page loads in 1 second, do aKeepAliveTimeout
of 2 seconds. -
Review the code for your WordPress sites for potential bottlenecks: Concentrate on the PHP core of it & clear up what you can. Look out for excessive MySQL calls & file system calls. This is where you will be able to make the app fly! Also, check the
memory_limit
in yourphp.ini
and make sure it’s not higher than necessary. The default is 64M, but in many cases that can be lowered to 32M. - MySQL tuning or moving it onto it’s own server: After writing about MySQL above I realized you might be hosting your MySQL instance on the same box. Look into optimizing MySQL performance by running a script like MySQL tuning primer. Without tuning, MySQL will eat up all resources & big the system down. With tuning, MySQL will run better/faster & resources can be freed for other purposes. Also, consider moving your MySQL DB to a standalone server. You might have to learn how to properly network & firewall the server to allow your servers access but protect against hackers, but the performance benefit will be great.
- Consider moving the Java app to another server: My rule of thumb is one major Java application per server. In general, Java apps can be memory hogs when compared to PHP setups like WordPress. By giving the Java app it’s own server to use, the WordPress sites will be much happier.
Regarding the MySQL tuning, that is something that can take a few weeks to nail down at the beginning. The reason being tuning scripts are based on real traffic MySQL sees. So you basically make your site live to the world, wait 2 days (at least), run the tuning scripts & then wait a few more days to tune some more. After a week or so you should be able to tune MySQL to work as well as it can with your setup.