What Is Mod_Php

What is mod_php?

mod_php means PHP, as an Apache module.

Basically, when loading mod_php as an Apache module, it allows Apache to interpret PHP files (those are interpreted by mod_php).


EDIT : There are (at least) two ways of running PHP, when working with Apache :

  • Using CGI : a PHP process is launched by Apache, and it is that PHP process that interprets PHP code -- not Apache itself
  • Using PHP as an Apache module (called mod_php) : the PHP interpreter is then kind of "embedded" inside the Apache process : there is no external PHP process -- which means that Apache and PHP can communicate better.


And re-edit, after the comment : using CGI or mod_php is up to you : it's only a matter of configuration of your webserver.

To know which way is currently used on your server, you can check the output of phpinfo() : there should be something indicating whether PHP is running via mod_php (or mod_php5), or via CGI.

You might also want to take a look at the php_sapi_name() function : it returns the type of interface between web server and PHP.


If you check in your Apache's configuration files, when using mod_php, there should be a LoadModule line looking like this :

LoadModule php5_module        modules/libphp5.so

(The file name, on the right, can be different -- on Windows, for example, it should be a .dll)

What are the differences between mod_php and cgi php script?

When using CGI : a PHP process is launched by Apache, and it is that PHP process that interprets PHP code -- not Apache itself.

In theory, a distinct PHP process has to be created for each request -- which makes things slower : Apache has more work to do to answer a request.

(Well, as pointed out by @AlReece45 in a comment, this can be made better using FastCGI)


When using PHP as an Apache module (mod_php, or mod_php5), the PHP interpreter is kind of "embedded" inside the Apache process : there is no external PHP process.

Which means :

  • No forking to answer a request (faster)
  • Better communication between Apache and PHP


Generally speaking, I would say that mod_php is the solution that's used the most.

mod_php and Zend Engine

mod_php is an Apache module. It is one of the many ways of running PHP (the Zend Engine).

You can also run PHP directly on the command line, or another very popular way is using PHP-FPM and Nginx/Apache.

Basically, PHP (the Zend Engine) is something that is often used along side a web server (Apache, Nginx) and mod_php is one of the ways of doing that, but perhaps not the best.

Apache's mod_php OR FastCGI? Which is good for Wordpress?

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.

mod_php vs cgi vs fast-cgi

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.

How do mod_php, mod_python, mod_Language work

Basically, if you install and configure mod_php correctls, a php file inside an apache DirectoryRoot will be executed. Mod_python works similarly.

If you install apache without mod_php and you have foo.php at the root of your htdocs folder then http://yourdomain/foo.php will treat the document as a plain text file. Installing and configuring mod_php will cause the script to be parsed as a php script, and the output to be sent to the browser as opposed to the raw text..

Justin

Apache2 API (or another webserver) for mod_php or PHP

It is not possible for PHP to alter Apache's config dynamically, although as you noted you can use PHP to edit the configuration or .htaccess files reload the configuration. But I would consider that a big security risk to give your webapp that much power over the web server.

Your example of blocking IPs dynamically can be done with mod_rewrite's RewriteMap directive. Basically, you would write a mod_rewrite rule for your website that will instruct mod_rewrite to look in an external data source that your PHP script could maintain.

The mod_rewrite documentation has an example to start from: Denying Hosts in a Blacklist

Description:
We wish to maintain a blacklist of hosts, rather like hosts.deny,
and have those hosts blocked from accessing our server.

Solution:
RewriteEngine on
RewriteMap hosts-deny txt:/path/to/hosts.deny
RewriteCond ${hosts-deny:%{REMOTE_ADDR}|NOT-FOUND} !=NOT-FOUND [OR]
RewriteCond ${hosts-deny:%{REMOTE_HOST}|NOT-FOUND} !=NOT-FOUND
RewriteRule ^ - [F]
##
## hosts.deny
##
## ATTENTION! This is a map, not a list, even when we treat it as such.
## mod_rewrite parses it for key/value pairs, so at least a
## dummy value "-" must be present for each entry.
##

193.102.180.41 -
bsdti1.sdm.de -
192.76.162.40 -

Discussion:
The second RewriteCond assumes that you have HostNameLookups turned on,
so that client IP addresses will be resolved. If that's not the case,
you should drop the second RewriteCond, and drop the [OR] flag from
the first RewriteCond.

Your PHP script could maintain those text files (other data stores are available, too) and you could make any number of rulesets for your specific needs to control access dynamically. Would that work?

mod_php vs mod_python

Let's talk about mod_python vs. mod_php.

Since the Python language is NOT specifically designed for serving web pages, mod_python must do some additional work.

Since the PHP language IS specifically designed to serve web pages, mod_php simply starts a named PHP module.

In the case of mod_python (different from mod_fastcgi or mod_wsgi), the designer of mod_python decided that the best way to invoke Python is to call a method of an object defined in a module. Hopefully, that method of that object will write the headers and web page content to stdout.

In the case of mod_wsgi, the designer decided that the best way to invoke Python is to call a function (or callable object) in a module. Hopefully that function will use the supplied object to create header and return a list strings with the web page content.

In the case of mod_php, they just invoke the module because PHP modules are designed to serve web pages. The program runs and the result of that run is a page of content; usually HTML.

The reason mod_php works one way, mod_python works another and mod_wsgi works a third way is because they're different. Written by different people with different definitions of the way to produce a web page.

The php page can be object-oriented. A mod_wsgi function may be a callable object or it may be a simplef unction. Those are design choices.

The mod_python requirement for a class is a design choice made by the designer of mod_python.

The reason "why" is never useful. The reason "why" is because someone decided to design it that way.



Related Topics



Leave a reply



Submit