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.

I've got something strange on my Apache2 server that is under Ubuntu Linux.

I've got two websites, one is directly handled by Apache2 through Passenger module (it's a ruby webapp), the other one is reverse proxied to another port to a Tomcat6 server.

Everything seems fine but Apache2 behaviour seems really greedy: with top I see that it has at least 5-6 processes and they grow up to 13-14 when I make some requests for just these two servers.

Is it correct to have so many processes?

Should I configure it to be less greedy if there's no need? I was making comparisons with a similar configuration on a less powerful machine (512mb RAM vs 2gb RAM) and it seems that on this machine it keeps less processes open. Maybe Apache2 benchmarks the machine to understand how many resources to allocate?

Just for info both machines are actually virtualized under VMWare server

Thanks in advance

share|improve this question

1 Answer 1

up vote 3 down vote accepted

You should read through the Apache Documentation dealing with Multi-Processing Modules (MPMs). In a nutshell, this determines how Apache will handle multiple requests, and there's two main ways to do that: preforking (which is what it sounds like you're doing now) and threaded worker, which is often more efficient.

Prefork, as you said, creates and maintains a number of "spare" apache processes, and each process handles one client. When you get low on processes, Apache creates more. This can be slow and consume a lot of memory. How many processes are created can be tuned using the StartServers, MinSpareServers, and MaxSpareServers configuration directives.

Worker uses a single or a small number of processes, but creates multiple threads inside each process to handle each request. This can be much more efficient, but is not compatible with certain Apache modules which are not thread-safe (most notably PHP). So if you're using such modules, you'll want to consider switching to FastCGI.

From the Apache documentation:

Apache MPM prefork

This Multi-Processing Module (MPM) implements a non-threaded, pre-forking web server that handles requests in a manner similar to Apache 1.3. It is appropriate for sites that need to avoid threading for compatibility with non-thread-safe libraries. It is also the best MPM for isolating each request, so that a problem with a single request will not affect any other.

[...]

A single control process is responsible for launching child processes which listen for connections and serve them when they arrive. Apache always tries to maintain several spare or idle server processes, which stand ready to serve incoming requests. In this way, clients do not need to wait for a new child processes to be forked before their requests can be served.

The StartServers, MinSpareServers, MaxSpareServers, and MaxClients regulate how the parent process creates children to serve requests. In general, Apache is very self-regulating, so most sites do not need to adjust these directives from their default values. Sites which need to serve more than 256 simultaneous requests may need to increase MaxClients, while sites with limited memory may need to decrease MaxClients to keep the server from thrashing (swapping memory to disk and back). More information about tuning process creation is provided in the performance hints documentation.

And:

Apache MPM worker

This Multi-Processing Module (MPM) implements a hybrid multi-process multi-threaded server. By using threads to serve requests, it is able to serve a large number of requests with less system resources than a process-based server. Yet it retains much of the stability of a process-based server by keeping multiple processes available, each with many threads.

[...]

A single control process (the parent) is responsible for launching child processes. Each child process creates a fixed number of server threads as specified in the ThreadsPerChild directive, as well as a listener thread which listens for connections and passes them to a server thread for processing when they arrive.

I switched from mod_php and mpm_prefork to FastCGI PHP and mpm_worker, and found a very large increase in performance, especially for static files (images, HTML files, etc). Your mileage may vary...

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.