Docstoc

How to Install and Configure PHP 5 to Run with Apache on Windows

Document Sample
How to Install and Configure PHP 5 to Run with Apache on Windows Powered By Docstoc
					How to Install and Configure PHP 5 to Run
with Apache on Windows
by Christopher Heng, thesitewizard.com

Many web developers want to run Apache and PHP on their own computer since it allows them
to easily test their scripts and programs before they put them "live" on the Internet. This article
gives a step by step guide on how you can install and configure PHP5 to work together with the
Apache HTTP Server on Windows. The procedure has been tested to work on both Windows XP
and Vista.

If you have not already installed Apache on your machine, check out one of the guides listed
below. This "How To" guide assumes that you have already completed installing Apache.

      If you are using Apache 1.3.x, see the guide How to Install the Apache Web Server 1.x
       on Windows.
      If you plan to use one of the Apache 2 or 2.2 web servers on Windows XP, see the
       tutorial How to Install and Configure Apache 2 on Windows instead.
      If you are using Apache 2.2 on Windows Vista, please read How to Install Apache 2.2 on
       Windows Vista.

Note: those planning to install PHP 4 on Apache 1.x should read my article How to Install and
Configure PHP4 to Run with Apache on Windows instead.

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 VC6 thread-safe 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.

       [Update: note that I have not tested the procedure below with any of the PHP 5.3
       versions, only with 5.2.5, which was the latest version at the time I originally wrote this.
       In theory, the procedure should work with later 5.2 versions as well. I'm not sure about
       5.3 though. A version jump from 5.2 to 5.3 usually means bigger changes than simple
       bug fixes. If you want to be sure the procedure below will work, just get the latest of the
       5.2 series.]

   2. Install PHP 5

       Create a folder on your hard disk for PHP. I suggest "c:\php" although you can use other
       names if you 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 you used c:\php in this tutorial.

  Extract all the files from the zip package into that folder. To do that simply double-click
  the zip file 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,
  typically c:\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
  content with "c:\php\php.ini-recommended".

  Note: if you are using Apache 1, you should either move the php.ini file to your windows
  directory, "C:\Windows" on most systems, or configure your PATH environment variable
  to include "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 a directive 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 your Start menu) to open "php.ini". You may need to make the following changes to
  the file, depending on 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 "<?" will not be recognised as the start
         tag for a PHP script. In such a case, to begin a PHP script, you will need to code
         your script with an opening tag like "<?php". Since many third party PHP scripts
         use "<?", setting this to "Off" will give you more problems than it's worth,
         particularly since most, if not all, commercial web hosts that support PHP have no
         issues with your scripts using "<?" as the open tag. To fix this, change it to the
         following:

         short_open_tag = On

     2. Magic Quotes
   By default, input data is not escaped with backslashes. That is, if your visitors
   enter an inverted 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 have special requirements. If you want your input data to have the
   backslash ("\") prefix, such as, for example, 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 a PHP variable of the same name. For example, if your form has an input
   field with a name of "something", older PHP scripts assume that the PHP
   processor will automatically create 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.
   When writing new scripts, it's best to always code with the assumption that the
   register_globals item is set to "Off".

4. Display Errors

   On a "live" website, you typically want errors in your script to be silently logged
   to a PHP error file. On your own local machine, however, while you are testing
   and debugging a PHP script, it is probably more convenient to have error
   messages sent to the browser window when they appear. This way, you won't
   miss errors if you forget to check the error log file.
  If you want PHP to display error messages in your browser window, look for the
  following:

  display_errors = Off

  And change it to:

  display_errors = On

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

5. Session Path

  If your script uses sessions, look for the following line:

  ;session.save_path = "/tmp"

  The session.save_path sets the folder where PHP saves its session files. Since
  "/tmp" does not exist on Windows, you will need to set it to a directory that does.
  One way is to create a folder called (say) "c:\tmp" (the way you created c:\php
  earlier), and point this setting to that folder. If you do that, change the line to the
  following:

  session.save_path = "c:\tmp"

  Notice that in addition to changing the path, I also removed the semi-colon (";")
  prefix from the line.

  Alternatively, you can find out the current TEMP folder on your computer and
  use that. Or create a "tmp" folder in your PHP directory, like "c:\php\tmp" and set
  the configuration file accordingly. The possibilities are endless. If you can't
  decide, just create "c:\tmp" and do as I said above.

6. SMTP Server

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

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

  ; For Win32 only.
  ;sendmail_from = me@example.com
              Change it to point to your SMTP server and email account. For example, if your
              SMTP server is "mail.example.com" and your email address is
              "youremail@example.com", change the code to:

              [mail function]
              SMTP = mail.example.com
              smtp_port = 25
              sendmail_from = youremail@example.com

              Note that after you do this, when your script tries to use the mail() function, you
              will need to be connected to your ISP for the function to succeed. If you do not
              modify the above lines 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 set up Apache to use PHP: the first is to configure it to load the PHP
interpreter as an Apache module. The second is to configure it to run the interpreter as a CGI
binary. I will supply information 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 CGI method 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 editor to 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:\Program Files\Apache Group\Apache2\conf\" while Apache 2.2.x users can
       find it in "C:\Program Files\Apache Software Foundation\Apache2.2\conf\". Basically,
       it's in the "conf" folder of wherever you installed Apache.

       Search for the section of the file that has a series of "LoadModule" statements.
       Statements prefixed 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
  Windows backslash ("\"). This is not a typographical error.

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

  AddModule mod_php5.c

  Next, search for "AddType" in the file, and add the following line after the last
  "AddType" statement. Do this no matter which version of Apache you are using. For
  Apache 2.2.x, you can find the "AddType" lines in the <IfModule mime_module>
  section. Add the line just before the closing </IfModule> 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 your PHP 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
  Windows directory or somewhere in your PATH, so PHP should be able to find it by
  itself. You can of course do the same if you are using Apache 2, but I find modifying the
  Apache configuration file a better solution than cluttering your c:\windows directory or
  your PATH variable.

2. Running PHP 5 as a CGI Binary

  If you have configured PHP 5 to run as an Apache module, skip forward to the next
  section. This section 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 Apache 2.2.x, make sure that the line goes before the closing
</IfModule> for that <IfModule alias_module> section.

Note that if you installed PHP elsewhere, such as "c:\Program Files\php\", you should
substitute the appropriate path in place of "c:/php/" (for example, "c:/Program
Files/php/"). Observe carefully that I used forward slashes ("/") instead of the usual
Windows backslashes ("\") below. You will need to do the same.

ScriptAlias /php/ "c:/php/"

Apache needs to be configured for the PHP MIME type. Search for the "AddType"
comment block explaining its use, and add the AddType line in the box below after it.
For Apache 2.2.x, you can find the AddType lines in the <IfModule mime_module>
section. Add the following line just before the closing </IfModule> 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 want Apache 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 PHP script. 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
be expanded to "c:/php/" (or "c:/Program Files/php/" if you installed PHP there) by
Apache. In other words, 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:

<Directory "C:/Program Files/Apache Software Foundation/Apache2.2/cgi-bin">
AllowOverride None
Options None
Order allow,deny
       Allow from all
       </Directory>

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

       <Directory "C:/php">
       AllowOverride None
       Options None
       Order allow,deny
       Allow from all
       </Directory>

   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
       your website, you will have to add another line to the "httpd.conf" file. To do this, look
       for the line in the file that begins with "DirectoryIndex" and add "index.php" to the list of
       files on that line. For example, 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 is not 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
directives for PHP that you have placed into the httpd.conf file. The Apache 2.2 server can be
restarted by doubleclicking the Apache Service Monitor system tray icon, and when the window
appears, clicking the "Restart" button.

Testing Your PHP Installation
Create a PHP file with the following line:

<?php phpinfo(); ?>
Save the file as "test.php" or any other name that you fancy, but with the ".php" extension, into
your Apache htdocs directory. If you are using Notepad, remember to save as "test.php" with the
quotes, or the software will add a ".txt" extension behind your back.

Open your browser and access the file by typing "localhost/test.php" (without the quotes)
into your browser's address bar. Do not open the file directly on the hard disk — you'll only see
the words you typed in earlier. You need to use the above URL so that the browser will try to
access your Apache web server, which in turn runs PHP to interpret your script.

If all goes well, you should see a pageful of information about your PHP setup. Congratulations
— you have successfully installed PHP and configured Apache to work with it. You can upload
this same file, test.php, to your web host and run it there to see how your web host has set up his
PHP, so that you can mimic it on your own machine.

If for some reason it does not work, check to see whether your PHP setup or your Apache setup
is causing the problem. To do this, open a Command Prompt window (found in the
"Accessories" folder of your "Start" menu) and run php-cgi.exe on test.php with a command line
like "c:\php\php-cgi test.php" (without the quotes).

If invoking PHP from the command line causes a large HTML file with all the PHP
configuration information to be displayed, then your PHP set up is fine. The problem probably
lies with your Apache configuration. Make sure that you have restarted the Apache server after
making configuration changes. Verify that you have configured Apache correctly by looking
over, again, the instructions on this page and the steps given in How to Install and Configure
Apache 1.x for Windows (for Apache 1.x users) or How to Install and Configure Apache 2 on
Windows (for Apache 2.x users).

Learning PHP
The complete PHP reference manual can be obtained from the php website. You can refer to it
online or download the entire set of HTML files for reference offline. As its name implies, it is a
reference manual only. For tutorials, check out the PHP tutorials at thesitewizard.com. If you are
new to writing PHP scripts, the following chapters may interest you:

      Writing Your First PHP Script gets you on your feet quickly with writing a useful and
       functional PHP script.
      Form Validation, Disabling Browser Caching, Embedding HTML Code in PHP
       introduces more features of PHP in a practical, usable way.
      How to Prevent Email Injection in Your PHP Form-to-Mail Scripts deals with how to
       avoid a security hole in PHP scripts that use the mail() function.

Have fun!

Steps to Installing Apache 2.2 on Windows Vista
1. Download Apache 2.2

   If you haven't got Apache 2.2, download it from the Apache site. Since this guide deals
   with the 2.2.x series, simply click the "Download" link under the 2.2.x (where "x" is
   some number) section. You will be brought to the download page. Scroll down and look
   for one of the "Win32 Binary" links. I used the "Win32 Binary without crypto (no
   mod_ssl) (MSI Installer)" link for my purposes. Make sure that you get the 2.2.x version
   (where "x" is some number) and not the 2.0.x (again, where "x" stands for some number)
   binary, otherwise your installation procedure will not match those given in this guide.

   Save the file on your desktop. (Technically speaking, you can save it anywhere you like,
   but I shall assume that you saved it on your desktop in this tutorial. It's best that you save
   it on your desktop if you're following this tutorial, otherwise some of the command lines
   that you will use below won't work correctly.)

2. Run the Installer as Administrator

   Before you run the installer, you will need to open the command prompt window as
   administrator. To do this, click the Start menu button, the "All Programs" menu item, the
   "Accessories" folder, and look for the item "Command Prompt". Do NOT click it, or
   you'll be running the command prompt with normal rights (even if you're logged in as an
   administrator). Instead, right click "Command Prompt" (click with the right mouse
   button), and click the "Run as administrator" item in the menu that pops out.

   Vista will prompt you with a dialog box entitled "User Account Control". Click the
   "Continue" button. A command prompt window will open. This command prompt
   window has the title "Administrator Command Prompt" instead of the normal "Command
   Prompt" or "c:\Windows\system32\cmd.exe".

   (If you do not see the title "Administrator Command Prompt" on your command prompt
   window, it means you did it wrongly. Close the command prompt window and redo this
   step, this time following every step of my instructions.)

   Type the following line into the window followed by the ENTER key. Include all the
   quotation marks and the percent sign. (That is, type it literally as given.)

   cd "%USERPROFILE%\desktop"

   This command switches you to your desktop directory, where you have saved your
   Apache installer. For example, if your Vista username is "chris", you should now be in
   the "c:\Users\chris\desktop" directory. Next, you will need to run the Apache installer
   from this command line. The actual name of the Apache installer file is different
   depending on the version you downloaded. For example, at the time I wrote this article,
   the file I downloaded was called "apache_2.2.10-win32-x86-no_ssl.exe". Later versions
   may, instead, have an ".msi" extension, like "apache_2.2.11-win32-x86-no_ssl.msi",
  although you may not be able to see any file extension, depending on how you have
  configured Vista.

  Now run the installer by typing the full name of the file (including the extension,
  especially if it has an ".msi" extension) on the command line and hitting ENTER. If you
  are too lazy to type the full filename, are a slow typist or simply afraid that you'll give the
  wrong filename, a shortcut is to type "apache" and hit the TAB key. The command line
  shell (the Command Prompt) will immediately try to complete the filename for you. If
  you have many files on your desktop starting with the word "apache", just keep hitting
  the TAB key until Command Prompt auto-completes the correct filename for you.

  When the correct filename appears, hit ENTER to run the installer. The Apache
  installation wizard will then pop up.

3. How to Set Up Apache with the Installation Wizard on Vista

  Follow the installer's instructions on setting up the web server (that is, read the
  information displayed in each dialog box, and click the necessary buttons). For the most
  part, installing is pretty straightforward. Don't worry, the not-so-straightforward portions
  are described in detail in the next few paragraphs.

  When you come to the "Server Information" window, enter "localhost" (without the
  quotation marks) in both the "Network Domain" and "Server Name" fields. In the
  "Administrator's Email Address" field, enter whatever email address you wish. In theory,
  this information is used by the installer to create a default configuration file for your web
  server. (In practice, the installer seems to ignore whatever you enter here. But you should
  still enter what I said earlier, just in case the installer has been fixed in the version you
  use.) You can easily modify this in your configuration file later if you change your mind.

  Leave the default option "for All Users, on Port 80, as a Service" as it is. When you're
  satisfied with what you've entered, click the "Next" button.

  When you're presented with the "Setup Type" screen, leave the default setting of
  "Typical" and click "Next". You'll be asked for the location to install Apache. Leave it at
  the default of "c:\Program Files\Apache Software Foundation\Apache 2.2\". I will assume
  that you've installed to this location in the rest of this article. Click "Next".

  When the "Ready to Install the Program" window appears, click the "Install" button. The
  setup utility will proceed to set up Apache on your machine.

  When the installer is done, click the "Finish" button to dismiss it. There should be no
  error messages or error windows. (If you get any errors, it probably means that you did
  not rigorously follow my procedure in this guide. To solve it, uninstall everything and
  start again.)
       Do not close the administrator command prompt window that you opened earlier. You'll
       need it again later.

   4. How to Start and Stop the Apache Service: Introducing The Apache
      Monitor

       Once you've finished all the above steps, you should be able to see a new icon in your
       system tray. This is the Apache Monitor. If you're not sure which icon in your tray is the
       Apache monitor, move your mouse button over each icon and wait for the tooltip to
       appear. The Apache Monitor should give a tooltip of "Running all Apache services" after
       you have successfully installed the program.

       Doubleclick the icon to open up the Apache Monitor program. A window, "Apache
       Service Monitor", will appear. This program allows you to start and stop the Apache web
       server on your machine. By default, the setup utility starts the Apache web server. If you
       want to the server to run only when you actually test your website on your computer, you
       can use the monitor to stop and start it.

       At the risk of stating the obvious, clicking the "Stop" button will stop the Apache web
       server service. The "Start" button starts it. If you make a change to the Apache
       configuration file (see later in this article), you will need to hit the "Restart" button to
       reload the Apache service, so that the server will reread the configuration file. When
       you're through playing with the buttons, click the "OK" button (not the "Exit" button) to
       return the Monitor to the system tray. If you have accidentally hit the "Exit" button and
       found that you've killed the Monitor, you can restart it again from your Start menu. (Type
       "monitor apache servers" in the Start menu search field.)

       If a button is disabled, it means that it's not relevant to the current situation. For example,
       if the Apache web server is already running, the "Start" button will be disabled. You can
       pretty much ignore the other buttons.

   5. Interim Test

       At this point, the Apache web server is fully functional. To make sure that the server is
       properly set up, start up your web browser, and go to the address "http://localhost/". If
       you see a simple web page with the words "It works!", it means that the server
       installation was successful.

How to Make the Apache Web Server Convenient to Use on
Windows Vista
Even though Apache may now be properly installed on your system and running smoothly, in its
current state, it is not convenient to configure or even copy your website files to it.
The problem is that the current Windows version of Apache 2.2 installer (version 2.2.10 at the
time I wrote this article) was not really written with Vista in mind. As such, things that you
frequently need to modify, like the configuration file and the web server files, are located in the
"c:\Program Files\Apache Software Foundation\Apache 2.2\" folder. In Vista, the "Program
Files" folder and its descendants are protected folders that programs can access only if they are
run with administrator privileges (like we did earlier when we ran the Command Prompt as
administrator).

If we leave things in this state, the only way that you can configure Apache is to run your editor
as administrator. And when you need to copy your website's files to your local server, you'll need
to do it when running as administrator. All in all, this makes things very troublesome for the web
developer.

As such, until the Apache developers update the server to directly support Vista installations, you
may want to do the following to make your life easier.

   1. Create a Folder for Your Apache Configuration and Website Files

       Create a new folder, either on your Desktop or in your Documents folder, called
       "apache". Actually, it doesn't matter where you create the folder or what you call it.
       However, for the purpose of this tutorial, I will assume that you created a new empty
       folder called "apache" on your desktop. That is, if your username is "heng", you will have
       a new folder called "c:\Users\heng\Desktop\apache".

       (To create a folder, right click your desktop and select "New" from the menu that
       appears, followed by the "Folder" item in the submenu. Rename the "New Folder" to
       "apache".)

       Open the folder by doubleclicking it. It should be empty. Leave the window open.

   2. Copy the Conf and Htdocs Folders to Your Private Apache Folder

       Click the start menu followed by the "Computer" item in the menu. This will open an
       Explorer window. Navigate to "c:\Program Files\Apache Software Foundation\Apache
       2.2\" in the usual way you go to different folders on your computer. (That is, doubleclick
       the C: drive, then the "Program Files" folder, then the "Apache Software Foundation"
       folder, and finally the "Apache 2.2" folder).

       Look for the folder "conf". Right click the folder and select "Copy" from the menu that
       appears. Switch to the "apache" window that you opened in the previous step. Right click
       in the empty space in that window and select "Paste". Do the same for the "htdocs"
       folders as well. Do NOT drag and drop them. Copy and paste the folders or you will end
       up with folders and files having the wrong permissions.

       By the end of this step, you should have two copies of the "conf" and "htdocs" folders:
       one in your "Apache 2.2" Program Files folder, and one in your Desktop "apache" folder.
