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?

hakre
  • 193,403
  • 52
  • 435
  • 836
Karthick
  • 2,844
  • 4
  • 34
  • 55

3 Answers3

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.

Emil Vikström
  • 90,431
  • 16
  • 141
  • 175
  • 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") – Karthick Oct 17 '10 at 15:30
  • 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()! – Emil Vikström Oct 17 '10 at 15:35
  • 1
    @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. – Anand Oct 17 '10 at 15:37
  • @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? – Karthick Oct 17 '10 at 15:41
  • 2
    @Karthick - Shared data have to be read by every request, but there are alternatives to a configuration file, such as database, APC, memcached, or redis – Mark Baker Oct 17 '10 at 21:09
  • "You get a lot better performance when you run it in FastCGI or FPM mode" - Why do you say that? – Val Kornea Oct 21 '17 at 18:19
  • @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. – Emil Vikström Oct 22 '17 at 06:45
  • 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. – Emil Vikström Oct 23 '17 at 10:29
  • 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. – Emil Vikström Oct 23 '17 at 10:34
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.

stormwild
  • 2,855
  • 2
  • 31
  • 38
  • 2
    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. – Tim Eckel May 15 '19 at 03:43
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.

Anand
  • 729
  • 2
  • 10
  • 26