Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Guys, my server was impossibly slow the whole day, taking over 2 minutes to load a page, and miraculously it just got fast again when I restarted apache. Anyone knows why? Should I restart apache on a regular basis using cron?

share|improve this question

5 Answers 5

up vote 4 down vote accepted

That depends entirely on what your apache is running. Is it just static pages? Or do you have dynamic code?

Usually if you are seeing slowdowns after some period of time that are fixed by restarting the process, that's indicative of a memory leak. It's highly unlike (though not impossible) that Apache itself has a major memory leak (it is extremely widely used and well tested..), chances are it is in the underlying application somewhere.

Make a point to watch the processes on your system, preferably use a tool to graph their memory and CPU consumption over time, and find the culprits.

share|improve this answer

It'll be swap, and restarting Apache merely got all of the worker processes out of the swap mire. It'll keep happening, and you'll keep restarting Apache unless you invest learning a system info tool such as sysstat, which keeps track of all your processes and system statistics so you can work out what's consuming all the memory. Once you know what's the hog (like Kamil, I'm betting it's a leaky app inside of Apache) you can perform app-specific diagnostics to determine the exact cause and kill it.

share|improve this answer

Due to some applications I run (or have run) on top of Apache that have had issues, I bounce it once a day (along with MySQL).

That being said, you really shouldn't have to do that - because it's a sign of a broken app. I know many apache servers running on weeks-months of uptime, only being bounced when major updates to the server are performed (ie kernel upgrades, new hardware, etc).

share|improve this answer

You may be able to mitigate the issue by either setting MaxRequestsPerChild to either a non-zero value or lower than it's current value.

As the name suggests, this defines the number of requests that an Apache child process will handle before it exits and starts a new process. Setting it to a lower value can help by periodically turning processes over before they have a chance to suck up too much memory.

Nevertheless you should still investigate the underlying cause.

share|improve this answer

It really helps if you understand just how Linux uses memory. Linux (by default) likes to cache frequently / recently accessed files in memory, it will happily send processes like Apache, MySQL and others to dirty pages (swap) so that it has room to maintain those file buffers.

This may, or may NOT be what you want, depending on your server. If you are running the typical LAMP, you have three memory hogs all competing for contiguous physical memory:

  1. Apache
  2. MySQL
  3. PHP

Its very likely that other things (perhaps ClamAV?) is also needing memory. In a nutshell, the applications that need physical memory aren't getting it.

What you should do is read up on a few sysctl tweaks that adjust how quickly Linux reclaims memory from its page cache to give to applications. "swappiness" and "vfs_cache_pressure" are two good knobs to begin with. I can't tell you (specifically) how to set these without looking at your system.

You also might look at your apache configuration, simply disabling keep-alive requests may prevent so many idle children from living in memory needlessly. Again, this is hard to say, I'm not sure what kind of sites you host. Also, are you using the default MySQL configuration? You might do well to post another question that describes your hardware, what kind of sites you host and what your slow queries look like, people can then suggest good optimizations.

So, in short (and to answer your question), no - it is a very bad idea to just have cron re-start apache on set intervals. Its much better to fix the underlying problem.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.