56

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?

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

9 Comments

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")
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()!
@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.. 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 - Shared data have to be read by every request, but there are alternatives to a configuration file, such as database, APC, memcached, or redis
|
15

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.

1 Comment

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

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.