Apache's mod_php OR FastCGI? Which is good for Wordpress?


Question

I have basic idea about running PHP in different configurations like mod_php, cgi, FastCGI, etc.

In my findings and test I found FastCGI is slightly better. I like FastCGI's support for SuEXEC most. Wait I do not want to get into benchmarking business here again. If you surf web, you will find people proving one way is faster than another in terms of number of requests handled per second. Well its good metrics but I am interested in different factors and here are my questions...

  1. Which method of running PHP consumes less memory?
  2. Also which method consumes memory nearly constant. I see with mod_php my servers memory usage fluctuating between 300MB and 800MB, every few seconds.
  3. But with FastCGI, first response from server comes very late. I see with FastCGI there is an initial delay per webpage request. Once first response from server arrives, other items like images, css, js loads pretty faster.
  4. Is it OK to run mix of both? I have 5 sites on dedicated server. Is it ok if I run few with mod_php and rest with FastCGI?
  5. I am sure that my server goes down mostly because of improper memory usage by mod_php. I checked all scripts. Is there any way to make sure memory consumption on server remains nearly constant?
  6. Does complexity of .htaccess affects memory usage significantly? If yes, can it be a single reason to make server run out of memory?
  7. Does apache MPM prefork/worker settings affect memory consumption? Do they affect mod_php and FastCGI mode equally?
  8. When I run "top" command, I see apache (httpd) consuming memory around 40MB. There are many instances of httpd running. Also in addition to that FastCGI forks some processes of similar size. What is normal memory size for httpd process?
  9. As I am running Wordpress on all of our sites, which will be good way in that context?
  10. Does FastCGI/SuExec works fine with APC? Do I need to reconfigure APC to work with SuEXEC and FastCGI.

Please note, I am less interested in surviving against DIGG or traffic spikes. I want a way which can make server stable and predictable.

Sorry if I am confusing but I am really in mess. I have 512MB physical RAM, 400MB Swap and my server is running out of memory like crazy. Average memory requirement is around 350MB, it just memory usage spikes makes memory unavailable for few seconds and if few extra hits received in those few second window, apache crashed while mysql and all other fellas keep running fine.

Please help me out guys. I am not gonna buy more RAM or hardware. I am damn sure that problem is in my configuration. Sorry if I sound arrogant or ignorant.

1
18
10/21/2011 3:28:56 PM

Accepted Answer

Which method of running PHP consumes less memory?

I assume that per PHP-processed request they are more or less the same. But if you have mod_php loaded into apache serving images too, then I assume your memory footprint will be higher due to serving static files.

Also which method consumes memory nearly constant. I see with mod_php my servers memory usage fluctuating between 300MB and 800MB, every few seconds.

You can make both pretty constant. If you carefully set MaxClients, MinSpareServers and MaxSpareServers, you pretty much can tell how many processes are running. If you also set memory_limit within your PHP config, you can calculate how much memory you need. You can do the same for fcgi too, since you can decide how many processes are running.

But with FastCGI, first response from server comes very late. I see with FastCGI there is an initial delay per webpage request. Once first response from server arrives, other items like images, css, js loads pretty faster.

This doesn't make sense. I am not sure why it happens in your case.

Is it OK to run mix of both? I have 5 sites on dedicated server. Is it ok if I run few with mod_php and rest with FastCGI?

I guess, but it will both be a nightmare to maintain and will probably be harder to configure for saving memory. Quite the contrary I believe.

I am sure that my server goes down mostly because of improper memory usage by mod_php. I checked all scripts. Is there any way to make sure memory consumption on server remains nearly constant?

Configure memory and processes as I outlined above, and keep monitoring.

Does complexity of .htaccess affects memory usage significantly? If yes, can it be a single reason to make server run out of memory?

I don't think so. per-directory .htaccess can slow things down, but unless there is some serious bug in Apache, it should not cause mass memory consumption.

Does apache MPM prefork/worker settings affect memory consumption? Do they affect mod_php and FastCGI mode equally?

It might, but I recommend to stay away from worker, as PHP is mostly not thread safe.

When I run "top" command, I see apache (httpd) consuming memory around 40MB. There are many instances of httpd running. Also in addition to that FastCGI forks some processes of similar size. What is normal memory size for httpd process?

30MB is the min. The upper limit depends on your application (I have seen cases where it was ~1GB)

As I am running Wordpress on all of our sites, which will be good way in that context?

It is probably a matter of taste. I have recently moved away from apache towards nginx+fastcgi. it takes a bit of time to get used to, but it does work well. No problems whatsoever with wordpress (even not with supercache, which is rather involved in terms of web server).

Does FastCGI/SuExec works fine with APC? Do I need to reconfigure APC to work with SuEXEC and FastCGI.

I am not using suExec, but fastcgi works well with APC. No need to configure anything.

19
1/3/2012 9:02:12 PM

Tried switching to mod_php, many claims about it's speed. Didn't realize mod_php burns through extra memory when serving static files. (I didn't care much about the security issue with the root owning files).

Without a CDN setup, fastcgi is your choice for serving PHP apps. Without it, switching from fcgi to mod_php slowed my page generation time way down, from 3895ms to 6264ms (admin pages on shared hosting).


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon