. 96
( 132 .)


must be done in concert. Once again you have many, many installation options.
You can create PHP as an executable for use with CGI or command-line processing,
as a shared Apache module (apxs), or for DSO. Here we cover only installation as an
Apache module (except for OS X, which is discussed later in the appendix).
Start by unpacking both Apache and PHP.

gunzip apache 1.3.x.tar.gz
tar xf apache1.3.x.tar
gunzip php-4.2.tar.gz
tar xf php-4.2.tar.gz

Here x (in apache_1.3.x.tar) is the version number of Apache.
Use cd to move into the Apache directory and run configure, specifying the
path to the directory in which you want to install Apache. You should be probably
be running as the root account before you do this.

./configure --prefix=/path/to/apache

This prepares Apache to set up machine-specific information that PHP needs to
compile. You come back later and finish up the Apache installation.
Then move to the directory holding PHP. Here you find a variety of flags you
might or might not wish to specify. We suggest using the following:

./configure --with-mysql=/path/to/mysql --enable-trans-sid

Here, the two flags do the following:

— --with-mysql ” Since the MySQL client libraries are no longer distributed
as part of PHP, the MySQL extension is not enabled by default. You need
to use this flag and, if you have installed MySQL somewhere other than
the default location, specify the directory where it can be found.
— --enable-trans-sid ” This option allows the session ID to be included
automatically in URLs after a session is started. The Shopping Cart appli-
cation in Chapter 14 makes use of this option.
Appendix C: Brief Guide to MySQL/PHP Installation and Configuration 579

Additionally, you might want to include one or more of the following flags:

— --with-gd ” The GD functions enable you to create images on the fly,
using nothing but code. A version of GD is bundled with PHP (at least,
as of this writing).
— --with-config-file-path=/path/to/file ” The php.ini file specifies
many options for the PHP environment. PHP expects to find the file in
/usr/local/lib/php.ini. If you wish to change the location, use this flag.

