Run PHP Applications under CGI with Apache on CentOS 5

Select distribution:
Traducciones al Español
Estamos 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.
Deprecated

This guide has been deprecated and is no longer being maintained.

Create a Linode account to try this guide with a $ credit.
This credit will be applied to any valid services used during your first  days.

In most cases, we recommend using the mod_php module to run PHP scripts with the Apache HTTP server. This embeds a PHP interpreter in the web server process and makes running PHP applications easy. The embedded interpreter approach, however, is not without challenges; it forces all scripts to be executed with the permissions of a shared user account, and is incompatible with some other Apache modules and process. For example, in our experience mod_php is incompatible with the mod_rails or Phusion Passenger method of running Ruby On Rails. In these cases, if you want to run PHP and Rails applications within a single instance of Apache, you must run PHP scripts as CGI processes, using the method outlined below.

Before beginning this guide, we assume that you’ve completed the Setting Up and Securing a Compute Instance. If you are new to Linux server administration, we recommend considering the beginner’s guide, and the article concerning systems administration basics. If you’re interested in learning more about the Apache HTTP server, we encourage you to consider our extensive documentation on Apache configuration.

Set the Hostname

Before you begin installing and configuring the components described in this guide, please make sure you’ve followed our instructions for setting your hostname. Issue the following commands to make sure it is set properly:

hostname
hostname -f

The first command should show your short hostname, and the second should show your fully qualified domain name (FQDN).

Installing Apache and PHP

Make sure your system is up to date by issuing the following command:

yum update

If you have not already installed the Apache HTTP server, issue the following command to install the packages for Apache:

yum install httpd

Issue the following command to ensure that Apache will start following the next reboot cycle:

chkconfig httpd on

You can now configure virtual hosting in accordance with the needs of your server. To install the PHP CGI binaries, issue the following command:

yum install php-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

The directives required to enable PHP CGI may be set anywhere in Apache’s configuration tree. We recommend creating the php-cgi.conf file in Apache’s conf.d/ directory and setting these variables there. For CentOS systems this is located at /etc/httpd/conf.d/. 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 the php 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 of php-cgi.

The configuration file for PHP is located at /etc/php.ini. You can modify this file to suit the needs of your deployment.

File: /etc/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 with the following command:

yum install php-mysql

Congratulations! Apache is now configured to run PHP scripts using CGI.

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


Your Feedback Is Important

Let us know if this guide was helpful to you.


Join the conversation.
Read other comments or post your own below. Comments must be respectful, constructive, and relevant to the topic of the guide. Do not post external links or advertisements. Before posting, consider if your comment would be better addressed by contacting our Support team or asking on our Community Site.
The Disqus commenting system for Linode Docs requires the acceptance of Functional Cookies, which allow us to analyze site usage so we can measure and improve performance. To view and create comments for this article, please update your Cookie Preferences on this website and refresh this web page. Please note: You must have JavaScript enabled in your browser.