3. Delete the Original Conf and Htdocs Folders

   Open the Apache Monitor by doubleclicking on its system tray icon. Click the "Stop"
   button to stop the service. When the service has stopped, you will see a message in the
   lower window "The Apache2.2 service has stopped". Click the "OK" button.

   Now switch back to the first window, the one pointing to "c:\Program Files\Apache
   Software Foundation\Apache 2.2\". Right click the "conf" folder and click "Delete" from
   the menu that appears. You will be asked to confirm in triplicate (I'm not kidding). That
   is, a dialog box will appear asking if you are sure that you want to move the folder to the
   Recycle Bin. Click "Yes" to the prompt that appears. Another dialog box, with the title
   "Destination Folder Access Denied" will open, asking you to confirm the operation. Click
   "Continue". Yet another window, the "User Account Control" will appear, again asking
   for your permission to continue. Click "Continue" again.

   Repeat the procedure with the "htdocs" folder.

4. Create a Symbolic Link to Your Private Apache Folder

   Switch to the Administrator Command Prompt that you opened earlier.

   Type the following line into the "Administrator Command Prompt" window and hit the
   ENTER key.

   cd "c:\Program Files\Apache Software Foundation\Apache 2.2\"

   Be sure to type the opening and closing quotation marks (yes, the quotes are needed). The
   command will bring you into the Apache folder. To make sure you're in the right folder,
   type "dir" (without the quotes) followed by the ENTER key. You should see the same
   files and folders that you saw earlier in the Explorer window, albeit arranged in a
   different order.

   Type the following command, followed by the ENTER key. Notice that the command
   line includes characters like the quotation marks and the percent sign. All the characters
   are required.

   mklink \D conf "%USERPROFILE%\desktop\apache\conf"

   The shell (command prompt) will print reassuring words to the effect that a symbolic link
   has been created and where the link points to.

   If you make a mistake creating the symbolic link, so that it points to the wrong location,
   delete it by typing "rd conf" followed by the ENTER key. Then try again. Note that my
   above command line assumes that you have created a folder called "apache" on your
   desktop, as I mentioned earlier.
      Finally, create a symbolic link for the htdocs directory.

      mklink /D htdocs "%USERPROFILE%\desktop\apache\htdocs"

      To make sure that all is well, doubleclick the Apache Monitor icon again, and start the
      web server service. (That is, click the "Start" button in the Monitor window.) Then open
      your web browser and surf to "http://localhost/". The "It Works!" web page should
      appear. If "It Works" does not appear, and you get a directory listing of some other
      directory, it means that your "htdocs" link is pointing to the wrong location. Delete the
      existing link with "rd htdocs" in the Administrator Command Prompt and try again.

Using Apache on Vista: Basic Getting Started Guide
     How to Publish Your Web Pages to Your Own Apache Server

      The "htdocs" subfolder that we created in your desktop "apache" folder is where all your
      web pages live. Since the folder directly resides on your computer, you don't have to do
      anything special to "publish" your website files. That is, you don't have to upload your
      pages using FTP or anything like that. (You can't anyway, unless you set up an FTP
      server in addition to your web server.) All you need to do is to copy your files to the
      "htdocs" folder and it will be displayed by Apache when there is a request for them.

     How to Change the Server Name and Administrator's Email Address

      Remember how I mentioned earlier that if you change your mind about the email address
      you enter into the Apache setup utility or the domain name, you can change it later? For
      those who actually need to change these settings, you will have to modify the Apache
      configuration file, "httpd.conf". This file, if you have followed my procedure above, lives
      in the "conf" subfolder in your desktop "apache" folder.

      To edit it, either load it in Notepad directly, or invoke the Start menu, type "apache" into
      the "Start Search" box, and select the "Edit the Apache httpd.conf Configuration File"
      item that appears. This will invoke Notepad to load httpd.conf. Note that you should
      always use an ASCII or programmer's text editor to edit this file. Notepad is fine. But
      never use a word processor or office program. For example, any program that allows you
      to underline text, make things bold or italicize words should NOT be used.

      To change the server name, look for the following line and replace "localhost" with the
      domain you want.

      ServerName localhost:80

      If the "ServerName" entry is preceded by a hash ("#") sign, and there are no other
      "ServerName" entries in the file, remove the hash before you modify the line.
    To change the Administrator's email address, look for a line like the following (that is,
    starting with "ServerAdmin") and replace the email portion with whatever you want:

    ServerAdmin admin@localhost

    Once you're done with your changes, save the file by selecting the "Save" item from the
    "File" menu. Then invoke the Apache monitor and click the "Restart" button to restart the
    server.

    In general, if your purpose for setting up Apache is to test your site on your own machine
    before you upload it "live" onto a real web host, you don't have to change anything. For
    most people, the default settings are fine for such purposes.

    (As a side note, if you're thinking of buying your own domain name and attaching it to
    your web server, please note that your server, as currently set up, is not suitable for live
    deployment on the Internet. Live deployment requires additional security measures, not
    dealt with here. You will also need to have your domain point to your machine's real IP
    address in a name server.)

   How to Configure Apache to Accept Server Side Includes

    If you want the web server to handle Server Side Includes (SSI), search for the text
    "server-side includes" in the configuration file. (If you skipped the previous item, and
    thus don't know how to load the Apache configuration file, please return to that section to
    find out how it is done.)

    The text you found should be in a section of lines all preceded by the hash ("#") sign.
    Any line that has a hash mark at the start is regarded as a comment by the Apache web
    server. Comments are ignored by the server, and are meant as explanatory text for human
    beings to read. They don't do anything except give information to human readers.

    Add the following lines immediately after that block of comments. The lines should be
    added ABOVE the closing </IfModule> line.

    AddType text/html .shtml
    AddOutputFilter INCLUDES .shtml

    If you look carefully at the commented text, you will notice that the configuration
    settings I provided above are mirrored exactly in the last two lines of the commented
    block. Instead of adding my lines, you can also uncomment those two lines in your file by
    removing the initial hash ("#") prefix before each line.

    When you do this, your Apache server will now be able to recognize files ending with the
    ".shtml" extension, and parse them for SSI directives.
       For those who want "index.shtml" to be your default start page for your directories, ie, if
       you want Apache to load "index.shtml" when you type "http://localhost" or
       "http://localhost/directory/", you will need to search for the following line in your
       "httpd.conf" file:

       DirectoryIndex index.html

       Replace that line with:

       DirectoryIndex index.shtml index.html

       This setting causes Apache to first look for an "index.shtml" file in the directory and use
       that as the default page for that directory. If this file cannot be found, it will look for the
       next file specified. That is, Apache goes through the list of filenames in the order given.

       When you're through with the configuration, save the file by clicking "Save" from the
       "File" menu. Again, you will have to invoke the Apache Monitor and click the "Restart"
       button to restart the server. This is needed because Apache only checks the configuration
       file once, when it loads. Any time you change the configuration file, you need to click the
       "Restart" button to make sure that Apache recognizes your new configuration.

