CGI - Common Gateway Interface
Apache can be configured to invoke the php interpreter as cgi program. This means that the php-cgi binary shall be run externally and the php script shall be executed by it and the output would be passed back to apache. This is one way to run php with apache. Other methods include shared module, fastcgi etc. Read my previous post on php server apis to learn more about various options to run php with apache.
Since in cgi mode, php is run separately, its possible to use a threaded apache mpm like worker. In this post we shall setup apache and php together through the cgi.
The necessary packages to install include, apache, mpm worker, php5, php cgi. Install all of them at one go using the apt-get command
# sudo apt-get install apache2 apache2-mpm-worker php5 php5-cgi
Enable mod actions in apache.
$ sudo a2enmod actions
If you already had apache and php installed with mod php, then you first need to disable mod php
$ sudo a2dismod php5
Now configure apache to run php as cgi. The configuration is done in the relevant vhost block.
The Apache virtual host configuration files are located at the following location
The default vhost file is 000-default.conf which is present on most systems.
A few necessary directives have to be added in the right directory block in the configuration file which will tell apache to use php-cgi binary to run files ending with ".php".
The directives are AddHandler, Action and ScriptAlias. A minimal configuration looks like this
<Directory /var/www/> Options Indexes FollowSymLinks MultiViews AllowOverride None Order allow,deny allow from all AddHandler cgi-handler .php Action cgi-handler /local-bin/php-cgi </Directory> ScriptAlias /local-bin /usr/bin
The AddHandler line declares a handler called cgi-handler for .php files. This can be named to anything.
The Action line tells apache to run cgi-handler using the /local-bin/php-cgi program, which is the php cgi binary. Now this path is not a real one.
The ScriptAlias line maps /local-bin to /usr/bin. So the path /local-bin/php-cgi becomes /usr/bin/php-cgi which is the actual path of the php-cgi binary on ubuntu/debian.
Now restart apache
# service apache2 restart
Put a php file with phpinfo() in the web root and open from browser. The Server API shall be reported as "CGI/FastCGI".