Build Your Own DatabaseDriven Website Using PHP amp;amp; MySQL [Electronic resources]

Kevin Yank

نسخه متنی -صفحه : 190/ 12
نمايش فراداده

Windows Installation

Installing MySQL

As I mentioned above, MySQL may be downloaded free of charge. Simply proceed to http://www.mysql.com/downloads/ and choose the recommended stable release (as of this writing, it is MySQL 3.23). On the MySQL 3.23 download page, under the heading of Windows downloads, click the Download link next to the latest version of MySQL (3.23.54 as of this writing). After downloading the file (it's about 13MB as of this writing), unzip it and run the setup.exe program contained therein.

Once installed, MySQL is ready to roll (barring a couple of configuration tasks that we'll look at shortly), except for one minor issue that only affects you if you're running Windows NT, 2000, XP, or .NET Server. If you use any of those operating systems, find a file called my-small.cnf in the directory to which you just installed MySQL. Copy it to the root of your C: drive and rename it to my.cnf. See the following sidebar if you have any trouble working with .cnf files on your Windows system.

Working with .cnf files in Windows

It just so happens that files ending in .cnf have a special meaning to Windows, so even if you have Windows configured to show file extensions, the my-small.cnf file will still appear as simply my-small with a special icon. Windows actually expects these files to contain SpeedDial links for Microsoft NetMeeting.

Assuming you don't use NetMeeting (or at least you don't use its SpeedDial facility) you can remove this file type from your system, enabling you to work with these files normally:

Open the Windows Registry Editor (in WinNT/2000/XP/.NET, click Start, Run..., and then type regedt32.exe to launch it, in Win9x/ME run regedit.exe instead).

Navigate to the HKEY_LOCAL_MACHINE\SOFTWARE\Classes branch of the registry, where you'll find a list of all the registered file types on the system.

Select the .cnf key and choose Edit, Delete from the menu to remove it.

Log out and log back in, or restart Windows for the change to take effect.

If you prefer not to mess with the file types on your system, however, you should still be able to open the files in Notepad to edit them and you can rename it to my.cnf by renaming the SpeedDial link icon to my (Windows will maintain the .cnf extension automatically).

If you don't like the idea of a MySQL configuration file sitting in the root of your C: drive, you can instead name it my.ini and put it in your Windows directory (e.g. D:\WINDOWS or D:\WINNT if Windows is installed on drive D:). Whichever you choose, open the file in Notepad and look for the following lines:

#basedir = d:/mysql/
#datadir = d:/mysql/data/

Uncomment these lines by removing the # symbol at the start, and change the paths to point to your MySQL installation directory, using slashes (/) instead of backslashes (\). For instance, I changed the lines on my system to read as follows:

basedir = d:/Program Files/MySQL/
datadir = d:/Program Files/MySQL/data/

With that change made, save the file and close Notepad. MySQL will now run on your Windows NT/2000/XP system! If you're using Windows 95/98/ME, this step is not necessary—MySQL will run just fine as-installed.

Just like your Web server, MySQL is a program that should be run in the background so that it may respond to requests for information at any time. The server program may be found in the bin subfolder of the folder into which you installed MySQL. To make things complicated, however, there are actually several versions of the MySQL server to choose from:

mysqld.exe This is the basic version of MySQL if you run Windows 95, 98, or ME. It includes support for all advanced features, and includes debug code to provide additional information in the case of a crash (if your system is set up to debug programs). As a result of this code, however, the server might run a little slow, and I've generally found that MySQL is so stable that crashes aren't really a concern.

mysqld-opt.exe This version of the server lacks a few of the advanced features of the basic server, and does not include the debug code. It's optimized to run quickly on today's processors. For beginners, the advanced features are not a big concern. You certainly won't be using them while you complete the tasks in this book. This is the version of choice for beginners running Windows 95, 98, or ME.

mysqld-nt.exe This version of the server is compiled and optimized like mysqld-opt, but is designed to run under Windows NT/2000/XP/.NET as a service. If you're using any of those operating systems, this is probably the server for you.

mysqld-max.exe This version is like mysqld-opt, but contains advanced features that support transactions.

mysqld-max-nt.exe This version's similar to mysqld-nt, but has advanced features that support transactions.

All these versions were installed for you in the bin directory. If you're running on Win98x/ME I recommend sticking with mysql-opt for now—move to mysqld-max if you ever need the advanced features. On Windows NT/2000/XP/.NET, mysqld-nt is my recommendation. Upgrade to mysqld-max-nt when you need more advanced features.

