views:

40

answers:

2

Hi all, 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?

+1  A: 

It's read when the PHP module is loaded, both for mod_php and for FastCGI. In usual CGI usage 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 is easier. You get a lot better performance when you run it in FastCGI mode and can use a threaded (instead of forked) Apache, or when you can throw out Apache altogether.

Emil Vikström
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
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
@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.
Meher
@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
@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
+1  A: 

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.

Meher
Please read the comment I just posted to the other answer. See If you could get that one!
Karthick