How to Add PHP and Perl to Your Apache Web Server
Out of the box, Apache does not contain support for PHP or Perl. If you want your server to
support PHP scripts, please see the article How to Install and Configure PHP 5 to Run with
Apache on Windows.

I also have an older article How to Add Perl CGI Script Support to Your Apache Server on
Windows that deals with adding Perl to Apache. That article was written for Apache 1.x, and is
thus probably not very relevant to the 2.x series. I'll eventually get round to updating the article
for Apache 2 on Vista, but in the meantime, if you're really stuck, it may provide some tips on
what to look for when installing Perl.

(Note: If you want to be notified when these articles are updated or when new articles are
published, please subscribe to thesitewizard.com's news feed. Instructions on how to do this can
be found in my FAQ How to Subscribe to or Read News Feeds. Subscription is free, of course.)

Conclusion
Congratulations! You have now not only installed Apache 2.2.x successfully on Windows Vista,
you've also configured it and made the overall use of Apache very much easier so that each time
you modify its settings or update your website, you don't have to face multiple UAC prompts
from Vista. And you didn't even have to permanently disable UAC (which, in spite of its
detractors, has its uses) to accomplish it.
PHP Tutorial: Writing Your First PHP
Script: Feedback Form Script
by Christopher Heng, thesitewizard.com