You can incorporate many other flags and libraries into PHP. Please see the
online manual (http://php.net/install.configure) or run --configure --
help for the latest and most complete list.
After running configure, run the following two commands, the latter as root:

make install

Now you need to go back to the Apache directory and rerun the configure

./configure --prefix=/path/to/apache --activate-module=

Note that libphp5.a will not yet exist. It is created after the compiling is completed.
Now it™s time to run the great twosome:

make install

Apache should now be installed in the directory you specified.
Now move back to the PHP directory and copy the file named php-ini.dist to
/usr/local/lib/php.ini (or to the directory you specified in the --config-file-path
The final step is to go into the /conf directory of your Apache installation and
open the httpd.conf file. There you should uncomment the following line:

AddType application/x-httpd-php .php

Then move into the /bin directory and start Apache.

./apachectl start

Your installation should now be complete.
580 Part V: Appendixes

PHP looks for the socket to MySQL in /tmp/mysql.sock. If you have more
than one MySQL installation and need PHP to connect to the socket in
another location, you need to specify that in the mysql_connect() func-
tion, like this:
“username”, “password”);

In Mac OS X you probably want to use the version of Apache that ships with the
OS. Therefore, you probably won™t need to do any configuring or compiling of
Apache. Simply start by configuring PHP the way you want it, using all the
libraries and flags you need. Simply add the --with-apxs flag when compiling
PHP, and PHP makes itself part of your Apache configuration.
Depending on the state of PHP 5 and OS X at the time you™re installing, PHP
may require some extra libraries, like libxml2. You should be able to get what you
need from either Darwin Ports at http://www.opendarwin.org/projects/
darwinports/ or Fink at http://fink.sourceforge.net/.
You still need to make the changes listed earlier in this section to your httpd.conf
file. In OS X, that file can be found in /etc/httpd.

PHP Configuration
The php.ini file is extremely large and has more options than we can cover here. A
full list of options and potential settings can be found at http://www.php.net/
manual/configuration.php. Here are a few highlights.

We™ve talked a lot in this book about the use of global variables. If possible you
should have the register_globals line set to Off. This configuration requires you
to use the $_GET, $_POST, $_SERVER, and other like-named arrays when using
global variables, which keeps your applications more secure. But if you™re working
off legacy applications, it may be tough to make all the changes needed to have
register_globals set to Off. If you are able to set register_globals to Off, you
can prevent anyone from setting an internal PHP variable by referring to that vari-
able in the URL or in a POST.

MySQL configuration entries
The following are some of the MySQL configuration entries:

mysql.allow_persistent =
mysql.max_persistent =
Appendix C: Brief Guide to MySQL/PHP Installation and Configuration 581

mysql.max_links =
mysql.default_port =
mysql.default_host =
mysql.default_user =
mysql.default_password =

If you want to forbid persistent connections, change that setting to Off; if you
want to limit the number of persistent links to MySQL, change the setting on
max_persistent and max_links from -1 to an appropriate number.
You can use the default_user, default_host, and default_password entries
if you want to save yourself the trouble of entering these strings in your
mysql_connect() command. Note that putting your MySQL password in the
MySQL configuration file is a tremendous security risk.
You can make lots of other settings in your MySQL configuration file. The fol-
lowing sections explain some of the more useful ones.


If magic_quotes_gpc is set to On, all single quotes (˜), double quotes (“), back-
slashes (\), and NULLs are prepended with a backslash immediately upon being
uploaded from a form element. This makes doing your inserts into MySQL a lot
If magic_quotes_runtime is set to On, data retrieved from the file system or a
database are automatically escaped with backslashes.


max_execution_time = 30
memory_limit = 8388608

These settings are intended to protect you in the event of an infinite loop or an
endlessly recursive function. All scripts are automatically terminated if they reach
either of these limits. If you want to use a script that you expect to take more than
30 seconds, you can set the maximum execution time within a script with the
set_time_limit () function. This function can contain a number of seconds; if
you want to specify no limit, specify set_time_limit(0).


auto_prepend_file =
auto_append_file =
582 Part V: Appendixes

With these settings you can specify files that are automatically included at the
start and end of your PHP files. It might be useful for connection information or
common headers.



This should contain a list of paths separated by colons (:). These paths are auto-
matically searched for every include() and require().

You might want to change many session settings. Here are a few of them:

session.save_handler = files
session.save_path = /tmp
session.use_cookies = 1
session.auto_start = 0

Appendix F contains a set of functions for using MySQL for session handling. If
you want to use it you must set session.save.handler to user.
save_path indicates where in the file system PHP saves session information.
If use_cookies is set to 0 you must use another means of storing cookies ”
either using <?=SID ?> or configuring PHP with the --with-trans-sid flag.
Finally, if auto_start is set to 1, sessions are started automatically on every
page, meaning you™ll be able to track users through your pages by examining their
session IDs.
Appendix D

MySQL Utilities
THIS APPENDIX PRESENTS a brief overview of some of the MySQL administrative util-
ities. These are the tools that you™ll use to build and maintain your databases.
The best place to get the full details about the tools you have available to you is
the Docs subdirectory of your local installation of MySQL. (Note: This is the install
directory, not the data directory.) You can also check the online version of the
MySQL documentation at http://www.mysql.com/documentation/.
Keep in mind that the online MySQL manual does not do a good job of indicat-
ing when features were added to a utility. This can be confusing as you may find
yourself attempting to use a feature that is not available in your currently installed
version. Don™t be surprised if you can™t find a utility ” it may have been added in a
later version.

This is the command-line interface to MySQL; it enables you to run any arbitrary SQL
command, as well as the MySQL-specific commands like describe table. It™s a tool
you should get to know. You can use it to test out or debug queries for your code,
create your database, create tables, add columns to existing tables ” everything,
really. It also has some batch-oriented options that make it handy to use in mainte-
nance scripts, or as a quick no-frills reporting tool.

mysql [options] [databasename] [<inputfile] [>outputfile]

If you just type mysql, you™ll start the tool up, but you won™t be anywhere. When
you try to do anything that involves interaction with a database, you™ll get this

ERROR 1046: No Database Selected

To select one, type

use databasename;

584 Part V: Appendixes

use™ is one of the mysql tool™s built-in commands. Type help to see a list of

help (\h) Display this help.
? (\?) Synonym for `help™.
clear (\c) Clear command.
connect (\r) Reconnect to the server. Optional arguments are db
and host.
edit (\e) Edit command with $EDITOR.
ego (\G) Send command to mysql server, display result
go (\g) Send command to mysql server.
nopager (\n) Disable pager, print to stdout.
notee (\t) Don™t write into outfile.
pager (\P) Set PAGER [to_pager]. Print the query results via
print (\p) Print current command.
quit (\q) Quit mysql.
rehash (\#) Rebuild completion hash.
source (\.) Execute a SQL script file. Takes a file name as an
status (\s) Get status information from the server.
system (\!) Execute a system shell command (Linux only).
tee (\T) Set outfile [to_outfile]. Append everything into
given outfile.
use (\u) Use another database. Takes database name as


. 96
( 132 .)