Run PHP with CGI and Apache on Debian 7 (Wheezy)
Traducciones al EspañolEstamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
DeprecatedThis guide has been deprecated and is no longer being maintained.
In instances where running the mod_php
module to run PHP scripts on Apache is not sufficient, PHP can be run as a CGI binary. Combined with the itk
multi-processing module (MPM), PHP scripts can be run as user processes in a per-virtual host setup. This guide will walk users through the process of setting up Apache and PHP CGI.
NoteThis guide is written for a non-root user. Commands that require elevated privileges are prefixed withsudo
. If you’re not familiar with thesudo
command, you can check our Users and Groups guide.
Before You Begin
Ensure that you have followed the Getting Started and Securing Your Server guides, and the Linode’s hostname is set.
To check your hostname run:
hostname hostname -f
The first command should show your short hostname, and the second should show your fully qualified domain name (FQDN).
Update your system:
sudo apt-get update && sudo apt-get upgrade
Installing Apache and PHP
If you have not already installed the Apache HTTP server, do so:
sudo apt-get install apache2
You can now configure virtual hosting in accordance with the needs of your server. Next, install the CGI binaries:
sudo apt-get install php5-cgi
When this process completes, we can configure Apache to hand PHP scripts to the CGI process for rendering these scripts.
Configure Apache for PHP CGI
In order to set up Apache to use PHP-CGI on Debian systems, you must enable the
mod_actions
module:sudo a2enmod actions
The required directives can be set anywhere in Apache’s configuration tree. We recommend creating the
php-cgi.conf
file in Apache’sconf.d/
directory and setting these variables there. For Debian systems this directory is located at/etc/apache2/conf.d/
. You may also choose to place these settings in your/etc/apache2/httpd.conf
file. Regardless of their location, the relevant settings are:- File: Apache Configuration Block
1 2 3
ScriptAlias /local-bin /usr/bin AddHandler application/x-httpd-php5 php Action application/x-httpd-php5 /local-bin/php-cgi
In this example, the path to the
php-cgi
binary is/usr/bin/php-cgi
. All files with thephp
extension will be handed to the PHP CGI binary.You may also choose to put these configuration directives within a virtual hosting block. If you do not have
mod_php
enabled or installed, you can use this to selectively enable PHP for certain virtual hosts. Furthermore, if your deployment requires multiple versions of PHP, you can specify virtual host specific handlers by specifying paths to various versions ofphp-cgi
.The configuration file for the CGI executable of PHP is located at
/etc/php5/cgi/php.ini
. You can modify this file to suit the needs of your deployment.- File: /etc/php5/cgi/php.ini
1 2 3 4 5 6 7
error_reporting = E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR display_errors = Off log_errors = On error_log = /var/log/php.log max_execution_time = 30 memory_limit = 64M register_globals = Off
If you need support for MySQL in PHP, then you must install the php5-mysql package:
sudo apt-get install php5-mysql
When
php-cgi
is configured, you can now safely enable theitk
multi-processing module for Apache. The installation process foritk
will restart the Apache process. If you choose to use PHP CGI with the default or existing MPM, then restart Apache by issuing the following command:sudo service restart apache2
Enabling the “itk” MPM
The default Apache configuration uses an MPM called worker
, which uses a threaded approach to efficiently handling HTTP requests. An alternative MPM is prefork
which does not use threads and is compatible with non-tread-safe libraries. Both the worker
and prefork
modules require that all requests be handled by a process running under a user with particular permissions. On Debian systems, Apache processes run under the www-data
user.
This may not be ideal if you have multiple users running publicly accessible scripts on your server. In some of these cases it is prudent to isolate virtual hosts under specific user accounts using an alternative MPM, known as itk
or mpm-itk
. Functionally, mpm-itk
is quite similar to prefork
; however, itk
can processes requests for each virtual host each site under a specified user account. This useful in situations where you’re hosting a number of distinct sites that you need to isolate sites on the basis of user privileges.
Install the
mpm-itk
module:sudo apt-get install apache2-mpm-itk
In the
<VirtualHost >
entries for your sites (the site-specific files in/etc/apache2/sites-avalible/
) add the following sub-block:- File: Apache Virtual Hosting Configuration Block
1 2 3
<IfModule mpm_itk_module> AssignUserId webeditor webgroup </IfModule>
In this example, webeditor
is the name of the user of the specific site in question, and webgroup
is the name of the user group that “owns” the web server related files and processes for this host. Remember that you must create the user accounts and groups using the useradd
command. Consider our documentation of
user groups and permissions for more information about creating the necessary users and groups.
More Information
You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.
This page was originally published on