Thursday, April 2, 2009

[Transferred]Configuring PHP with MySQL for Apache 2 or IIS in Windows

from Get it Done with MySQL 5 by Peter Brawley and Arthur Fuller

If you are installing PHP under IIS, skip to Installing PHP. If you have not yet installed Apache 2, first follow this walkthrough.

Preparing Apache 2 for PHP

Your Apache installation folder under Windows is something like c:\apache. Call it [APACHEDIR]. The Apache configuration file is [APACHEDIR]\Conf\httpd.conf. Edit it with any text editor, for example TextPad. To change the default DocumentRoot folder, where Apache looks for Web files to serve, set DocumentRoot in htppd.conf, for example

    DocumentRoot "c:/web/htdocs"


There are two more tweaks, for running PHP under Apache and for error logging. Within your text editor, search for DirectoryIndex:



    DirectoryIndex index.html index.html.var # index.php


To have Apache serve PHP pages, remove the comment character (#). You should also permit the use of .htaccess files in any directory: search for an AllowOverride setting and change it from None to All. Save the changes made so far, and leave the file open in the text editor; more changes are needed once PHP is installed.



If a setting triggers an error, the error may log to [APACHEDIR]\apache2\logs\error.log but more likely will log only to the Windows Event log (Start | Settings | Control Panel | Administrative Tools | Event Viewer), which is not a convenient tool for debugging installation settings. It is much easier to test Apache 2 in a command line window, where error reports appear instantaneously. So open a command line window, navigate to C:\Apache\Apache2\bin, and make sure you can start and stop Apache from there:



    httpd -k start 
httpd -k stop

In Apache 2.0 the command is apache; in 2.2 it is httpd.

Installing PHP


The PHP-MySQL combination is no picnic to install in Windows. The rules change from version to version of Windows, PHP and MySQL, and those rules often have been documented inaccurately. Variations abound.



First, Windows can run PHP programs in two main ways: via a CGI interface (eg via php.exe), or internally and faster using PHP DLLs, so for many PHP releases there have been two Windows binaries, a smaller .msi package which installs the CGI executable php.exe and is missing DLLs needed to run under Apache (so you have to download them from http://snaps.php.net), and a more complete .zip package which includes those DLLs.



Second, the web server makes a huge difference. PHP installations under Apache differ radically from PHP installations under Windows Internet Information Services (IIS), and installations under IIS versions 4, 5, 6 and 7 differ significantly from one another.



Third, MySQL may be installed on the machine serving PHP scripts, or on another machine on the LAN.



Fourth, there are two PHP APIs for MySQL, mysql and mysqli, where i means "improved". The main mysqli improvements are support for O-O syntax and multiple stored procedure calls. But those features come at a price: mysqli syntax is very different, so refactoring code for mysqli can be painful, and the mysqli interface is pickier, so for example determining whether a column is a key requires bit twiddling.



Fifth, in late 2005 MySQL began releasing its own builds of libmysql.dll, php_mysql.dll and php_mysqli.dll as Connector/PHP. MySQL strongly recommended that developers running PHP under Windows use Connector/PHP rather than the DLLs which ship with PHP. But in March 2008, MySQL removed the Connector/PHP download page without comment. In response to an email inquiry, a MySQL spokesman said Connector/PHP is no longer required.



To be on the safe side, before proceeding remove all traces of previous PHP installations, including php*.* from %windir% and %windir%\system32.



Then:



For Apache:



Take the PHP package you want from http://www.php.net/downloads.php and unpack it to %HOMEDRIVE%\php, preserving paths.



For IIS 4: Upgrade IIS to version 5 then follow IIS 5 options below.



For PHP 4.4 and IIS 5: Download and run http://www.iis-aid.com/iis_aid_php_installer.



For PHP 5 and IIS 5



Take the latest zip package and thread-safe PECL binaries package (PHP extensions) from http://www.php.net/downloads.php.



Create a PHP installation folder, for example c:\php.



If you are using Windows Vista, turn off User Account Control (UAC) as described here before installing PHP to prevent files being deposited in the wrong folder.



As explained above, it is no longer necessary (or indeed possible) to download MySQL's own mysql and mysqli extension zips from http://dev.mysql. com/downloads/connector/php



Unpack the PECL package into the newly created PHP subdirectory ext, preserving paths.



For PHP 5 and IIS 6 under Windows Server 2003 see here.



For PHP 5 and IIS 7:



See this tutorial. There are also installation tutorials for Windows Server 2008 at http://www.youtube.com/watch?v=5tw1MBtuVmQ and http://www.youtube.com/watch?v=5lKLXelnz-k&feature=related.



Here we refer to your PHP installation folder as [PHPDIR]. To test your PHP installation, open a command window over PHPDIR and type:



  php -v



If it runs without error, PHP is installed.  



The PHP manual is available for download in many languages and formats at http://www.php.net/docs.php.



If you are installing PHP under IIS, skip to Installing PHP and MySQL under IIS.



Configuring PHP and MySQL under Apache 2


1. Set PHP path: In My Computer | Properties | Advanced, ensure that [PHPDIR] is in the PATH. If you are installing PHP 4, move all DLLs from both [PHPDIR]\dlls and [PHPDIR]\sapi to [PHPDIR]. For PHP 5, that's not necessary---the DLLs are already in [PHPDIR].



2. Configure Apache 2 for PHP: In the text editor window where Httpd.conf is open, look for the section that contains a list of AddType commands, and add this one:



    AddType application/x-httpd-php .php


For PHP 4, in the LoadModule section of Httpd.conf, uncomment or add in:



    LoadModule php4_module "c:/php/php4apache2.dll"


For PHP 5 it should be:



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


For PHP 5.2 it should be:



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


3. Set up php.ini: Make a copy of [PHPDIR]\php.ini-recommended, name it php.ini, and copy it to [APACHEDIR]\bin or to %WINDIR% so Apache can find it. Open it in your text editor. Add or edit the first two settings shown below. If you are installing PHP 5, comment out the third setting, otherwise the fourth:



    doc_root = "c:\apache\htdocs"            ; MUST BE THE SAME AS IN Httpd.conf 
session.save_path = "c:/temp" ; DIRECTORY MUST EXIST
extension_dir = "c:\php\extensions" ; FOR PHP 4 ONLY
extension_dir = "c:\php\ext" ; FOR PHP 5 ONLY


Create c:\temp if it does not exist. The PHP manual says these path arguments need trailing backslashes, but since PHP 4.3 that is not the case.



4. Apache 2, PHP and MySQL: Copy libmysql.dll from [PHPDIR] to [APACHEDIR]\bin, and make sure php_mysql.dll and php_mysqli.dll are in extension_dir as set in Step 3. Some older Windows installations are missing msvcr71.dll, which Connector/PHP needs. If Windows cannot find it in %WINDIR%, Google for it and unpack it into %WINDIR%\system32. Then in php.ini, uncomment or add:



    extension=php_mysql.dll
extension=php_mysqli.dll


The default MySQL host is localhost. If MySQL is on another machine on your LAN, set the default MySQL host:



    mysql.default_host = "networkmysqlservername"


5. CGI mode: If you need to run PHP programs in CGI mode, add to Httpd.conf:



    ScriptAlias /php/ "c:/php/" 
Action application/x-httpd-php"/php/php.exe"


6. Test: Save all changes and reboot the machine. To test Apache alone, execute



    httpd -k start


in [APACHEDIR]\bin. If there is an error, Apache will report it immediately in this window. If you prefer, run it from Control Panel | Services | Apache and check [APACHEDIR]\logs\errors  for errors.



To ensure that PHP can connect to MySQL, use a text editor to save this code



<?php

mysql_connect('HOST','USR','PWD') or die(mysql_error());


?>



substituting your host, username and password values to connect.php in [PHPDIR], open a command window over [PHPDIR], and type:



php connect.php



If it fails, redo Steps 1 through 5.



To test Apache's serving of PHP pages and PHP's connection to MySQL under Apache, use your text editor to create a simple PHP page, called phptest.php, containing just this line:



    <?php phpinfo(); ?>


Save to the Apache DocumentRoot folder as phptest.php.



Run it by browsing http://localhost/phptest.php. If everything is working correctly, you will see a page with the PHP logo and a long list of settings. Whether PHP is running through CGI or inside Apache is indicated by the environment variable script_name. If PHP is running through CGI, this variable is set to /Php/Php.exe. If Apache is running the PHP script directly, this variable will contain the name of your script, /Phptest.php. In either case, if PHP can connect to MySQL then the page shows a MySQL section. If MySQL and MySQLi sections do not appear on the page, check that the setting labelled Loaded Configuration File in the top section references the php.ini you prepared for this installation.



Configuring PHP and MySQL under IIS


IIS is in %HOMEDRIVE%\Inetpub. The web folder is %HOMEDRIVE%\Inetpub\wwwroot



If you installed PHP 5 under IIS 7 as described here, or PHP 5 and IIS 6 under Windows Server 2003 as described here, skip to Step 6.



If you are installing PHP 4.4 and ran the PHP installer from iis-aid.com, skip to Step 3. 



Otherwise ...



1. Set environment variables: In My Computer | Properties | Advanced | Environment Variables | System variables, scroll down to path, click Edit, add the [PHPDIR] spec and click OK. Under System variables click New, add a variable called PHPRC, and set it to the [PHPDIR] spec.



2. Install PHP application mapping: Copy David Wang's Visual Basic Script from http://blogs.msdn.com/ david.wang/archive/2004/12/02/273681.aspx, save it in a convenient folder as chglist.vbs, and run this command from that folder:



    chglist.vbs W3SVC/ScriptMaps "" ".php,[PHPDIR]\php5isapi.dll,5" /INSERT /COMMIT


substituting for [PHPDIR] your PHP installation path. In My Computer | Manage | Services and Applications | Internet Information Service | Web Sites | Properties | Home Directory | Configuration, use the Add or Edit button to ensure that there is an entry for which Extension is .php and Executable Path is [PHPDIR]\ext\php5isapi.dll (eg c:\php\ext\php5isapi.dll).



For IIS 6.0, you must also configure a Web extension. In My Computer | Manage | Services and Applications | Internet Information Service | SERVER | Web Service Extensions, click on Add a new Web service extension, enter a name for Extension Name, set extension Status to Allowed, and in the Add File applet click on Browse, navigate to [PHPDIR], and select php5isapi.dll.



3. DLLs: Make sure libmysql.dll is in [PHPDIR], and that php_mysql.dll and php_mysqli.dll are in [PHPDIR]\ext. Some older Windows installations are missing msvcr71.dll, which Connector/PHP needs. If Windows cannot find it in %WINDIR%, Google for it and unpack it into %WINDIR%\system32.



4. Set up php.ini to talk to MySQL: In [PHPDIR] make a copy of php.ini-recommended, name it php.ini, open it in your text editor, and touch up these entries:



    extension_dir="c:\php\ext" ; or whatever yours is
extension=php_mysql.dll
extension=php_mysqli.dll


The default MySQL host is localhost. If MySQL lives on another machine on your LAN, set the default MySQL host:



    mysql.default_host = "your_network_mysql_server_name" 


5. Under Windows Vista restore UAC as described here.



6. Test: Reboot the machine, or in My Computer | Manage | Services and Applications | Internet Information Service | All Tasks, click Restart. Write Inetpub\wwwroot\test.php that just says



    <?php phpinfo(); ?>


browse http://localhost/test.php, and scroll down the page to see the MySQL entries.



[Latest rev: 16 Jul 2008]



 



From: http://www.artfulsoftware.com/php_mysql_win.html

No comments:

Post a Comment

Google+