Monday, September 27, 2010

Installing PHP4 for Linux and Apache

In this section, we will look at one way of installing PHP4 with Apache on Linux. The
process is more or less the same for any UNIX operating system. You might be able
to find prebuilt versions of PHP for your system, which are simple to install.
Compiling PHP, though, gives you greater control over the features built in to your
binary.
Before you install you should make sure that you are logged into your system as the
root user. If you are not allowed access to your system's root account, you may
need to ask your system administrator to install PHP for you.
There are two ways of compiling an Apache PHP module. You can either recompile
Apache, statically linking PHP into it, or you can compile PHP as a Dynamic Shared
Object (DSO). If your version of Apache was compiled with DSO support, it will be
capable of supporting new modules without the need for recompiling the server.This method is the easiest way to get PHP up and running, and it is the one we will
look at in this section.
In order to test that Apache supports DSOs you should launch the Apache binary
(httpd) with the -l argument.
/www/bin/httpd -l
You should see a list of modules. If you see
mod_so.c
among them, you should be able to proceed; otherwise, you may need to recompile
Apache. The Apache distribution contains full instructions for this.
If you have not already done so, you will need to download the latest distribution of
PHP4. Your distribution will be archived as a tar file and compressed with gzip, so
you will need to unpack it:
tar -xvzf php-4.0.tar.gz
After your distribution is unpacked, you should move to the PHP4 distribution
directory:
cd ../php-4.0
Within your distribution directory you will find a script called configure. This accepts
arguments that will control the features that PHP will support. For this example, we
will include some useful command line arguments, although you might want to
specify arguments of your own. We will discuss some of the configure options
available to you later in the hour.
./configure --enable-track-vars \
--with-gd \
--with-mysql \
--with-apxs=/www/bin/apxs
The path you assign to the --with-apxs argument is likely to be different on your
system. It is possible that you will find apxs in the same directory as your Apache
executable.
After the configure script has run, you can run the make program. You will need a C
compiler on your system to run this command successfully.
make make install
These commands should end the process of PHP4 compilation and installation. You
should now be able to configure and run Apache.
Some configure Options
When we ran the configure script, we included some command-line arguments
that determined the features that the PHP interpreter will include. The configure
script itself gives you a list of available options. From the PHP distribution directory
type the following:
./configure --help
The list produced is long, so you may want to add it to a file for reading at leisure:
./configure --help > configoptions.txt
Although the output from this command is very descriptive, we will look at a few
useful options— especially those that might be needed to follow this book.
--enable-track-vars
This option automatically populates associative arrays with values submitted as part
of GET, POST requests or provided in a cookie. You can read more about arrays in
Hour 7, "Arrays," and about HTTP requests in Hour 13, "Beyond the Box." It is a
good idea to include this option when running configure.
--with-gd
--with-gd enables support for the GD library, which, if installed on your system,
allows you to create dynamic GIF or PNG images from your scripts. You can read
more about creating dynamic images in Hour 14, "Working with Dynamic Images."
You can optionally specify a path to your GD library's install directory:
--with-gd=/path/to/dir
--with-mysql
--with-mysql enables support for the MySQL database. If your system has MySQL
installed in a directory other than the default location, you should specify a path:
--with-mysql=/path/to/dir
As you know, PHP provides support for other databases. Table 2.1 lists some of
them and the configure options you will need to use them.
Table 2.1: Some Database configure Options
Database configure Option
Adabas D --with-adabas
FilePro --with-filepro
msql --with-msql
informix --with-informix
iODBC --with-iodbc
OpenLink
ODBC
--with-openlink
Oracle --with-oracle
PostgreSQL --with-pgsql
Solid --with-solid
Sybase --with-sybase
Sybase-CT --with-sybase-ct
Velocis --with-velocis
LDAP --with-ldap


Configuring Apache
After you have compiled PHP and Apache, you should check Apache's configuration
file, httpd.conf, which you will find in a directory called conf in the Apache install
directory. Add the following lines to this file:
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
This ensures that the PHP interpreter will parse files that end with the .php
extension. Any files with the .phps extension will be output as PHP source. That is,
the source code will be converted to HTML and color-coded. This can be useful for
debugging your scripts.
If you want to offer to your users PHP pages with extensions more familiar to them,
you can choose any extension you want. You can even ensure that files with
the .html extension are treated as PHP files with the following:
AddType application/x-httpd-php .html
Note that treating files with the .html extension as PHP scripts could slow down your
site, because every page with this extension will be parsed by the PHP interpreter
before it is served to the user.
If PHP has been preinstalled and you have no access to the Apache configuration
files, you may be able to change the extensions that will determine which files will
be treated as PHP executables by including an AddType directive in a file
called .htaccess. After you have created this file, the directive will affect the
enclosing directory, as well as any subdirectories. This technique will only work if
the AllowOverride directive for the enclosing directory is set to either FileInfo or All.
Although the filename .htaccess is the default for an access control file, it may have
been changed. Check the AccessFileName directive in httpd.conf to find out. Even if
you don't have root access, you should be able to read the Apache configuration
files.
An .htaccess file can be an excellent way of customizing your server space if you do
not have access to the root account. An additional way of controlling the behavior of
PHP, even as a non-root user, is the php.ini file.


php.ini
After you have compiled or installed PHP, you can still change its behavior with a file
called php.ini. On UNIX systems, the default location for this file is /usr/local/lib;
on a Windows system, the default location is the Windows directory. A php.ini file
in the current working directory will override one in the default location, so you can
change the behavior of PHP on a per-directory basis.
You should find a sample php.ini file in your distribution directory, which contains
factory settings. Factory settings will be used if no php.ini file is used.
The default settings should be adequate for most of the examples in this book,
although you can read about some amendments you might like to make in Hour 22,
"Debugging."
Directives in the php.ini file take the form of a directive and a value separated by an
equals sign. Whitespace is ignored.
If PHP has been preinstalled on your system, you might want to check some of the
settings in php.ini. Remember, if you are not allowed to alter this document, you
can create one in your script's directory that can override the default. You can also
set an environmental variable PHPRC that designates a php.ini file.
You can change your php.ini settings at any time, though if you are running PHP as
an Apache module, you should restart the server for the changes to take effect.
short_open_tag
The short_open_tag directive determines whether you can begin a block of PHP
code with the symbols <? and close it with ?>. If this has been disabled, you will see
one of the following:
short_open_tag = Off
short_open_tag = False
short_open_tag = No
To enable the directive you can use one of the following:
short_open_tag = On
short_open_tag = True
short_open_tag = Yes
You can read more about PHP open and close tags in Hour 3, "A First Script."


Error Reporting Directives
To diagnose bugs in your code, you should enable the directive that allows error
messages to be written to the browser. This is on by default:

No comments:

Post a Comment