Great Templates

FEATURED WEB TEMPLATES

Amazon Books

Learn PHP

PHP Training
Zend Cert Training Zend Certified Engineer Zend PHP Pro

PHP: Installation And Configuration

1. General Installation Considerations

Table of contents:

  • General Installation Considerations
  • Installation on Linux systems
  • Installation on Windows systems
  • Runtime Configuration

The following are the three main feilds were you can apply the use of PHP.

  • Server-side scripting
  • Command line scripting
  • Client-side GUI applications

For the first and most common form, you need three things: PHP itself, a web server and a web browser. You probably already have a web browser, and depending on your operating system setup, you may also have a web server (e.g. Apache on Linux and MacOS X; IIS on Windows). You may also rent webspace at a company. This way, you don\\\\\\\\'t need to set up anything on your own, only write your PHP scripts, upload it to the server you rent, and see the results in your browser.

While setting up the server and PHP on your own, you have two choices for the method of connecting PHP to the server. For many servers PHP has a direct module interface (also called SAPI). These servers include Apache, Microsoft Internet Information Server, Netscape and iPlanet servers. If PHP has no module support for your web server, you can always use it as a CGI or FastCGI processor. This means you set up your server to use the CGI executable of PHP to process all PHP file requests on the server.

If you are also interested to use PHP for command line scripting (e.g. write scripts autogenerating some images for you offline, or processing text files depending on some arguments you pass to them), you always need the command line executable.

With PHP you can also write desktop GUI applications using the PHP-GTK extension. This is a completely different approach than writing web pages, as you do not output any HTML, but manage windows and objects within them.

From now on, this section deals with setting up PHP for web servers on Unix and Windows with server module interfaces and CGI executables. You will also find information on the command line executable in the following sections.

2. Installation on Linux systems

Getting Ready

Get ready to take the big plunge and set up your first dynamic Web site with PHP as your choice. This quick introduction will get your Linux box up and running with PHP in no time.

For starters, let\\\\\\\\'s lay out the basics: we\\\\\\\\'re going to install PHP on a Linux box, we\\\\\\\\'ll be running Apache as our Web server, and MySQL as our database. Since I\\\\\\\\'m running Red Hat Linux, I\\\\\\\\'ll run through things with that perspective, though any Linux distro shouldn\\\\\\\\'t be too far from what we\\\\\\\\'ll cover. We\\\\\\\\'ll assume you\\\\\\\\'re generally familiar with the Linux OS, and that you\\\\\\\\'ve got a basic idea on how to compile source code and install binaries.

Getting the Files You\\\\\\\\'ll Need

To do PHP right, you\\\\\\\\'ll need to grab the following:

  1. The latest version of Apache -- the stock install of Apache that comes with Red Hat Linux won\\\\\\\\'t do, you should definitely upgrade to the latest version. Either source code or binaries should be fine. You can grab the latest from our Apache Releases page. If you go with the binaries, make sure you get the apache-devel package too -- otherwise you\\\\\\\\'ll run into trouble later.
  2. The latest stable version of MySQL. I personally used the RPMs (binaries) as compiling the source was a bit tricky w/my Red Hat 5.2ish setup. You\\\\\\\\'ll need to download the Server, Client, Client Libraries, and Development packages -- the benchmarking package is optiona. Note, if you\\\\\\\\'re grabbing the RPMs, make sure to get the MySQL development package too --otherwise PHP won\\\\\\\\'t compile. You can get the files you need from our MySQL Releases page.
  3. The latest stable version of PHP (as of this writing, that means pre-PHP4) -- I\\\\\\\\'d reccomend not going with the RPMs here, the source code is a straight forward compile, and I found the RPMs to be a little tricky.
  4. Lastly, you\\\\\\\\'ll need to get the freetype-devel package. Red Hat distros come with Freetype out of the box, but not the development package, so head on over to RPMFind.net and download that one. (If you\\\\\\\\'re not sure if you have freetype-devel, you can check by typing rpm -q freetype-devel at the command line.)

Preparing to Install PHP

Once you\\\\\\\\'ve downloaded all of the above, you\\\\\\\\'ll need to do the following to get ready for your PHP install.

The first thing to do is to install the Apache upgrade you\\\\\\\\'ve downloaded. Assuming you\\\\\\\\'re going with the RPMs, cd into the directory you\\\\\\\\'ve downloaded to and it\\\\\\\\'s a simple

rpm -U apache*.rpm

at the command line.

Once you\\\\\\\\'ve done that, go ahead and install you\\\\\\\\'re freetype-devel package with a

