Getting PHP and MySQL to work correctly together under Windows is an argument for using Linux. If Linux isn’t an option for you, here is how to minimize the pain.
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
There are two more tweaks, for running PHP under Apache and for error logging. Within your text editor, search for
DirectoryIndex index.html index.html.var # index.php
To have Apache serve PHP pages, remove the comment character (#) in that line. 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
If you prefer, right click on the desktop My Computer icon, select Manage, open Services and Management, click on Services, right click on Apache, and select Start.
Installing PHP (read carefully)
PHP installation files and rules vary with the web server, the version of PHP, and the version of MySQL:
1. The web server: The web server makes a huge difference. Apache is more reliable than Windows Internet Information Services (IIS). PHP installations under Apache differ radically from PHP installations under IIS. Installations under IIS versions 4, 5, 6 and 7 differ significantly from one another.
2. PHP and PHP-CGI: Generally, 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.
As of PHP 5.3, if you are running IIS then this choice has been taken out of your hands: Under IIS, PHP 5.3 and later run only under FastCGI.
3. Where MySQL is installed makes a difference. It may be installed on the machine serving PHP scripts, or on another machine on the LAN.
4. Now you see it, now you don’t: In late 2005 MySQL began releasing its own builds of the required client library libmysql.dll, and the two PHP-MySQL API libraries 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.
In March 2008 MySQL reversed itself, and removed the Connector/PHP download page without comment. In response to an email inquiry, a MySQL spokesman said Connector/PHP is “no longer required”. Much later, a page appeared at the MySQL site to explain that Connector/PHP “is no longer necessary”.
So forget Connector/PHP.
5. Libmysql and mysqlnd: As of PHP 5.3, libmysql.dll disappeared from the PHP package! PHP 5.3 includes a built-in replacement called mysqlnd. Contrary to earlier suggestions on the MySQL site, there is no mysqlnd download—it’s built into PHP 5.3. Now the mysqlnd page (http://dev.mysql.com/downloads/connector/php-mysqlnd/) says “as of PHP 5.3 you can use ext/mysql, ext/mysqli and PDO_MYSQL either together with libmysql as you did in the past or with mysqlnd. We have no plans to remove libmysql support from ext/mysql, ext/mysqli or PDO_MYSQL.” Plans or not, they’ve done it—libmysql.dll does not exist for PHP 5.3, and earleir libmysql.dll versions do not work with with PHP 5.3.
But mysqlnd is not entirely reliable in some contexts, and cannot connect to MySQL 4.1 or later servers using the older authentication protocol. If you don’t want to live with mysqlnd problems or work around them, naturally you’ll want to disable mysqlnd and use libmysql.dll. With PHP 5.3 you can’t unless you’re willing to recompile PHP from source with a
--disable-mysqlnd flag! So if mysqlnd problems affect you, until they’re fixed we suggest you use PHP 5.2 instead of PHP 5.3.
6. API libraries: there are two main 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.
Because of all these variations and inconsistencies, before installing any version of PHP you should remove all traces of previous PHP installations, including all PHP-related files in %windir% and %windir%\system32.
MSI installer packages are generally reliable, but PHP installation under Windows is not failsafe, so best practice is to take the PHP .zip you want from http://windows.php.net/download/. That page now advises that to run PHP under Apache, you need a VC6 build (built with the legacy Visual Studio compiler). Unpack it to %HOMEDRIVE%\php, preserving paths.
For packages up to 5.2x, if the package does not include libmysql.dll, use the copy of libmysql.dll in the bin folder of your MySQL installation. From PHP 5.3 on, PHP cannot use libmysql.dll.
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:
MSI installer packages are generally reliable, but PHP installation under Windows is not failsafe, so best practice is to take the PHP .zip you want from http://windows.php.net/download/. That page now advises that to run PHP under IIS, you need a VC9 build (built with the Visual 2008 compiler). Unpack it to %HOMEDRIVE%\php, preserving paths. Unless the PHP version is 5.3 or later, if the package turns out not to include libmysql.dll, download an earlier .zip.
Create a PHP installation folder with no spaces in its name, 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 or later:
# 127.0.0.1 localhost
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 now to Installing PHP and MySQL under IIS.
Configure 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
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 and later 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 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: Up to and including PHP 5.2x: if there is a copy of libmysql.dll in [PHPDIR], copy it to [APACHEDIR]\bin, otherwise copy libmysql.dll from the bin folder of your MySQL installation. For PHP 5.3 and later: remove any copy of libmysql.dll from [APACHEDIR]\bin.
Then ensure that 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 PHP may need. If Windows cannot find it in %WINDIR%, Google for it and unpack it into %WINDIR%\system32.
Then in php.ini, uncomment or add:
If you will be using the pdo_mysql interface library (http://forge.mysql.com/wiki/PHP_PDO_MYSQLND), also ensure that php_pdo.dll and php_pdo_mysql.dll are in
extension_dir as set in Step 3, and in php.ini uncomment or add:
The default MySQL host is
localhost. If MySQL is on another machine on your LAN, set the default MySQL host:
mysql.default_host = "networkmysqlservername"
Be sure to make these changes to the copies of php.ini in both [PHPDIR] and in [APACHEDIR]\bin!
To see the list of modules you have installed for PHP, from [PHPDIR] issue:
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
[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 via php_mysql.dll, use a text editor to save this code
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:
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.
IIS is in %HOMEDRIVE%\Inetpub. The web folder is %HOMEDRIVE%\Inetpub\wwwroot
If you are installing PHP 4.4 and ran the PHP installer from iis-aid.com, skip to Step 3.
For PHP 5 through 5.2 and IIS 5 and 6, follow this walkthrough, then skip to step 6.
For PHP 5.3 and later under IIS 5.1 and 6.0, follow this walkthrough, or this one, then skip to Step 6.
Otherwise for PHP earlier than 5.3 under IIS 5 and 6 …
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: Download David Wang’s Visual Basic Script from http://www.iis-aid.com/files/chglist.zip, unpack chglist.vbs to a folder in the path, then run this command:
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
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: 13 Jul 2011 - from http://www.artfulsoftware.com/php_mysql_win.html]
Read More on the Same Subject
- How to optimize images on a remote server using PHP
- [PHP] Searching a string inside another string or a text file
- Roo Web Development
- [PHP] Contact Form with Spam Check and Honeypot
- [PHP] Tiny WordPress