Searching...
Wednesday 5 June 2013

Configuring PHP

Configuring php


Although the base PHP installation is sufficient for most beginning users(like us), chances are you’ll soon want to make adjustments to the default configuration settings and possibly experiment (feeling like Dexter) with some of the third-party extensions that are not built into the distribution by default. In this section you’ll learn how to do so.


Configuring PHP at Build Time on Linux


You can view a complete list of configuration flags (there are more than 200) by executing the following:

%>./configure --help

To make adjustments to the build process, you just need to add one or more of these arguments to PHP’s configure command, including a value assignment if necessary. For example, suppose you want to enable PHP’s FTP functionality, a feature not enabled by default. Just modify the configuration step of the PHP build process like so:

%>./configure --with-apxs2=/usr/local/apache/bin/apxs --enable-ftp

As another example, suppose you want to enable PHP’s Bzip2 extension. Just reconfigure PHP like so:

%>./configure --with-apxs2=/usr/local/apache/bin/apxs \>--with-bz2=[INSTALL-DIR]

One common point of confusion among beginners is to assume that simply including additional flags will automatically make this functionality available via PHP. This is not necessarily the case. Keep in mind that you also need to install the software that is ultimately responsible for enabling the extension support. In the case of the bzip2 example, you need the Java Development Kit (JDK).


Customizing the Windows Build

A total of 45 extensions are bundled with PHP 5.1 and 5.2.X; however to actually use any of these extensions, you need to uncomment the appropriate line within the php.ini file. For example, if you’d like to enable PHP’s XML-RPC extension, you need to make a few minor adjustments to your php.ini file:

1. Open the php.ini file and locate the extension_dir directive and assign it C:\php\ext. If you installed PHP in another directory, modify this path accordingly.

2. Locate the line ;extension=php_xmlrpc.dll. Uncomment this line by removing the preceding semicolon. Save and close the file.

3. Restart the web server and the extension is ready for use from within PHP. Keep in mind that some extensions have additional configuration directives that may be found later in the php.ini file.

When enabling these extensions, you may occasionally need to install other software.

RUN-TIME CONFIGURATION

It’s possible to change PHP’s behavior at run time on both Windows and Linux through the php.ini file. This file contains myriad configuration directives that collectively control the behavior of each product. Our focus will be on PHP’s most commonly used configuration directives, introducing the purpose, scope, and default value of each.

Managing PHP’s Configuration Directives

Before you delve into the specifics of each directive, this section demonstrates the various ways in which these directives can be manipulated, including through the php.ini file, Apache’s httpd.conf and .htaccess files, and directly through a PHP script.

The php.ini File

The PHP distribution comes with two configuration templates, php.ini-dist and php.ini-recommended (as of PHP 5.3.0 these have been renamed to php.ini-development and php.ini-production, respectively). You’ll want to rename one of these files to php.ini and if you are using Windows, place it in the location specified by the PHPIniDir directive found in Apache’s httpd.conf file.
The php.ini file is PHP’s global configuration file, much like httpd.conf is to Apache. This file underwent a fairly significant reorganization as of PHP 5.3.0; however, in both pre- and post-5.3 versions the file continues to be organized into twelve sections, including:



The php.ini file is a simple text file, consisting solely of comments and the directives and their corresponding values. Here’s a sample snippet from the file:

;
; Allow the <? tag
;
short_open_tag = Off

Lines beginning with a semicolon are comments; the parameter short_open_tag is assigned the value Off.
Exactly when changes take effect depends on how you install PHP. If PHP is installed as a CGI binary, the php.ini file is reread every time PHP is invoked, thus making changes instantaneous. If PHP is installed as an Apache module, php.ini is only read in once, when the Apache is first started. In this case, you must restart Apache for any of the changes to take effect.


The Apache httpd.conf and .htaccess Files

When PHP is running as an Apache module, you can modify many of the directives through either the httpd.conf file or the .htaccess file. This is accomplished by prefixing directive/value assignment with one of the following keywords:
  • php_value: Sets the value of the specified directive.
  • php_flag: Sets the value of the specified Boolean directive.
  • php_admin_value: Sets the value of the specified directive. This differs from php_value in that it cannot be used within an .htaccess file and cannot be overridden within virtual hosts or .htaccess.
  • php_admin_flag: Sets the value of the specified directive. This differs from php_value in that it cannot be used within an .htaccess file and cannot be overridden within virtual hosts or .htaccess.

For example, to disable the short tags directive and prevent others from overriding it, add the following line to your httpd.conf file:

php_admin_flag short_open_tag Off


Within the Executing Script

The third, and most localized, means for manipulating PHP’s configuration variables is via the ini_set() function. For example, suppose you want to modify PHP’s maximum execution time for a given script. Just embed the following command into the top of the script:

ini_set('max_execution_time', '60');

Configuration Directive Scope

Don't think that configuration directives can be modified anywhere!! For security resons each directive is assigned a scope, and the directive can be modified only within that scope. In total, there are four scopes:
  • PHP_INI_PERDIR: Directive can be modified within the php.ini, httpd.conf, or .htaccess files
  • PHP_INI_SYSTEM: Directive can be modified within the php.ini and httpd.conf files
  • PHP_INI_USER: Directive can be modified within user scripts
  • PHP_INI_ALL: Directive can be modified anywhere

0 comments:

Post a Comment

 
Back to top!