rpm -i freetype-devel-x.x.x.rpm

and it\\\\\\\\'s two down, two to go. P>Next in line are the MySQL packages. Again, make sure you grab the MySQL-devel package, or your PHP compile will fail. If you\\\\\\\\'re in the directory where your MySQL packages are, do a

rpm -i MySQL*.rpm

and we\\\\\\\\'re pretty much there. There are some post installation steps needed to get MySQL configured correctly. Full instructions are available in the MySQL RPM Post Installation section.

The last thing to do before you can compile your PHP source is to open up your /usr/sbin/apxs file in your favorite text editor, and head down to line 81. Somewhere around that point you\\\\\\\\'ll find a line that starts with

my $CFG_LIBEXECDIR =

Make sure that this is set to /usr/lib/apache so that the whole line reads

my $CFG_LIBEXECDIR =\\\\\\\\'/usr/lib/apache\\\\\\\\'

If you\\\\\\\\'re apxs file doesn\\\\\\\\'t have this problem, obviously, you can skip this step.

Compile and Configure

Installing the PHP Source

Ok, now that all that\\\\\\\\' s done, we\\\\\\\\' re finally ready to compile and install the PHP source itself. First, unzip the PHP source by doing a

tar zxvf php-x.x.xx.tar.gz

Then, cd into the source directory and do a

./configure --with-apxs=/usr/sbin/apxs --with-mysql

for a standard MySQL-based setup. If you want to configure PHP with additional options, a ./configure --help will give you a list of what\\\\\\\\' s available.

The do the compile and install with

make

make install

Post Compilation Setup

A quick edit of a couple of Apache config files is all that\\\\\\\\' s needed to alert Apache to PHP. First, open up /etc/httpd/conf/httpd.conf and add the following lines

LoadModule php3_module libexex/libphp3.so

AddModule mod_php3.c

Make sure to check the LoadModule and AddModule sections of the conf file to make sure these lines aren\\\\\\\\' t already there -- doubles are bad. Next, edit the /etc/httpd/conf/srm.conf and add the following lines

AddType application/x-httpd-php3 .php3

AddType application/x-httpd-php3-source .phps

to tell Apache to use PHP on any file with a .php3 extension.

Last up -- restart Apache and you should be good to go

/etc/rc.d/init.d/httpd restart

That\\\\\\\\' s it, now you\\\\\\\\' ve got PHP installed, and you\\\\\\\\' re ready to rock! If you\\\\\\\\' re having trouble, head on over to the PHP Forum and ask away ... :)