I have always believed that the most fun way to learn a new programming language, whether it is
a language like C or a scripting language like PHP, is to use it to write a real-life useful program.
Of course this is not the most systematic method of learning, but it works well if you already
have some background in programming.

Preliminaries
   1. Before you write your first PHP program, make sure that your website is running on a
      web host that runs PHP 4.1 or above.
   2. You may also find it useful to have a copy of PHP 4.1 or later installed on your own
      computer. This makes testing your PHP scripts much easier. If you use Windows, you
      can find some tips on installing PHP on your own computer from my article on "How to
      Install and Configure PHP 5 to Run with Apache on Windows" at
      http://www.thesitewizard.com/php/install-php-5-apache-windows.shtml
   3. And of course, you will need an ASCII text editor of some kind (such as Notepad on
      Windows). There's a list of such editors on
      http://www.thefreecountry.com/programming/editors.shtml
   4. This tutorial also assumes that you have at least some knowledge of HTML. This is
      necessary because if I have to explain all the HTML tags as well, this tutorial will end up
      being tediously long.
   5. You'll probably need a bit of programming background, or at the very least, an aptitude
      for computer programming. Note that being able to code in HTML does not count, since
      HTML is not a programming language. Unlike the majority of the other articles on
      thesitewizard.com, this article is not targeted at the absolute newcomer. You really need
      background in both web design using HTML and a bit of programming skill/aptitude,
      otherwise this article will be indecipherable. If you are reading this article because you
      want to create a website, please start with How to Make / Create Your Own Website: The
      Beginner's A-Z Guide instead.

