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

18,595

Solution 1

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.

Solution 2

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).

Share:
18,595

Related videos on Youtube

rahul286
Author by

rahul286

A geek (wordpress & nginx), blogger (tech) & entrepreneur (rtCamp) Currently leading EasyEngine Project - a CLI tool to manage WordPress-Nginx sites & more!

Updated on April 15, 2022

Comments

  • rahul286
    rahul286 about 2 years

    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.

  • rahul286
    rahul286 over 14 years
    @yhager Thanks for ur detailed reply. I also moved our high-traffic blog network to nginx+fastcgi+wp-supercache. Is there anyway to add APC to nginx+fastcgi+wp-supercache? Also will it enhance performance any further?
  • yhager
    yhager over 14 years
    just install APC and restart PHP. And, YES, it will enhance performance quite a bit
  • rahul286
    rahul286 over 12 years
    sorry to say but only in exceptional circumstances, mod_php will perform better than fastcgi/fcgi
  • doublejosh
    doublejosh over 12 years
    In my circumstance fastcgi was almost twice as fast (1.6x). From what I could tell it's because of my static files. Let me know if that's an incorrect assumption.
  • rahul286
    rahul286 over 12 years
    fastcgi is faster because of architectural differences. Apache+mod_php loads webserver+php-parser for all files. When u keep Apace and fastcgi separate, you load fastcgi only for PHP files and for rest of the files you run Apache without mod_php (mod_php is a very big module itself). Apart from this fastcgi has many other advantages also
  • thomasrutter
    thomasrutter over 11 years
    The security issue isn't that root owns files. Actually, having the Apache process running as an unprivileged user (as the default) is a good thing in a sense because if the account that Apache runs as is compromised, it cannot modify random PHP files. And then you can have either root, or any other user, own the files. The downside to mod_php is that you cannot keep separate user's files hidden from each other, as they all have to be readable by an unprivileged process. If you don't need to hide each users' website files from other users, mod_php and default settings is fine.
  • doublejosh
    doublejosh about 11 years
    Unless you're serving static files... then it's a terrible idea. Two issues.