3. Installation on Windows systems

  1. Download & Unpack

    Download and install PHP from
  2. http://www.php.net/downloads.php, you should grab the newest 5.x.x Windows Binary zip package that will work on apache.

    My file was named: php-5.0.3-Win32.zip
  3. Unzip php. In my case, I unzipped to:
    e:\\\\\\\\php\\\\\\\\
    (check and make sure you do not end up with something like e:\\\\\\\\php\\\\\\\\php-5.0.3-Win32\\\\\\\\)
  4. Rename e:\\\\\\\\php\\\\\\\\php.ini-dist it to php.ini
  5. Edit your php.ini

    Open php.ini in a text editor and scroll down about halfway through the file and look for doc_root then change it to point to whatever your Apache DocumentRoot is set to. In my case: doc_root = "e:\\\\\\\\public_html"

    Scroll down about 8 more lines and change the extension_dir from extension_dir = "./" to the location of the ext directory after you unzipped PHP. in my case: extension_dir = "e:\\\\\\\\php\\\\\\\\ext"

    Another thing you might want to change is you session path. Search for: session.save_path and uncomment (remove the ; mark) and change it to an existing directory. One reader pointed out the windows temporary directory works well (for windows directory, see "Some Notes" below). So session.save_path = "c:/windows/temp" or if you use 2k/XP maybe session.save_path = "c:/WINXP/Temp"

    If you are going to be using your server for testing, i recommend you search and change the error reporting to give more feedback. Look for and change:
    error_reporting = E_ALL & ~E_NOTICE & ~E_STRICT
    to
    error_reporting = E_ALL | E_NOTICE | E_STRICT

    Some notes
    On Windows 95/98/ME "windows" directory is called %WINDOWS% on Windows NT, Windows 2000, or Windows XP it\\\\\\\\'s called %SYSTEMROOT%.

    If you are unsure where that is, go to the Start menu > Run and type in %SYSTEMROOT% or %WINDOWS% and it will pop-up the directory you need
  6. Editing Apache Conf File

    Using Notepad open e:\\\\\\\\Apache2\\\\\\\\conf\\\\\\\\httpd.conf (in the start-menu there should also be a "Edit the Apache HTTP Server httpd.conf Configuration File" shortcut). Either at the very beginning or end of the file add the following lines: (NOTE: be sure to change the e:/php/php5apache2.dll to the directory you installed your php to and change e:/php to the directory the php.ini file is in)
    	LoadModule php5_module "e:/php/php5apache2.dll"
    AddType application/x-httpd-php .php
    PHPIniDir "e:/php"
  7. Testing

    Restart Apache if it is already running. (if it doesn\\\\\\\\'t start or you get errors, use your Apache "Test Configuration" shortcut to see why)

    To test your PHP simply create a test.php file and put these 3 lines into it. (When loaded in your browser, you should get a whole long list of php variables, settings, etc):
    phpinfo();
    ?>
  8. Documentation Suggestion

    One weird thing I have noticed about PHP is that it does not come with documentation of any kind. If you are a php developer/programmer, I suggest you download the documentation. Downloads can be found on http://www.php.net/download-docs.php and I personally recommend the "English - Windows HTML Help" (chm) version as the search is so handy, although they all contain the same information.

The configuration file

The configuration file (called php3.ini in PHP 3, and simply php.ini as of PHP 4) is read when PHP starts up. For the server module versions of PHP, this happens only once when the web server is started. For the CGI and CLI version, it happens on every invocation.

php.ini is searched in these locations (in order):

  • SAPI module specific location (PHPIniDir directive in Apache 2, -c command line option in CGI and CLI, php_ini parameter in NSAPI, PHP_INI_PATH environment variable in THTTPD)
  • HKEY_LOCAL_MACHINE\\\\\\\\SOFTWARE\\\\\\\\PHP\\\\\\\\IniFilePath (Windows Registry location)
  • The PHPRC environment variable
  • Current working directory (for CLI)
  • The web server\\\\\\\\'s directory (for SAPI modules), or directory of PHP (otherwise in Windows)
  • Windows directory (C:\\\\\\\\windows or C:\\\\\\\\winnt) (for Windows), or --with-config-file-path compile time option

If php-SAPI.ini exists (where SAPI is used SAPI, so the filename is e.g. php-cli.ini or php-apache.ini), it\\\\\\\\'s used instead of php.ini. SAPI name can be determined by php_sapi_name().

Note: The Apache web server changes the directory to root at startup causing PHP to attempt to read php.ini from the root filesystem if it exists.

The php.ini directives handled by extensions are documented respectively on the pages of the extensions themselves. The list of the core directives is available in the appendix. Probably not all PHP directives are documented in the manual though. For a complete list of directives available in your PHP version, please read your well commented php.ini file.

How to change configuration settings

Running PHP as an Apache module

When using PHP as an Apache module, you can also change the configuration settings using directives in Apache configuration files (e.g. httpd.conf) and .htaccess files. You will need "AllowOverride Options" or "AllowOverride All" privileges to do so.

With PHP 4 and PHP 5, there are several Apache directives that allow you to change the PHP configuration from within the Apache configuration files. For a listing of which directives are PHP_INI_ALL, PHP_INI_PERDIR, or PHP_INI_SYSTEM, have a look at the List of php.ini directives appendix.

Note: With PHP 3, there are Apache directives that correspond to each configuration setting in the php3.ini name, except the name is prefixed by "php3_".

php_value name valueSets the value of the specified directive. Can be used only with PHP_INI_ALL and PHP_INI_PERDIR type directives. To clear a previously set value use none as the value.

Note: Don\\\\\\\\'t use php_value to set boolean values. php_flag (see below) should be used instead.

php_flag name on|offUsed to set a boolean configuration directive. Can be used only with PHP_INI_ALL and PHP_INI_PERDIR type directives. php_admin_value name valueSets the value of the specified directive. This can not be used in .htaccess files. Any directive type set with php_admin_value can not be overridden by .htaccess or virtualhost directives. To clear a previously set value use none as the value. php_admin_flag name on|offUsed to set a boolean configuration directive. This can not be used in .htaccess files. Any directive type set with php_admin_flag can not be overridden by .htaccess or virtualhost directives.


 


Learn PHP | Zend Certified Engineer | Zend PHP Pro | PHP Web Apps | Web Hosting Service | Low Cost Domain Names | Great Templates | Great Books | Testimonials | Tech.Articles | TOS | AUS | Home | Linux Apache MySQL PHP