There is 100% nothing wrong with what you are seeing. PHP is a module being run by Apache which is controlled by the user www-data
. What you are seeing is normal behavior. And the bulk of adjustments to me made to improve performance are connected to Apache configs & not so much PHP settings.
If you want to better control this behavior, there are two places to make adjustments.
1. Adjust the PHP Memory Limit: Open up your php.ini file which should be located in /etc/php5/apache2/php.ini
and edit the option
called memory_limit
. It should be set to 64M or 32M. But that is
basically the MAX limit for memory any PHP process—which is tied to
each Apache2 process (see below)—can use. So if you run WordPress &
it wants to run any process with 32M RAM, no problem. If it needs
more the script will fail & your Apache2 error log located at
/var/log/apache2/error.log
will fill with errors.
2. Adjust Your Apache Config to Suit Your Needs: Since PHP is a module running in Apache, the more Apache processes you have running the more memory will be eaten up. If you have a standard Apache2 install open up this file & look around: /etc/apache2/apache2.conf
. Specifically here are the settings I always adjust in a standard install.
- Timeout: This is the timeout between a request being made by the client to the server & the server fulfilling that request. In my experience the setting of 300 seconds (5 minutes) is too high. I adjust this to 120.
- KeepAlive: I always keep this on as it helps Apache use memory & resources better. It basically means an Apache child process won't
kill itself if a client is still requesting content. That way one
process can flow data & then safely die after it has done it's job.
- MaxKeepAliveRequests: This is directly connected to
KeepAlive
. And I adjust this based on server needs. For development
environments I keep this low. Let's say 3 or 4. For production
environments I tend to feel KeepAlive
is only useful for about 1/3
of connections. So I set this to about 24 or 32. But it memory is a
concern, I would keep this low as the more KeepAlive
processes you
have running the more clients you have running & the more RAM you
have used per process.
KeepAliveTimeout: This is basically a measurement of how fast one can load a page on your site. Think about it: Why keep a connection
alive past the time it takes for a client to get the content for one
page? In my experience a setting of 2 seconds to 5 seconds is good.
But when it veers up to 5 seconds to render a page, it means that
something is really choking the underlying system. So to me that is
a sign to re-evaluate code & clean up resource intensive coding.
Next go to the mpm_prefork_module
area. This is how I setup my development environment; this is for a low traffic site.
<IfModule mpm_prefork_module>
StartServers 8
MinSpareServers 8
MaxSpareServers 8
ServerLimit 16
MaxClients 16
MaxRequestsPerChild 2000
</IfModule>
Basically this sets all the limits & parameters for a basic Apache install. I believe each parameter should be self explanatory, but here is my breakdown.
- StartServers: How many child servers startup immediately under Apache.
- MinSpareServers: The minimum amount of child servers to keep in reserve.
- MaxSpareServers: The maximum amount of child servers to keep in reserve.
- ServerLimit: The top limit on the how many child servers can exist.
- MaxClients: The maximum amount clients that can exist at any given moment.
- MaxRequestsPerChild: This setting helps kill of child processes after a certain number of requests. This helps prevent memory leaks because technically child processes can run on forever until the next Apache2 restart. A safeguard I like to have in place so I can sleep at night even when a developer deploys sloppy code.
Hope this helps!