I will begin with a very rudimentary (but working) PHP script to take input from a feedback
form and send it to you in an email message. This type of form is sometimes referred to as a
FormMail or Form to Mail script. In later articles, I will develop that script (and others) to
include features commonly found in such FormMail scripts.

If you are programming-savvy, you will recognize this as a sort of "Hello World" program, but
infinitely more useful!

Writing the Feedback Form
The first thing we need to do is to write the feedback form itself. Put the following code in the
<body> section of an HTML file named, say, feedback.html.

<form method="post" action="sendmail.php">
  Email: <input name="email" type="text" /><br />
  Message:<br />
  <textarea name="message" rows="15" cols="40">
  </textarea><br />
  <input type="submit" />
</form>

Basically the form asks the visitor for his email address (the field named "email" found in input
name="email" above) and message (the field named "message" found in textarea
name="message"), and presents him with a button which he can click to submit the contents of
the form. When the form is submitted, it is "posted" (see the "method" attribute of the <form>
tag) to a script named "sendmail.php" (also specified in the <form> tag).

The Feedback Form PHP Script
Now all that remains is to code "sendmail.php". This is made extremely easy by the facilities
available in PHP. Type the following code into a file named "sendmail.php". Do not put anything
else into that file, ie, don't put in any other HTML tags or headers, etc.

