configure apache php - rishindra namdeo

Upload: rishindra

Post on 31-May-2018

221 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/14/2019 configure apache php - Rishindra Namdeo

    1/6

    Steps to Setting Up PHP 5

    1. Download PHP 5

    Before you begin, get a copy of PHP 5 from the PHP download page. In particular, download the

    zip package from the "Windows Binaries" section - that is, don't get the installer. For example,select the package labelled "PHP 5.2.5 zip package" if 5.2.5 is the current version.

    2. Install PHP 5

    Create a folder on your hard disk for PHP. I suggest "c:\php" although you can use other names ifyou wish. Personally though, I prefer to avoid names with spaces in it, like "c:\Program Files\php"to avoid potential problems with programs that cannot handle such things. I will assume that youused c:\php in this tutorial.

    Extract all the files from the zip package into that folder. To do that simply double-click the zipfile to open it, and drag all the files and folders to c:\php.

    3. Upgraders: Remove the Old PHP.INI File from Your Windows Directory

    If you are upgrading to PHP 5 from an older version, go to your windows directory, typicallyc:\windows, and delete any php.ini file that you have previously placed there.

    4. Configuring PHP

    Go to the c:\php folder and make a copy of the file "php.ini-recommended". Name the new file"php.ini". That is, you should now have a file "c:\php\php.ini", identical in contents with"c:\php\php.ini-recommended".

    Note: if you are using Apache 1, you should either move the php.ini file to your windowsdirectory, "C:\Windows" on most systems, or configure your PATH environment variable toinclude "c:\php". If you don't know how to do the latter, just move the php.ini file to the"c:\windows" folder. You do not have to do this if you are using Apache 2, since we will include adirective later in the Apache 2 configuration file to specify the location of the php.ini file.

    Use an ASCII text editor (such as Notepad, which can be found in the Accessories folder of yourStart menu) to open "php.ini". You may need to make the following changes to the file, dependingon your requirements:

    1. Enable Short Open Tags

    Search for the line that reads:

    short_open_tag = Off

    If short_open_tag is set to "off", tags like "

  • 8/14/2019 configure apache php - Rishindra Namdeo

    2/6

    short_open_tag = On

    2. Magic Quotes

    By default, input data is not escaped with backslashes. That is, if your visitors enter aninverted comma (single quote) into your web form, the script will receive that unadorned

    inverted comma (single quote). This is for the most part desirable unless you specialrequirements. If you want your input data to have the backslash ("\") prefix, such as, forexample, to mimic your web host's settings, search for the following:

    magic_quotes_gpc = Off

    and replace it with:

    magic_quotes_gpc = On

    Do not do this unless your web host has this setting as well. Even with the setting of"Off", you can still use the addslashes() function in PHP to add the slashes for the

    specific pieces of data that need them.

    3. Register Globals

    A number of older scripts assume that all data sent by a form will automatically have aPHP variable of the same name. For example, if your form has an input field with a nameof "something", older PHP scripts assume that the PHP processor will automaticallycreate a variable called $something that contains the value set in that field.

    If you are running such scripts, you will need to look for the following field:

    register_globals = Off

    and change it to the following:

    register_globals = On

    WARNING: Do NOT do this unless you have third party scripts that need it. Whenwriting new scripts, it's best to always code with the assumption that the register_globalsitem is set to "Off".

    4. Display Errors

    On a "live" website, you typically want errors in your script to be silently logged to aPHP error file. On your own local machine, however, while you are testing anddebugging a PHP script, it is probably more convenient to have error messages sent to thebrowser window when they appear. This way, you won't miss errors if you forget to checkthe error log file.

    If you want PHP to display error messages in your browser window, look for thefollowing:

    display_errors = Off

  • 8/14/2019 configure apache php - Rishindra Namdeo

    3/6

    And change it to:

    display_errors = On

    This value should always be set to "Off" for a "live" website.

    5. SMTP Server

    If your script uses the mail() function, and you want the function to successfully sendmail on your local machine, look for the following section:

    [mail function]; For Win32 only.SMTP = localhostsmtp_port = 25

    ; For Win32 only.;sendmail_from = [email protected]

    Change it to point to your SMTP server and email account. For example, if your SMTPserver is "mail.example.com" and your email address is "[email protected]",change the code to:

    [mail function]SMTP = mail.example.comsmtp_port = 25sendmail_from = [email protected]

    Note that after you do this, when your script tries to use the mail() function, you will needto be connected to your ISP for the function to succeed. If you do not modify the abovelines and attempt to use mail() in your script, the function will return a fail code, and

    display (or log) the error (depending on how you configure php.ini to handle errors).

    (Note that in Apache 1.x, the smtp_port line may not be present. If so, don't include it.)

    How to Configure Apache for PHP 5

    There are two ways to setup Apache to use PHP: the first is to configure it to load the PHP interpreter as anApache module. The second is to configure it to run the interpreter as a CGI binary. I will supplyinformation for how you can accomplish both, but you should only implement one of these methods.Choose the module method if your web host also installed PHP as an Apache module, and use the CGImethod if they have implemented it to run as a CGI binary.

    1. Running PHP 5 as an Apache Module

    To configure Apache to load PHP as a module to parse your PHP scripts, use an ASCII text editorto open the Apache configuration file, "httpd.conf". If you use Apache 1.x, the file is found in"c:\Program Files\Apache Group\Apache\conf\". Apache 2.0.x users can find it in "C:\ProgramFiles\Apache Group\Apache2\conf\" while Apache 2.2.x users can find it in "C:\ProgramFiles\Apache Software Foundation\Apache2.2\conf\". Basically, it's in the "conf" folder ofwherever you installed Apache.

  • 8/14/2019 configure apache php - Rishindra Namdeo

    4/6

    Search for the section of the file that has a series of "LoadModule" statements. Statementsprefixed by the hash "#" sign are regarded as having been commented out.

    If you are using Apache 1.x, add the following line after all the LoadModule statements:

    LoadModule php5_module "c:/php/php5apache.dll"

    If you are using Apache 2.0.x, add the following line after all the LoadModule statements:

    LoadModule php5_module "c:/php/php5apache2.dll"

    If you are using Apache 2.2.x, add the following line instead:

    LoadModule php5_module "c:/php/php5apache2_2.dll"

    Note carefully the use of the forward slash character ("/") instead of the traditional Windowsbackslash ("\"). This is not a typographical error.

    If you are using Apache 1.x, search for the series of "AddModule" statements, and add thefollowing line after all of them. You do not have to do this in any of the Apache 2 series of webservers.

    AddModule mod_php5.c

    Next, search for "AddType" in the file, and add the following line after the last "AddType"statement. Do this whichever version of Apache you are using. For Apache 2.2.x, you can find the"AddType" lines in the section. Add the line just before the closing for that section.

    AddType application/x-httpd-php .php

    If you need to support other file types, like ".phtml", simply add them to the list, like this:

    AddType application/x-httpd-php .phtml

    Finally, for those using one of the Apache 2 versions, you will need to indicate the location of yourPHP ini file. Add the following line to the end of your httpd.conf file.

    PHPIniDir "c:/php"

    Of course if you used a different directory for your PHP installation, you will need to change"c:/php" to that path. Remember to use the forward slash ("/") here again.

    If you are using Apache 1, you will have already placed your php.ini file in either the Windowsdirectory or somewhere in your PATH, so PHP should be able to find it by itself. You can of coursedo the same if you are using Apache 2, but I find modifying the Apache configuration file a bettersolution than cluttering your c:\windows directory or your PATH variable.

    2. Running PHP 5 as a CGI Binary

  • 8/14/2019 configure apache php - Rishindra Namdeo

    5/6

    If you have configured PHP 5 to run as an Apache module, skip forward to the next section. Thissection is for those who want to configure PHP to run as a CGI binary.

    The procedure is the same whether you are using the Apache 1.x series or one of the 2.x series.

    Search for the portion of your Apache configuration file which has the ScriptAlias section. Add

    the line from the box below immediately after the ScriptAlias line for "cgi-bin". If you use Apache2.2.x, make sure that the line goes before the closing for that section.

    Note that if you installed PHP elsewhere, such as "c:\Program Files\php\", you should substitutethe appropriate path in place of "c:/php/" (for example, "c:/Program Files/php/").

    ScriptAlias /php/ "c:/php/"

    Apache needs to be configured for the PHP MIME type. Search for the "AddType" comment blockexplaining its use, and add the AddType line in the box below after it. For Apache 2.2.x, you canfind the AddType lines in the section. Add the following line justbefore the closing for that section.

    AddType application/x-httpd-php .php

    As in the case of running PHP as an Apache module, you can add whatever extensions you wantApache to recognise as PHP scripts, such as:

    AddType application/x-httpd-php .phtml

    Next, you will need to tell the server to execute the PHP executable each time it encounters a PHPscript. Add the following somewhere in the file, such as after the comment block explaining"Action". If you use Apache 2.2.x, you can simply add it immediately after your "AddType"statement above; there's no "Action" comment block in Apache 2.2.x.

    Action application/x-httpd-php "/php/php-cgi.exe"

    Note: the "/php/" portion will be recognised as a ScriptAlias, a sort of macro which will beexpanded to "c:/php/" (or "c:/Program Files/php/" if you installed PHP there) by Apache. In otherwords, don't put "c:/php/php.exe" or "c:/Program Files/php/php.exe" in that directive, put"/php/php-cgi.exe".

    If you are using Apache 2.2.x, look for the following section in the httpd.conf file:

    AllowOverride None

    Options NoneOrder allow,denyAllow from all

    Add the following lines immediately after the section you just found.

    AllowOverride None

  • 8/14/2019 configure apache php - Rishindra Namdeo

    6/6

    Options NoneOrder allow,denyAllow from all

    3. Configuring the Default Index Page

    This section applies to all users, whether you are using PHP as a module or as a CGI binary.

    If you create a file index.php, and want Apache to load it as the directory index page for yourwebsite, you will have to add another line to the "httpd.conf" file. To do this, look for the line inthe file that begins with "DirectoryIndex" and add "index.php" to the list of files on that line. Forexample, if the line used to be:

    DirectoryIndex index.html

    change it to:

    DirectoryIndex index.php index.html

    The next time you access your web server with just a directory name, like "localhost" or"localhost/directory/", Apache will send whatever your index.php script outputs, or if index.php isnot available, the contents of index.html.

    Restart the Apache Web Server

    Restart your Apache server. This is needed because Apache needs to read the new configuration directivesfor PHP that you have placed into the httpd.conf file. The command to do this for the Apache 2.x series canbe found in the Start menu: Start -> Programs -> Apache HTTP Server -> Control Apache Server ->Restart.