What is mod_php?
Asked Answered
K

6

120

While going through a Zend tutorial, I came across the following statement:

Note that the php_flag settings in .htaccess only work if you are using mod_php.

Can someone explain what that means?

Knar answered 26/4, 2010 at 10:55 Comment(0)
P
173

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)

Purgative answered 26/4, 2010 at 10:57 Comment(7)
I used xampp to install the php, apache and mysql package... So, what will be my setup?Knar
You should enable it if PHP isn't currently working. If PHP is already working then it must be using CGI, in which case you'll probably need to do more than just enable the module. The module may not even exist on the system.Dunnage
Yup..!. When i run phpinfo() i see that mod_php5 is included in the Loaded Modules section.... And that means i've got it running as an apache module... right?Knar
mod_php is by far the most common configuration.Aletheaalethia
@Pascal, I'd say mod_php is not real PHP; it's just an Apache addon. Or rather, I think you should start off the answer with a definition of What exactly does PHP mean?Flannery
PHP-FPM, FastCGI?Smalto
@Pascal MARTIN good answer, can you update your answer and add php-fpm as well, pleaseCollett
K
65

This answer is taken from TuxRadar:

When running PHP through your web server, there are two distinct options: running it using PHP's CGI SAPI, or running it as a module for the web server. Each have their own benefits, but, overall, the module is generally preferred.

Running PHP as a CGI means that you basically tell your web server the location of the PHP executable file, and the server runs that executable, giving it the script you called, each time you visit a page. That means each time you load a page, PHP needs to read php.ini and set its settings, it needs to load all its extensions, and then it needs to start work parsing the script - there is a lot of repeated work.

When you run PHP as a module, PHP literally sits inside your web server - it starts only once, loads its settings and extensions only once, and can also store information across sessions. For example, PHP accelerators rely on PHP being able to save cached data across requests, which is impossible using the CGI version.

The obvious advantage of using PHP as a module is speed - you will see a big speed boost if you convert from CGI to a module. Many people, particularly Windows users, do not realise this, and carry on using the php.exe CGI SAPI, which is a shame - the module is usually three to five times faster.

There is one key advantage to using the CGI version, though, and that is that PHP reads its settings every time you load a page. With PHP running as a module, any changes you make in the php.ini file do not kick in until you restart your web server, which makes the CGI version preferable if you are testing a lot of new settings and want to see instant responses.

Knar answered 26/4, 2010 at 11:19 Comment(1)
@JohannEchavarria, Unless you've added a lot of crazy stuff, restarting the server will take only a few seconds. For "normal" modern systems with SSD it's less than 2 seconds.Flannery
H
15

Your server needs to have the php modules installed so it can parse php code.

If you are on ubuntu you can do this easily with

sudo apt-get install apache2

sudo apt-get install php5

sudo apt-get install libapache2-mod-php5

sudo /etc/init.d/apache2 restart

Otherwise you may compile apache with php: http://dan.drydog.com/apache2php.html

Specifying your server OS will help others to answer more specifically.

Humboldt answered 26/4, 2010 at 11:6 Comment(0)
S
8

Just to add on these answers is that, mod_php is the oldest and slowest method available in HTTPD server to use PHP. It is not recommended, unless you are running old versions of Apache HTTPD and PHP. PHP-FPM and proxy_cgi are the preferred methods.

Shopkeeper answered 23/7, 2018 at 12:22 Comment(0)
P
5

It means that you have to have PHP installed as a module in Apache, instead of starting it as a CGI script.

Paradise answered 26/4, 2010 at 10:57 Comment(0)
Z
3

mod_php is a PHP interpreter.

From docs, one important catch of mod_php is,

"mod_php is not thread safe and forces you to stick with the prefork mpm (multi process, no threads), which is the slowest possible configuration"

Zloty answered 26/8, 2019 at 23:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.