<?php
  $email = $_REQUEST['email'] ;
  $message = $_REQUEST['message'] ;

     mail( "yourname@example.com", "Feedback Form Results",
       $message, "From: $email" );
     header( "Location: http://www.example.com/thankyou.html" );
?>

When the form is submitted to sendmail.php, the contents of the "email" field in the form is put
into a PHP variable called $_REQUEST['email']. Likewise the contents of the "message" field is
put into the variable $_REQUEST['message'].

If you had named the fields in your form "emailofsender" and "contentsofmessage", then the
information submitted in those fields would have been available to your script in the variables
$_REQUEST['emailofsender'] and $_REQUEST['contentsofmessage'] respectively. I think you
get the idea.

The first thing we do in our PHP script is to make the information that is submitted easily
accessible to the rest of the program.

Firstly, we made a copy of the contents of $_REQUEST['email'] in a variable we call $email.
This was done in the line

$email = $_REQUEST['email'] ;
Note that we don't really have to call this new variable $email. We could have called it
$thingamajig if we wished, but it makes sense to name a variable with some meaningful name.

Likewise, in the next line, we made a copy (assigned) of $_REQUEST['message'] in a variable
$message.

$message = $_REQUEST['message'] ;

Again, we could have named the new variable anything we wanted — but it's easier for us to
understand the program if the variable name reflects what it does.