Starting MySQL is also a little different under WinNT/2000/XP/.NET, but this time let's start with the procedure for Win95/98/ME. Open an MS-DOS Command Prompt[2] and proceed to the MySQL bin directory, and run your chosen server program:

C:\mysql\bin>mysqld-opt

Don't be surprised when you receive another command prompt. This command launches the server program so that it runs in the background, even after you close the command prompt. If you press Ctrl-Alt-Del to pull up the task list, you should see the MySQL server listed as one of the tasks that's active on your system.

To ensure that the server is started whenever Windows starts, you might want to create a short cut to the program and put it in your Startup folder. This is just like creating a short cut to any other program on your system.

On WinNT/2000/XP/.NET, you must install MySQL as a system service. Fortunately, this is very easy to do. Simply open a Command Prompt (under Accessories in the Start Menu) and run your chosen server program with the --install option:

C:\mysql\bin>mysqld-nt --install
Service successfully installed.

This will install MySQL as a service that will be started the next time you reboot Windows. To manually start MySQL without having to reboot, just type this command (which can be run from any directory):

C:\>net start mysql
The MySQL service is starting.
The MySQL service was started successfully.

To verify that the MySQL server is running properly, press Ctrl-Alt-Del and open the Task List. If all is well, the server program should be listed on the Processes tab.

Installing PHP

The next step is to install PHP. At the time of this writing, PHP 4.x has become well-established as the version of choice; however, some old servers still use PHP 3.x (usually because nobody has bothered to update it). I'll cover the installation of PHP 4.3.0 here, so be aware that if you're still working with PHP 3.x there may be some differences.

Download PHP for free from http://www.php.net/downloads.php. You'll want the Windows Binaries package, and be sure to grab the version that includes both the CGI binary and the server API versions if you have a choice.

In addition to PHP itself, you will need a Web server such as Internet Information Services (IIS), Apache, Sambar or OmniHTTPD. PHP was designed to run as a plug-in for existing Web server software. To test dynamic Web pages with PHP, you'll need to equip your own computer with Web server software, so that PHP has something to plug into. If you have Windows 2000, XP Professional[3], or .NET Server, then install IIS (if it's not already on your system): open Control Panel, Add/Remove Programs, Add/Remove Windows Components, and select IIS from the list of components. If you're not lucky enough to have IIS at your disposal[4], you can instead use a free 3rd party Web server like Apache. I'll give instructions for both options in detail.

First, whether you have IIS or not, complete these steps:

Unzip the file you downloaded into a directory of your choice. I recommend C:\PHP and will refer to this directory from here onward, but feel free to choose another directory if you like.

Find the file called php4ts.dll in the PHP folder and copy it to the System32 subfolder of your Windows folder (e.g. C:\Windows\System32).

Find the file called php.ini-dist in the PHP folder and copy it to your Windows folder. Once there, rename it to php.ini.

