Install and configure Apache and PHP with cgi on Ubuntu or Debian

By | December 9, 2014

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.

Install packages

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

Configure Apache

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

/etc/apache2/sites-enabled/

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

About Silver Moon

A Tech Enthusiast, Blogger, Linux Fan and a Software Developer. Writes about Computer hardware, Linux and Open Source software and coding in Python, Php and Javascript. He can be reached at [email protected].

4 Comments

Install and configure Apache and PHP with cgi on Ubuntu or Debian
  1. Gunstick

    [authz_core:error] [pid 15053] [AH01630: client denied by server configuration: /usr/bin/php-cgi

    I know /usr/bin is bad. But I first want to make it run at all. If that finally works, I’ll move php to a single dir

  2. Bachi

    You’re probably not aware of how much trouble you can cause with this suggestion:
    ScriptAlias /local-bin /usr/bin

Leave a Reply

Your email address will not be published. Required fields are marked *