The real workhorse of this script is in the line beginning with "mail".

mail( "yourname@example.com", "Feedback Form Results",
  $message, "From: $email" );

mail  is a special function in PHP that sends mail. The first parameter to mail is supposed to
contain the email address you want the form contents to be sent to, such as your own email
address. The second parameter is the "Subject" of the email message. The last two parameters are
the content of the message and the headers you want sent, respectively. We want a "From"
header so that we know who is sending the email to us and can reply to him/her if we need to.

Notice that, like many other programming languages, strings (sequences of characters) are
enclosed in double quotes, such as "Feedback Form Results".

Variables like $message can be used as-is. Note also that you can also interpolate (introduce) the
contents of the variable $email into a string, like "From: $email", so that if your $email string
contained an address like "william@shakespeare.com", the final string that is passed to the mail
function will be "From: william@shakespeare.com".

You can also use single quotes (such as those in 'Hi there') to quote strings, but when you do
so, the variables included are not expanded. This is useful if, for some reason, you really want to
pass the string 'From: $email' to mail without PHP translating that to "From:
william@shakespeare.com".

Finally, it is appropriate to thank the visitor for his message. This is done with the line

header( "Location: http://www.example.com/thankyou.html" );

This line causes PHP to send an HTTP header back to the visitor's browser telling it to load the
URL "http://www.example.com/thankyou.html". The "header" function allows us to send any
HTTP header to the browser.

