mod_php vs cgi vs fast-cgi
Asked Answered
R

3

55

I have been trying to understand the exact meaning/purpose of loading php as an apache module vs the rest.

When php is installed as an apache module, what exactly happens? For example, does reading the php-ini file happen every time the php request comes or when the php module is loaded alone?

Restrain answered 17/10, 2010 at 15:10 Comment(0)
O
25

php.ini is read when the PHP module is loaded in both mod_php, FastCGI and FPM. In regular CGI mode, the config file have to be read at runtime because there's no preforked processes of any kind.

I think the only real advantage of running PHP as a module inside the web server is that the configuration might be easier. You get a lot better performance when you run it in FastCGI or FPM mode and can use a threaded or evented (instead of forked) Apache, or when you can throw out Apache altogether.

Odericus answered 17/10, 2010 at 15:22 Comment(9)
So to be more specific, Say If I have a variable that could be used by all the requests. In other words, lets assume we have to make php interpreter to look for files relative to cerstain path (/home/user/documents). Will I be able to store /home/user/documents in a configuration and load it once into some global variable? This way if user issues file_get_contents("new.txt"), it gets translated to file_get_contents("/home/user/documents/new.txt")Restrain
Check out the PHP setting auto_prepend_file, which will give you the opportinuty to prepend PHP code to be run before the actual request is parsed. inside your auto_prepended file, use chdir() to change directory. Note though that this will intercept ALL file operations, even include() and require()!Valleau
@Karthick: Why would the PHP interpreter store such information? That sort of information is for your code to bother about. When you do not give an absolute path to a file, it looks relative to the script that is being executed by the PHP interpreter. And no, different PHP interpreter instances cannot and must not share data between themselves.Motif
@Meher.. So if any such data is to be shared across multiple instances, then only way is to put them in a configuration file and read them for every request rite?Restrain
@Restrain - Shared data have to be read by every request, but there are alternatives to a configuration file, such as database, APC, memcached, or redisCataldo
"You get a lot better performance when you run it in FastCGI or FPM mode" - Why do you say that?Lylelyles
@vladkornea because you can run a threaded or evented Apache instead of using prefork. PHP is not thread-safe. You also get the best performance when your CPUs are working close to 100% and not more. Prefork Apache have hundreds of processes, one per connection, all fighting for the CPU with little coordination. You therefore lose time due to context switches and similar inefficiencies. In an event-based system you have maybe one or two Apache workers that can handle thousands of connections. Then you have 1 application server (one FastCGI process, for example) per CPU.Valleau
You can do your own benchmarks if you don't trust me. If you ask me, then mod_php is not trading memory for speed. They trade memory for ease of implementation. If I look at your website Kornea.com I can see that you only offer 5 second Keep-Alive to your visitors. So for almost every page they visit they need to set up a new TCP connection. I suspect the reason is that you run Apache prefork and can't handle more than a few hundred active connections at a time. I always deploy Nginx which is event-driven instead of forked and normally offer 20 minutes Keep-Alive.Valleau
More processes always means more overhead. Context switching is expensive. CPU cache misses are expensive. All kinds of lock contention. There is a middle-road to get down the number of Apache processes: putting Nginx as a reverse proxy in front of a preforked Apache usually gives you most of the benefits. We use this for a server that needs Apache.Valleau
D
14

This link may help: http://2bits.com/articles/apache-fcgid-acceptable-performance-and-better-resource-utilization.html

Conclusion

If pure speed is what you are after, then stay with mod_php.

However, for better resource usage and efficiency, consider moving to fcgid.

Dormer answered 14/9, 2011 at 14:29 Comment(1)
The quoted article now suggests using mod_php as of May of 2013. I would also suggest using mod_php, especially if running a PHP-heavy website with static content on a cdn (which you should be doing). RAM is cheap in 2019, cdn services can be even free, use mod_php instead of fcgid.Dehlia
M
2

php.ini is read when the module is loaded in the case of an Apache module. PHP CGI uses a php interpreter executable like any other shell script would do. Since there is no state involved at each invocation, the config file would have to be read every single time in case of CGI.

Motif answered 17/10, 2010 at 15:25 Comment(1)
Please read the comment I just posted to the other answer. See If you could get that one!Restrain

© 2022 - 2024 — McMap. All rights reserved.