Searching...
Monday 3 June 2013

Installing Apache and PHP on Windows

Apache php windows

Most developers prefer to develop applications on a local workstation/laptop or on a dedicated development server, you’ll probably need to know how to at least install and configure PHP and a web server (apache and ms IIS). Asking "Why should i learn to install php and a web server" is the same as asking "Why should a soldier need to load/handle a gun?". It is something you will never regret about.


In this lecture you will be guided to install apache and PHP on Windows Platform.


Necessary download links are given in the footer. Check it out.


DOWNLOADING APACHE

If you’re running Windows and would like to use Apache, then download the latest stable Windows binary located within the binaries/win32 directory. Two binary versions are available: one containing additional SSL-related capabilities and one lacking these capabilities. These binaries are appropriately named to make clear which is which. I suggest choosing the non-SSL capable version for development purposes.


DOWNLOADING PHP

Download the latest stable version by clicking on the Downloads link located at the top of the PHP web site and then choosing from one of the available distributions:

Source: In very special circumstances, the Windows binary will suit your needs just fine. This distribution is archived in bzip2 and gzip formats. Keep in mind that the contents are identical; the different compression formats are just there for your convenience.

Windows zip package: If you plan to use PHP in conjunction with Apache on Windows, you should download this distribution because it’s the focus of the later installation instructions.

Windows installer: This version offers a convenient Windows installer interface for installing and configuring PHP, plus support for automatically configuring the IIS, PWS, and Xitami servers. Although you could use this version in conjunction with Apache, I have not had much luck using it, and instead suggest downloading the Windows binary package.

After selecting a distribution, choose the download mirror closest to your location (recommended).


INSTALLING APACHE AND PHP ON WINDOWS PLATFORM

Apache 2 was completely rewritten to take advantage of Windows platform-specific features unlike it's previous versions. The installation process follows:

1. Start the Apache installer by double-clicking the apache_X.X.XX-win32-x86-no_ssl.msi icon. The Xs in this file name represent the latest stable version numbers of the distributions you downloaded in the previous section.

2. The installation process begins with a welcome screen. Click Next. The license agreement is displayed next. Carefully read through the license(lol). Assuming that you agree with the license stipulations, click Next.

3. A screen containing various items pertinent to the Apache server is displayed next. Take your time and click Next.

4. On the next screen you will be asked to enter network domain, the server name, and the administrator’s email address. If you know this information, fill it in now; otherwise, just enter 'localhost' for the first two items and put in any e-mail address for the last. You can always change this information later in the httpd.conf file.

5. Choose Typical or custom(choose yourself, you are smart enough). Click Next.

6. Consider changing the destination folder to C:\apache for the sake of conveneince. Click Next.

7. Click Install to complete the installation.

8. Now moving on to PHP installation. Unzip it to c:\php, avoid choosing a path that contains spaces

9. Navigate to C:\apache\conf and open httpd.conf and Add the following three lines below the block of LoadModule entries located in the bottom of the Global Environment section:

LoadModule php_module c:/php/php5apache2_2.dll
AddType application/x-httpd-php .php
PHPIniDir "c:\php"

10. Rename the php.ini-dist file in c:\php to php.ini and save it to the C:\php directory (as of PHP 5.3.0, the INI files have been reorganized and renamed as php.ini-development and php.ini-production, so if you are running 5.3+ you should rename one of these as php.ini depending upon your situation).

11. Now navigate to Start | Settings | Control Panel | Administrative Tools | Services. Locate Apache in the list and make sure that it is started otherwise Start the Service. If it is started, highlight the label and click Restart the Service so that the changes made to the httpd.conf file take effect. Next, right-click Apache and choose Properties. Ensure that the startup type is set to Automatic.


TESTING YOUR INSTALLATION

The best way to verify your PHP installation is by attempting to execute a PHP script. Open a text editor and add the following lines to a new file:

<?php
phpinfo();
?>

Save this file as phpinfo.php. If you’re running Apache, save it to the htdocs directory. If you’re running IIS, save the file to C:\inetpub\wwwroot. Now open a browser and access this file by entering the following URL: http://localhost/phpinfo.php

Please note that you cannot just open the script by navigating through your browser’s File | Open feature, because in doing so this script will not be passed through the web server and therefore will not be parsed.
If all goes well, you should see output similar to that shown in Figure.

Output from PHP’s phpinfo() function
Figure. Output from PHP’s phpinfo() function

If you’re attempting to run this script on a web hosting provider’s server, and you receive an error message stating phpinfo() has been disabled for security reasons, you’ll need to create another test script. Try executing this one instead, which should produce some simple output:

<?php
echo "A simple but effective PHP test!";
?>

If you are not seeing the appropriate output, it may be due to one or more of the following reasons:
  • If you manually configured Apache, changes to its configuration file do not take effect until Apache has been restarted. Therefore, be sure to restart Apache after adding the necessary PHP-specific lines to the httpd.conf file.
  • Invalid characters or incorrect statements will cause Apache’s restart attempt to fail. If Apache will not start, go back and review your changes.
  • Verify that any PHP-enabled file ends in the same PHP-specific extension as defined in the httpd.conf file. For example, if you've defined only .php as the recognizable extension, don’t try to embed PHP code in an .html file.
  • Make sure that you've delimited the PHP code within the file using the <?php and ?> constructs. Neglecting to do this will cause the code to output to the browser.
  • You've created a file named index.php and are trying unsuccessfully to call it as you would a default directory index file (done by just referencing a directory within the URL sans the specific file name you’d like to request, for instance http://www.example.com/about/ versus http://www.example.com/about/ index.php). However, Apache only recognizes index.html as the default directory index file. Therefore, you need to add index.php to Apache’s DirectoryIndex directive.

0 comments:

Post a Comment

 
Back to top!