Skip navigation links

MySQL Forums :: PHP :: Cannot get PHP and MySQL working


Advanced Search

Re: Cannot get PHP and MySQL working
Posted by: Peter Brawley ()
Date: February 16, 2006 03:58PM

Kevin,

Here are the instructions we use to get PHP & MySQL working together with Apache 2.

Preparing Apache 2 for PHP

The Apache configuration file is <APACHEDIR>\Apache2\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 things to tweak the Apache configuration file for PHP, and set up for error logging. Within your text editor, a search for a setting called DirectoryIndex will bring you to a line that looks like this:

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

To serve PHP pages, remove the comment character (#) so that the line reads:

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

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.exe from there:

apache -k start
apache -k stop

Installing PHP

Apache 2 can run PHP programs in two ways: via a CGI interface using php.exe, or internally and faster using PHP DLLs. So for every PHP release there are two Windows binaries, a smaller .msi package which installs the CGI executable php.exe and is missing the Apache DLLs (so you have to download them from http://snaps.php.net), and a more complete .zip package which includes those DLLs. After downloading that .zip, unzip it to c:\php if it is version 4.3.x, or to c:\ if it is version 5.x, preserving folder names.

In late 2005, MySQL began releasing its own builds of libmysql.dll, php_mysql.dll and php_mysqli.dll for the latest version of PHP 5, christening the packages Connector/PHP. If you have downloaded PHP 5 (not 4!), download these Connector/PHP zips from http://dev.mysql.com/downloads/connector/php/, and unzip them into the folder where you installed PHP 5, overwriting files of the same name.

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

Configuring Apache 2 to run PHP 4 or 5 with MySQL

Let the PHP installation directory be <PHP_INSTALL_DIR>; c:\php. is best.

1. If you are upgrading from PHP 3 to 4, or from 4 to 5: Stop Apache, rename the old PHP home directory to a descriptive name, e.g. php433, and move the old PHP DLLs out of %windir%\system32 to a backup directory, for example:

md \php433\winfilecopies
cd \php433\dlls
for %f in (*.dll) do move %windir%\system32\%f \php433\winfilecopies

Also move %windir%\php.ini to that winfilecopies backup directory. Make a <PHP_INSTALL_DIR>\includes directory, and copy your PHP include files there.

2. Set PHP path: In the Advanced Properties tab of My Computer, ensure that <PHP_INSTALL_DIR> is in the PATH. If you are installing PHP 4, move all DLLs from both <PHP_INSTALL_DIR>\dlls and <PHP_INSTALL_DIR\sapi to <PHP_INSTALL_DIR>. For PHP 5, that's not necessary—the DLLs are already in <PHP_INSTALL_DIR>.

3. 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"

4. Set up php.ini: Make a copy of <PHP_INSTALL_DIR>\php.ini-recommended, name it php.ini, and 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\apache2\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.

5. Apache 2, PHP and MySQL: PHP 4.3 for Windows has MySQL extensions linked in, so PHP 4.3 works with MySQL out of the box. PHP 5 needs Connector/PHP files. Also, some programs may still look for a file named libmysqli.dll even though it does not ship with PHP, so make a copy of libmysql.dll, name it libmysqli.dll, then copy libmysql*.dll to <APACHEDIR>\apache\bin. In php.ini, uncomment or add:

extension=php_mysql.dll
extension=php_mysqli.dll

6. CGI mode: If you need to run PHP programs in CGI mode (why!?), add to Httpd.conf:

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

Save all changes and reboot the machine. To find out whether Apache loads, enter this in a command-line window opened over <APACHEDIR>\apache2\bin:

apache –k start

If there is an error, Apache will report it immediately in this window. To ensure that Apache is serving PHP pages correctly, use your text editor to create a simple PHP page, called phptest.php, containing just this one line:

<?php phpinfo(); ?>

Save to the Apache DocumentRoot folder as phptest.php. To run it, you need only browse 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 orig_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.

Options: ReplyQuote


Subject Written By Posted
Cannot get PHP and MySQL working Kevin Chilton 02/15/2006 03:58PM
Re: Cannot get PHP and MySQL working Georg Richter 02/15/2006 11:54PM
Re: Cannot get PHP and MySQL working Peter Brawley 02/16/2006 03:58PM
Re: Cannot get PHP and MySQL working Marc B 02/26/2006 09:43AM
Re: Cannot get PHP and MySQL working Martin Bosticky 02/26/2006 11:28AM
Re: Cannot get PHP and MySQL working Martin Bosticky 02/26/2006 11:41AM
Re: Cannot get PHP and MySQL working Peter Brawley 02/27/2006 09:17AM
Re: Cannot get PHP and MySQL working puja sharma 03/22/2008 02:08PM
Re: Cannot get PHP and MySQL working Alex Pandre 02/28/2006 03:55PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.