Find Out How PHP Is Running on Server (Cgi or Fastcgi or Mod_PHP)

Find out how PHP is running on server (CGI OR fastCGI OR mod_php)

That's the Server API row on top of phpinfo()'s output:

Server API: Apache 2.0 Handler + CGI/FastCGI

However, please note that it won't necessarily tell you the exact version of Apache or the exact CGI handler. It just describes the SAPI in use.

You can also call the php_sapi_name() function (or the PHP_SAPI constant, which provides the same info):

Description

string php_sapi_name ( void )

Returns a lowercase string that describes the type of interface (the
Server API, SAPI) that PHP is using. For example, in CLI PHP this
string will be "cli" whereas with Apache it may have several different
values depending on the exact SAPI used

It's still a good idea to check your HSP's documentation because it possible to have several PHP versions available.


Remember you need to run phpinfo() from the same environment you want to check (web server won't tell you about command line and vice-versa):

C:\>php -i | findstr /C:"Server API"
Server API => Command Line Interface
$ php -i | grep 'Server API'
Server API => Command Line Interface

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.

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.

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.

How can I tell if my Apache2+PHP uses CGI/FCGI?

Use php_sapi_name() to find out.

PHP - how to best determine if the current invocation is from CLI or web server?

php_sapi_name is the function you will want to use as it returns a lowercase string of the interface type. In addition, there is the PHP constant PHP_SAPI.

Documentation can be found here: http://php.net/php_sapi_name

For example, to determine if PHP is being run from the CLI, you could use this function:

function isCommandLineInterface()
{
return (php_sapi_name() === 'cli');
}

Running Apache mod_php and mod_fastcgi in seperate vhosts on one Apache server

We've got a similar setup on our servers to run 2 versions of PHP, but under the same virtual host. Essentially it's for an old version of the software while transitioning to a new version - 1 runs through the original Apache config and the other through CGI.
Ours is directory based, so slightly different but I think it should work for your situation still.

First, we have the fastCGI config in a separate file /etc/apache2/mods-enabled/fastcgi.conf:

<IfModule mod_fastcgi.c>
# AddHandler fastcgi-script .fcgi
FastCgiWrapper /usr/lib/apache2/suexec
FastCgiIpcDir /var/lib/apache2/fastcgi
FastCgiConfig -idle-timeout 110 -killInterval 120 -pass-header HTTP_AUTHORIZATION -autoUpdate
ScriptAlias /php-fcgi/ /var/www/cgi-bin/
</IfModule>

Then, in the virtual host config itself we have this:

<VirtualHost *:80>
ServerName sitename
...
# Original non-CGI site
<Directory "/sites/webroot/site1/">
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
Allow from all
</Directory>

# FastCGI version
<Directory "/sites/webroot/site2/">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
AddHandler php-cgi .php
Action php-cgi /php-fcgi/php562.fcgi
<FilesMatch "\.php$">
SetHandler php-cgi
</FilesMatch>
</Directory>
</VirtualHost>

I think the key thing that is required for you is the AddHandler and SetHandler directives which make it work. Then you should be able to add as many separate sites either with different directories or individual virtual hosts.

How do I find out the currently running PHP executable?

The PHP_BINDIR constant gives you the directory where the php binary is

How can I tell Apache2, run mod_php5 by default but run this VH in CGI mode?

A good alternative is suPHP.

If set up properly, you can have as many handlers, as you wish; even for different directories within a virtual host.

For added security, there's suPHP's "paranoid" mode in which you assign a Unix user and group to a virtual host and the scripts will be run as that user.

My suPHP config looks like that:

...

[handlers]
;Handler for php-scripts
x-httpd-php=php:/usr/bin/php4-cgi
x-httpd-php5=php:/usr/bin/php5-cgi

;Handler for CGI-scripts
x-suphp-cgi=execute:!self

...

Within a simple .htaccess file it is possible to have scripts run using different versions of PHP:

<FilesMatch \.php$>
SetHandler x-httpd-php
</FilesMatch>

<FilesMatch \.php5$>
SetHandler x-httpd-php5
</FilesMatch>

# etc...

Hope that helps.

fastcgi, php and execution of external commands (convert mostly) not working

I figured it out !!!
the path for execution was different so I assume that some library could not be called and included by convert or other used programs.

I changed my php-cgi file to include the following:

PATH=$PATH:/usr/local/bin:/usr/local/sbin:/sbin:/usr/sbin
export PATH

my current php-cgi file is as follow:

#!/bin/sh
# Shell Script To Run PHP5 using mod_fastcgi under Apache 2.x
# Tested under FreeBSD 6.x and 7.x
### Set PATH ###
PHP_CGI=/usr/local/bin/php-cgi

### no editing below ###
PATH=$PATH:/usr/local/bin:/usr/local/sbin:/sbin:/usr/sbin
export PATH
export PHP_FCGI_CHILDREN=0
export PHP_FCGI_MAX_REQUESTS=10000
#export USE_ZEND_ALLOC=0
exec $PHP_CGI

and now everything works.

you probably have to adjust your paths to your OS



Related Topics



Leave a reply



Submit