After downloading the source distribution, extract the source in a directory by using the tar xvzf php-4.3.1tar.gz command. We recommend that you install it in the same directory as you installed the Apache source. For example, if you installed the Apache source in the /usr/local/src/httpd-2.0.16 directory, then extract PHP into the /usr/local/src directory. A new subdirectory, called php- 4.2.3 , will be created. At this point, you have to decide how you plan to run PHP. PHP can be run as an Apache module (embedded in the server itself or as a DSO module) or as a CGI solution. The CGI solution means that you will not have any performance advan- tage over regular CGI scripts with PHP scripts because a PHP interpreter will be loaded each time to process a CGI-mode PHP script. Building PHP as a CGI solution Like Perl, PHP can be used in standalone scripts as well as embedded in Web pages. To build the PHP interpreter for CGI-mode operations, do the following: 1. As root, change to the PHP source distribution directory and run the following: ./configure enable-discard-path with-mysql 2. Now run make && make install to compile and install the PHP inter- preter on your system. Building PHP as an Apache module This is the preferred way of using PHP with Apache. You can either store the PHP module within the Apache binary or install it as a DSO module for Apache. An advantage of a DSO module is that it can be unloaded by just commenting out a configuration line in httpd.conf, thus saving some memory. Here we will show you how to create PHP as a DSO module for Apache. You must have DSO support enabled in Apache before you can use PHP as a DSO module. To recompile Apache with DSO support, do the following: 1. From the Apache source distribution directory, run the following com- mand as root: ./configure prefix=/usr/local/apache enable-so You can also add other options as necessary. 2. Compile and install Apache using the make && make install command. Appendix D: Linux Primer 811 34 549669 AppD.qxd 4/4/03 9:28 AM Page 811 After you have a DSO support–enabled Apache server, perform the following steps to create a DSO module for PHP: 1. From the PHP source distribution directory, run the following command as root: ./configure with-apxs2=/usr/local/apache/bin/apxs \ enable-track-vars \ with-zlib \ with-mysql=/usr Here, the with-mysql option is set to /usr because MySQL RPM pack- ages install the include files in the /usr/include/mysql directory. If your system has MySQL includes in a different location, you should use a dif- ferent directory name. You can find out where MySQL includes are kept by using the locate mysql.h command, which is available on most UNIX systems with the locate database feature. 2. Run make && make install to compile and install the DSO version of the PHP module for Apache. 3. Run the /usr/local/apache/bin/apachectl restart command to restart (or start) Apache. Configuring Apache for PHP After you have installed the mod_php module for Apache and configured php.ini as discussed earlier, you are ready to configure Apache for PHP as follows: 1. Add the following line to the httpd.conf file: AddType application/x-httpd-php .php This tells Apache that any file with a .php extension must be treated as an application/x-httpd-php application and processed by the mod_php module. There is no reason to use a different extension for PHP scripts. For example, you can set the preceding AddType directive to AddType application/x- httpd-php .html and have all your HTML pages treated as PHP script. We don’t recommend using the .html extension because chances are good that many of your HTML pages are not PHP scripts, and you simply do not want to slow down your Web server by having it parse each page for PHP scripts. 2. Save the httpd.conf file and restart the Apache Web server as usual. 812 Part VII: Appendixes 34 549669 AppD.qxd 4/4/03 9:28 AM Page 812 Now you are ready to create PHP scripts for your Web site. You can create PHP scripts and store them anywhere in your Web site’s document tree and Apache will automatically process them as PHP scripts. Configuring PHP by using php.ini The PHP configuration file is called php.ini, and it is stored in the /usr/ local/lib directory by default unless you specified a different path during PHP source configuration using the configure utility. When a PHP module is loaded, it reads the php.ini file. The module looks for php.ini in the current working direc- tory, the path designated by the environmental variable PHPRC, and in /usr/ local/lib . If you use PHP as a CGI solution, the php.ini file is read every time a PHP CGI is run. Conversely, when PHP is loaded as an Apache module, it is read once. You must restart the Apache server by using the /usr/local/ apache/bin/apachectl restart command to reload any changes that you make in the php.ini file. PHP directives in httpd.conf With Version PHP 4, only four mod_php-specific directives, as outlined in the fol- lowing sections, are allowed in httpd.conf. All other PHP directives must be in the php.ini file. php_admin_flag The php_flag directive enables you to set a Boolean value (On or Off) for a config- uration parameter. This directive cannot appear in directory containers or per- directory .htaccess files. Syntax: php_admin_flag name On | Off Context: Server config, virtual host php_admin_value The php_admin_value directive enables you to set a value for a configuration parameter. This directive cannot appear in directory containers or per-directory .htaccess files. Syntax: php_admin_value name value Context: Server config, virtual host Appendix D: Linux Primer 813 34 549669 AppD.qxd 4/4/03 9:28 AM Page 813 php_flag The php_flag directive enables you to set a Boolean value (On or Off) for a config- uration parameter. Syntax: php_flag name On | Off Context: Server config, virtual host, directory, per-directory (.htaccess) For example: php_flag display_errors On php_value The php_value directive enables you to set a value for a configuration parameter. Syntax: php_value name value Context: Server config, virtual host, directory, per-directory (.htaccess) For example: php_value error_reporting 15 PHP directives in php.ini The php.ini file has a simple directive = value structure syntax. Lines consisting of leading semicolons or lines with only whitespace are ignored. Section names are enclosed in brackets. You can learn about all the directives that go in php.ini at www.php.net/manual/en/configuration.php. The following sections discuss the most useful directives. auto_prepend_file The auto_prepend_file directive enables you to set a header document with each PHP-parsed page. Syntax: auto_prepend_file filename The following example preload.php page will be loaded before each PHP page is processed (this page is a good place to establish database connections if all the pages in the site use the same database connection.): auto_prepend_file preload.php default_charset The default_charset directive sets the default character set. Syntax: default_charset char_set The following example sets the default character set to 8-bit UTF: default_charset = “UTF-8” 814 Part VII: Appendixes 34 549669 AppD.qxd 4/4/03 9:28 AM Page 814 disable_functions The disable_functions directive enables you to disable one or more functions for security reasons. Syntax: disable_functions function_name [function_name] You can specify a comma-delimited list of PHP functions as follows: disable_functions = fopen, fwrite, popen In the preceding example, the functions responsible for opening, writing file or pipes are disabled. This directive is not affected by the safe_mode directive. display_errors The display_errors directive enables or disables printing of error message onscreen. This is recommended only for use on development systems and not for use on production servers. For production systems, you should use log_errors along with error_log directives to log error messages to files or to a syslog server so that malicious users cannot break your applications to glean information about them. Syntax: display_errors On | Off enable_dl The enable_dl directive enables or disables the capability to dynamically load a PHP extension. Syntax: enable_dl On | Off Default setting: enable_dl On error_append_string The error_append_string directive sets the string that is appended to the error message. See the error_prepend_string directive above. Syntax: error_append_string string error_log The error_log directive sets the PHP error log path. You can specify a fully quali- fied pathname of the log file, or you can specify the keyword syslog on Unix sys- tems to log using the syslog facility. On Windows systems, setting this directive to syslog writes log entries in the Windows Event log. Syntax: error_log fqpn Appendix D: Linux Primer 815 34 549669 AppD.qxd 4/4/03 9:28 AM Page 815 . over regular CGI scripts with PHP scripts because a PHP interpreter will be loaded each time to process a CGI-mode PHP script. Building PHP as a CGI solution Like Perl, PHP can be used in standalone. You can create PHP scripts and store them anywhere in your Web site’s document tree and Apache will automatically process them as PHP scripts. Configuring PHP by using php. ini The PHP configuration. in the php. ini file. PHP directives in httpd.conf With Version PHP 4, only four mod _php- specific directives, as outlined in the fol- lowing sections, are allowed in httpd.conf. All other PHP directives