PHP can be used either as a language that's embedded within web pages, to be run on the web server, or stand alone. Since it was designed as an embedded language, that's much the most common use and we'll look at its installation and configuration in that way in this module. It turns out that if you install PHP to be embedded in a web server, you can choose to get the stand alone program too!
1. PHP comes bundled with most Linux distributions. It won't necessarily be the most recent version (bundled software tends to provide the latest stable version that was available at the time that the bundle was sent for final testing), but it will be a version that has been somewhat configured to work with your installation.
2. Download the source code of PHP from www.php.net
and install your own version on your Linux or Unix system. The www.php.net site does NOT provide binaries for Linux / Unix, stating that it come with most operating system downloads anyway these days.
3. Download windows binaries from www.php.net
. There are
two versions - a .zip package (about 6 Mbytes in the current release) and an installer (about 1 Mbyte). You are strongly encouraged to use the .zip version; the installer version is restricted in what it provides - "CGI Only" it says, and the documentation says "We consider CGI installations of PHP to be suicidal".
4. As from release 10.1.x of Mac OSX, PHP was bundled as a part of the operating system - however, it is configured off by default.
If you have another requirement, the www.php.net site will point you at binaries for the less common operating systems, and there's plenty of discussions, documentation and FAQs available.
Before you source your PHP, consider very carefully which version you need.
UPDATE - April 2006. Latest versions are 4.4.x and 5.1.x - use 4.4.x for older compatibility and 5.1.x for new installations. Version 5.0 has a much improved OO model and is almost totally compatible with version 4 (but do note - "almost"). I'll leave my older notes following this paragraph in case you need to refer back to older versions.
Version 3 of PHP is now very old, and there are major speed and functionality improvements in version 4. Version 4.0 (up to version 4.0.6) is probably too old to recommend these days too.
Version 4.1.2 is configured (by default) to accept form variables both as globals and in $_GET and $_POST arrays. Globals are the way it always "was" done, but are less secure if the programmer doesn't really know about these things, so the $_GET and $_POST arrays were introduced in 4.1.0. You might choose 4.1.2 if you want a well established, stable PHP which is compatible with older and newer scripts
Version 4.2.3 ONLY accepts form variables via $_GET and $_POST by default, but it can be configured to accept the older form too.
Version 4.3.0 involved major internal changes. It was release in December 2002. There are a few compatibility and interfacing issues with release 4.3.0, and at the time of writing you might wish to stick with the previous release if you have a major software system already developed on another (older) server.
Note that PHP is a language with a very active developer community, and so you can expect new releases much more frequently than for other languages. You won't necessarily always want to upgrade to the latest release every time one comes out.
Before you install PHP to provide that executable content you want in your web page, you need to install a web server!
PHP works with a variety of web server, including Microsoft's IIS (Internet Information Server) and PWS (Personal Web Server), Xitami, Netscape servers, O'Reilly website pro, Sambar, OmniHTTPd, and Badblue. Far and away the most popular server is the Open Source Apache web server - this comes as standard with Linux, with OSX and with most other Unixes. It can be downloaded for Windows from www.apache.org, and being open source you don't pay a license fee!
Even if you're installing PHP on your local Windows machine that already has PWS or IIS on it, we suggest that you install Apache - especially if you're using you local machine as a test and development system; you're likely to find your ISP running Apache as the server of their choice. Installation and configuration of Apache is covered in other texts.
We'll look at some examples of PHP installation ...
INSTALLATION OF PHP UNDER AND APACHE WEB SERVER ON WINDOWS XP PRO
1. Download the .zip binary distribution of PHP for Windows from www.php.net
2. Unpack (unzip) the distribution file into a directory called c:\php ... you'll end up with a subdirectory there called
3. You have two files to copy
a) The file php4ts.dll should be copied to the sapi
subdirectory, where there should already by a file
called php4apache.dll and one called
b) The file php.ini-dist should be copied to the system
directory - usually c:\windows, and renamed to php.ini
4. Edit the Apache configuration file to include instructions for it to provide PHP support when restarted. The file is likely to be
c:\Program Files\Apache Group\Apache2\conf\httpd
and you need to add the following onto the end:
LoadModule php4_module c:/php/php-4.3.0-win32/sapi/php4apache2.dll
AddType application/x-httpd-php .php .php4
(note - you can select other / different file extensions for PHP if you wish, and you should load php4apache.dll not php4apache2.dll if you're running Apache 1.3)
5. Restart Apache
6. Create a file in the htdocs directory on the web server called test.php4, and type the following into it:
<?php phpinfo(); ?>
7. Visit the page with a browser and you should see ...
Now that you have basic functionality working, you'll probably want to go on and make some changes in the php.ini file to enable emailing from your server, and to allow the session functions to work. Both these features will NOT work unless you change the configuration file - you have been warned ;-)
INSTALLING PHP UNDER OSX
Although PHP is provided as part of the Apple operating system distribution, it's switched off by default ... first stage to getting it running is to turn on the web server from your system preferences (under the "sharing" submenu).
Then edit the apache configuration file at /private/etc/httpd/httpd.conf, taking the # character off the start of the two lines that mention php. (If you find one with a capital P, change it to a lower case p). Also add the following line at the end of the configuration file:
AddType application/x-httpd-php .php4
Stop and restart the server ("sharing" submenu of system preferences), and create and visit a test page containing
<?php phpinfo(); ?>
and you should see:
Runtime PHP configuration is controlled by the php.ini file that you may have had to make minor changes to during the installation process. If you're going to be running a public facing server, or if you're providing a service that's more than just a test and development installation, we suggest that you spend a few minutes going through some of the pertinent settings in this file.
The syntax of the file is straightforward. Whitespace and Lines beginning with a semicolon are silently ignored. Section headers (e.g. [Foo]) are also silently ignored, even though they might mean something in the future.
Directives are specified using the following syntax:
directive = value
Directive names are *case sensitive* - foo=bar is different from FOO=bar.
The value can be a string, a number, a PHP constant (e.g. E_ALL or M_PI), one of the INI constants (On, Off, True, False, Yes, No and None) or an expression (e.g. E_ALL & ~E_NOTICE), or a quoted string ("foo").
Boolean flags can be turned on using the values 1, On, True or Yes. They can be turned off using the values 0, Off, False or No.
SOME CHOICES FOR YOU TO MAKE
YOU NEED TO BE VERY CAREFUL OF SOME OF THESE as they will have script compatibility issues!
Relating to the population of global variables from the incoming form and environment:
register_globals = Off
; [Security, Performance]
register_globals = On
; [Old style compatibility]
and if you set it on:
variables_order = "EGPCS"
; [Collect environment variables as well as ...]
variables_order = "GPCS"
; [Collect Get, Post, Cookie and Server variables]
What do you want to happen to any error messages?
display_errors = Off
; [Don't tell the user but ...]
log_errors = On
; [... write to a log file]
error_log = filename
Do you want input data to be automatically \ protected?
magic_quotes_gpc = Off
Input data is no longer escaped with slashes so that it can be sent into SQL databases without further manipulation. Instead, you should use the function addslashes() on each input element you wish to send to a database.
include_path = ".:/php/includes"
; where to look for included files
CONFIGURATIONS TO ENABLE SERVICES:
The following must be checked if you want the services they effect to work!
file_uploads = On
; Allow file uploads?
; and if so, where to keep them temporarily
Email (for Windows systems):
SMTP = localhost
sendmail_from = email@example.com
mysql.connect_timeout = -1
(There are many other MySQL settings and settings for other relational databases!)
The following are for the session functions, which be default DO NOT WORK under Windows ay you must change the save_path:
session.save_handler = files
session.save_path = /tmp
session.use_cookies = 1
session.cookie_path = /
LIMITS YOU CAN CHANGE
max_execution_time = 30
; Maximum execution time of each script, in seconds
memory_limit = 8M
; Maximum amount of memory a script may consume (8MB)
post_max_size = 8M
; Maximum data that you can upload in a "post"
upload_max_filesize = 2M
; Maximum allowed size for uploaded files.
session.cookie_lifetime = 0
; Cookies to be non-persistent
session.cache_expire = 180
; sessions to be kept for 180 minutes
See also Training module on installing PHP
Sourcing, installing and configuring PHP 
Adding a PHP build option, rotating an image based on camera data, and a new look at thumbnails in PHP - (2015-02-22) 
PHP - some quick fixes if scripts have issues, and how to use our support - (2014-11-19) 
Building up from a small PHP setup to an enterprise one - (2012-12-16) 
Parse error: parse error, unexpected T_STRING on brand new web site - why? - (2012-03-03) 
Virtual Hosting with Apache http server - an overall scheme, and avoiding common pitfalls - (2011-01-14) 
Compile but do not run PHP - syntax check only - (2010-02-22) 
Your PHP code does not work? Here is where to start looking. - (2010-01-18) 
Controlling, supressing, enabling PHP error messages - (2009-10-02) 
How to make a Risotto (PHP build style) - (2009-08-02) 
Quick Summary - PHP installation - (2008-12-12) 
What is built in to this httpd and PHP? - (2008-08-23) 
Apache httpd, MySQL, PHP - installation procedure - (2008-08-01) 
cannot restore segment prot after reloc message - PHP / httpd - (2008-06-26) 
Checking server performance for PHP generated pages - (2008-06-06) 
Clustering, load balancing, mod_rewrite and mod_proxy - (2006-11-21) 
PHP upgrade - traps to watch - (2005-10-31) 
Development Environments - (2005-05-19) 
Current MySQL and PHP paths and upgrades - (2005-01-28)resource index - PHP
Solutions centre home page
You'll find shorter technical items at
The Horse's Mouth
delegate's questions answered at
At Well House Consultants, we provide
subjects such as Ruby, Lua, Perl, Python, Linux, C, C++,
Tcl/Tk, Tomcat, PHP and MySQL. We're asked (and answer)
many questions, and answers to those which are of general
interest are published in this area of our site.