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