Open the php.ini file in your favourite text editor (use WordPad if Notepad doesn't display the file properly). It's a large file with a lot of confusing options, but look for a line that begins with extension_dir and set it so that it points to your PHP folder:

extension_dir = C:\PHP

A little further down, look for a line that starts with session.save_path and set it to your Windows TEMP folder:

session.save_path = C:\WINDOWS\TEMP

Save the changes you made and close your text editor.

Now, if you have IIS, follow these instructions:

In the Windows Control Panel, open Administrative Tools, Internet Information Services.

In the tree view, expand the entry labelled local computer, then under Web Sites look for Default Web Site (unless you have virtual hosts set up, in which case, choose the site you want to add PHP support to). Right-click on the site and choose Properties.

Click the ISAPI Filters tab, and click Add.... In the Filter Name field, type PHP, and in the Executable field, browse for the file called php4isapi.dll in the sapi subfolder of your PHP folder (e.g. C:\PHP\sapi\php4isapi.dll). Click OK.

Click the Home Directory tab, and click the Configuration... button. On the Mappings tab click Add. Again choose your php4isapi.dll file as the executable and type .php in the extension box (including the '.'). Leave everything else unchanged and click OK. If you want your Web server to treat other file extensions as PHP files (.php3, .php4, and .l are common choices), repeat this step for each extension. Click OK to close the Application Configuration window.

Click the Documents tab, and click the Add... button. Type index.php as the Default Document Name and click OK. This will ensure that a file called index.php will be displayed as the default document in a given folder on your site. You may also want to add entries for index.php3 and index.l.

Click OK to close the Web Site Properties window. Close the Internet Information Services window.

Again, in the Control Panel under Administrative Tools, open Services. Look for the World Wide Web Publishing service near the bottom of the list. Right-click on it and choose Restart to restart IIS with the new configuration options. Close the Services window.

You're done! PHP is installed!

If you don't have IIS, you'll first need to install some other Web server. For our purposes I'll assume you have downloaded and installed Apache server from http://httpd.apache.org/; however, PHP can also be installed on Sambar Server, OmniHTTPD, and others. I recommend Apache 1.3 for now, but if you want to use Apache 2.0, be sure to read the following sidebar.

PHP and Apache 2.x in Windows

As of this writing, the PHP team continues to insist that support for running PHP on Apache 2.0 is experimental only. There are a number of bugs that arise within PHP when it is run on an Apache 2.0 server, and on Windows especially, installation can be problematic. That said, many people are running PHP on Apache 2.0 quite successfully, and the bugs that do exist probably won't affect you if you're just setting up a low-traffic testing server.

The instructions below apply to both Apache 1.3 and Apache 2.0; however, it is possible that after configuring Apache 2.0 to use PHP, the server will fail to start. It is also possible that it will start, but that it will fail to process PHP scripts. In both cases, an error message should appear when you start Apache and/or in the Apache error log file.

This problem is caused by the fact that Apache 2.0 is a server still very much under development. With each minor release they put out, they tend to break compatibility with all server plug-in modules (such as PHP) that were compiled to work with the previous version. On Unix, this isn't such a big deal because people tend to compile PHP for themselves, so they simply re-compile PHP at the same time they're compiling the new release of Apache and PHP adapts accordingly. Unfortunately, on Windows, where people are used to simply downloading pre-compiled files, the situation is different.

The php4apache2.dll file that is distributed with PHP will only work on versions of Apache 2.0 up to the one that was current at the time that version of PHP was released. So if you run into problems, the version of PHP you're using is probably older than the version of Apache you're using. This problem can often be fixed by downloading the very latest version of PHP; however, every time a new release of Apache 2.0 comes out, the current release of PHP will be incompatible until they get around to updating it.

Should you ever install a later version of Apache and break compatibility with the latest PHP build, you should be able to download a 'work-in-progress' version of PHP and grab just the files you need (those responsible for the PHP-Apache interface). Information about doing this can be found in the PHP bug database.

Once you've downloaded and installed Apache according to the instructions included with it, open http://localhost/ in your Web browser, to make sure it works properly. If you don't see a Web page explaining that Apache was successfully installed, then either you haven't run Apache yet, or your installation is faulty. Check the documentation and make sure Apache is running properly before you install PHP.

If you've made sure Apache is up and running, you can add PHP support:

On your Start Menu, choose Programs, Apache HTTP Server, Configure Apache Server, Edit Configuration. This will open the httpd.conf file in Notepad.

All of the options in this long and intimidating configuration file should have been set up correctly by the Apache install program. All you need to do is add the following three lines to the very bottom of the file:

LoadModule php4_module c:/php/sapi/php4apache.dll
AddType application/x-httpd-php .php .php3 .l
AddType application/x-httpd-php-source .phps

Make sure the LoadModule line points to the appropriate file in the PHP installation directory on your system, and note the use of slashes (/) instead of backslashes (\).

Important

If you're using Apache 2.0 or later, the LoadModule line needs to point to php4apache2.dll instead of php4apache.dll.

Next, look for the line that begins with DirectoryIndex. This line tells Apache what file names to use when it looks for the default page for a given directory. You'll see the usual indexl and so forth, but you need to add index.php, index.php3, and index.l to that list if they're not there already:

DirectoryIndex indexl ... index.php index.php3 index.l

Save your changes and close Notepad.

Restart Apache by choosing Programs, Apache HTTP Server, Control Apache Server, Restart on the Start menu (or type NET STOP Apache && NET START Apache at the command prompt). If all is well, Apache will start up again without complaint.

You're done! PHP is installed!

With MySQL and PHP installed, you're ready to proceed to "Post-Installation Setup Tasks".

[2]If you're unfamiliar with the workings of the Command Prompt, check out my article Kev's Command Prompt Cheat Sheet to get familiar with how it works before you proceed further.

[3]Windows XP Home Edition does not come with IIS.

[4]A feature-limited edition of IIS called “Personal Web Server” (PWS) was distributed on the Windows 98 Second Edition CD, and was available for earlier editions of Windows as well. While PHP can technically run on PWS, this Web server is somewhat unstable and has a great many known security holes. For these reasons, I highly recommend using Apache if an up-to-date version of IIS is not available for your Windows operating system.