You will of course have to create such a file called "thankyou.html" with some sort of message
to thank your visitor for his efforts, otherwise your visitor will be greeted with an unfriendly
"404 File Not Found" error after he sends his message. You should also replace the URLs and
email addresses with the correct ones if you want to use that script on your site.

By the way, the script has to be enclosed within the "<?php" and "?>" tags because the PHP
processor treats all input as HTML code unless otherwise specified. On most systems, you can
simply use "<?" and "?>" as the opening and closing tags to get the script to work, however if
you want to be sure that your script will work on all systems, you should use the full "<?php"
form for the opening tag.

Conclusion
Easy wasn't it? In just a few lines, you've written your first PHP script. And it's not some trivial
and useless script — it is actually a working, usable program.

However, before you put the script "live" on the Internet, you should check out the following
additional tutorials:

      In How to Add Form Validation, Disable Browser Caching and Embed HTML with PHP,
       you will develop the script so that your visitor's input is checked to catch instances where
       someone accidentally clicks the "Submit" button before they fill in their email address.
       You will also learn how to integrate both the form and the script into a single
       "feedback.php" file if you wish.
      The article How to Prevent Email Injection in Your PHP Form to Mail Scripts deals with
       the security aspects of putting a PHP form to mail script "live" on the Internet, where
       spammers can abuse it and hijack it to send spam to others.

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:114
posted:7/1/2012
language:
pages:21