Article

Build Your Own Database Driven Web Site Using PHP & MySQL, Part 1: Installation

Page: 1 2 3 4 5 6 7 Next

Installing Individual Packages

Installing each individual package separately is really the way to go if you can afford to take the time. That way you learn how all the pieces fit together, but have the freedom to update each of the packages independently of the others. Ultimately, it’s always worthwhile becoming familiar with the inner workings of any software with which you’ll be spending a lot of time.
Installing MySQL

As I mentioned above, you can download MySQL free of charge. Simply proceed to the MySQL Downloads page and click the Download link for the free MySQL Community Server. This will take you to a page with a long list of download links for the current recommended version of MySQL (as of this writing, it’s MySQL 5.1).

At the top of the list you’ll see links for Windows and Windows x64. If you’re positive you’re running a 64-bit version of Windows, go ahead and follow the Windows x64 link to download the Windows Essentials (AMD64 / Intel EM64T) package (about 28MB in size). If you know you’re running a 32-bit version of Windows, or if you’re at all unsure, follow the Windows link and download the Windows Essentials (x86) package (about 35MB)—it’ll work even if it turns out you’re running a 64-bit version of Windows. Although a little obscure, the Pick a mirror link shown below is the one you need to click to download the file.

Finding the right link can be tricky -- here it is!

Once you’ve downloaded the file, double-click it and go through the installation as you would for any other program. Choose the Typical option when prompted for the setup type, unless you have a particular preference for the directory in which MySQL is installed. When you reach the end, you’ll be prompted to choose whether you want to Configure the MySQL Server now. Select this to launch the configuration wizard (In my testing, I found that the configuration wizard failed to actually launch automatically, even with this option checked. If you run into the same problem, just launch the MySQL Server Instance Config Wizard from the Start Menu after the installation has completed), and choose Detailed Configuration, which we’ll use to specify a number of options that are vital to ensuring compatibility with PHP. For each step in the wizard, select the options indicated here:

  1. Server Type

    Assuming you’re setting up MySQL for development purposes on your desktop computer, choose Developer Machine.


  2. Database Usage

    Unless you know for a fact that you will need support for transactions (as such support is usually superfluous for most PHP applications), choose Non-Transactional Database Only.


  3. Connection Limit

    Select Decision Support (DSS)/OLAP to optimize MySQL for a relatively modest number of connections.


  4. Networking Options

    Uncheck the Enable Strict Mode option to ensure MySQL’s compatibility with older PHP code that you might need to use in your own work.


  5. Default Character Set

    Select Best Support For Multilingualism to tell MySQL to assume you want to use UTF-8 encoded text, which supports the full range of characters that are in use on the Web today.


  6. Windows Options

    Allow MySQL to be installed as a Windows Service that's launched automatically; also select Include Bin Directory in Windows PATH to make it easier to run MySQL’s administration tools from the command prompt.


  7. Security Options

    Uncheck the Modify Security Settings option. It’s best to learn how to set the root password mentioned at this juncture without the assistance of the wizard, so I’ll show you how to do this yourself in the section called “Post-Installation Set-up Tasks”.

Once the wizard has completed, your system should now be fully equipped with a running MySQL server!

To verify that the MySQL server is running properly, type Ctrl+Alt+Del and choose the option to open the Task Manager. Click the Show processes from all users button unless it’s already selected. If all is well, the server program (mysqld.exe) should be listed on the Processes tab. It will also start up automatically whenever you restart your system.

Installing PHP

The next step is to install PHP. Head over to the PHP Downloads page and choose the PHP 5.2.x zip package under Windows Binaries; avoid the installer version, which is easier to install, but lacks the same flexibility attained by installing PHP manually.

Update for PHP 5.3: As of PHP 5.3, there are now four different versions of PHP to choose from. You want the one labelled VC6 x86 Thread Safe.

What about PHP 4?

At the time of writing, PHP 5 is firmly entrenched as the preferred version of PHP. For several years after PHP 5’s initial release, many developers chose to stick with PHP 4 due to its track record of stability and performance, and indeed today many bargain-basement web hosts have yet to upgrade to PHP 5. There’s no longer any excuse for this, however; PHP 5 is by far the better choice, and development of PHP 4 has been completely discontinued. If your web host is still living in the PHP 4 past, you’re better off finding a new web host!

PHP was designed to run as a plugin for existing web server software such as Apache or Internet Information Services, so before you can install PHP, you must first set up a web server.

Many versions of Windows come with Microsoft’s powerful Internet Information Services (IIS) web server, but not all do. Windows XP Home, Windows Vista Home, and Windows 7 Home Basic (among others) are without IIS, so you need to install your own web server on these versions of Windows if you want to develop database driven web sites. On top of that, assorted versions of Windows come with different versions of IIS, some of which vary dramatically in how you configure them to work with PHP.

With that in mind, if you’re still considering IIS, you should know it’s also relatively uncommon to host web sites built using PHP with IIS in the real world. It’s generally less expensive and more reliable to host PHP-powered sites on servers running some flavor of the Linux operating system, with the free Apache web server installed. About the only reason for hosting a PHP site on IIS is if your company has already invested in Windows servers to run applications built using ASP.NET (a Microsoft technology built into IIS), and you want to reuse that existing infrastructure to host a PHP application as well.

Although it’s by no means a requirement, it’s generally easiest to set up your development server to match the environment in which your web site will be deployed publicly as closely as possible. For this reason, I recommend using the Apache web server—even for development on a Windows computer. If you insist (or your boss insists) on hosting your PHP-based site using IIS, you will find the necessary installation instructions in the install.txt file contained in the PHP zip package you downloaded from the PHP web site.

If you need to install Apache on your computer, surf on over to The Apache HTTP Server Project and look for the version of Apache described as the best available (as of writing it’s version 2.2.11, as shown below).

The best available version -- accept no substitutes!

Once you get to the Download page, scroll down to find the links to the various versions available. The one you’ll want is Win32 Binary without crypto, shown below.

This is the one you need

Once the file has downloaded, double-click on it as usual to start the installation wizard. After a few steps, you’ll arrive at the Server Information screen.

If you were setting up a web server to be accessed publicly on the Web, the options on this screen would be important. For the purposes of setting up a development server, you can type whatever you like. If you know your computer’s network name, type that in for the Server Name. Feel free to put in your correct email address if, like me, you’re a stickler for the details. If you already have a web server running on your computer (for example, if you have also set up IIS to do some ASP.NET development on the same computer), you may need to select the only for the Current User, on Port 8080, when started Manually option on this screen, so as to avoid a conflict with the existing web server running on port 80.

On the next screen, choose the Typical option for the Setup Type, and follow the wizard from there to complete the installation. When it’s done, you should see a new icon for the Apache Service Monitor running in your System Tray. If you chose the default option to have Apache start up automatically, the status indicator should be green, as shown below.

The green light means Apache is up and running

Otherwise, you’ll need to start Apache manually as shown here before you can use it.

Choose Start to fire up Apache manually

You can also use the Apache Service Monitor icon to stop Apache running, once you’ve finished your web development work for the day.

When you have Apache up and running, open your web browser of choice and type http://localhost into the location bar. If you chose the option to run Apache on port 8080, you will need to type http://localhost:8080 instead. Hit Enter, and you should see a page like that shown below that confirms Apache is working correctly.

You can take my word for it!

With Apache standing on its own two feet, you can now install PHP. Follow these steps:

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

  2. Find the file called php.ini-dist in the PHP folder and make a duplicate copy of it. The easiest way to do it is to right-click and drag the file’s icon a short distance, drop it in the same Explorer window, and choose Copy Here from the pop-up menu. This will leave you with a new file named along the lines of php - Copy.ini-dist (depending on the version of Windows you’re using). Find this new file and rename it to php.ini. Windows will ask if you’re sure about changing the filename extension (from .ini-dist to .ini); click Yes.

    Windows Hides Known Filename Extensions by Default

    When you rename the file to php.ini, you might notice that the new filename that appears next to the icon is actually just php. If this happens, it’s because your copy of Windows is set up to hide the filename extension if it recognizes it. Since Windows knows that .ini files are Configuration Settings files, it hides this filename extension.

    As you can imagine, this feature can cause a certain amount of confusion. When you return to edit the php.ini file in the future, it would help to be able to see its full filename so you could tell it apart from the php.gif and php.exe files in the same folder.

    To switch off filename extension hiding, open the Windows Control Panel and search for Folder Options. Open the Folder Options window and switch to the View tab. Under Files and Folders, uncheck the Hide extensions for known file types checkbox, as shown below.

    Make filename extensions visible for all files

  3. Open the php.ini file in your favorite text editor. If you have no particular preference, just double-click the file to open it in Notepad. It’s a large file with a lot of confusing options, but look for the line that begins with doc_root (Notepad’s Edit > Find… feature will help). Out of the box, this line looks like this:

    doc_root =

    To the end of this line, add the path to your web server’s document root directory. For the Apache server, this is the htdocs folder in the main Apache web server directory. If you installed Apache in the default location, the path should be "C:\Program Files\Apache Software Foundation\Apache2.2\htdocs". If you installed it elsewhere, find the htdocs folder and type its path:

    doc_root = "C:\Program Files\Apache Software Foundation\Apache2.2\htdocs"

    Just a little further down in the file, look for the line that begins with extension_dir, and set it so that it points to the ext subfolder of your PHP folder:

    extension_dir = "C:\PHP\ext"

    Scroll further down in the file, and you’ll see a bunch of lines beginning with ;extension=. These are optional extensions to PHP, disabled by default. We want to enable the MySQL extension so that PHP can communicate with MySQL. To do this, remove the semicolon from the start of the php_mysqli.dll line:

    extension=php_mysqli.dll

    php_mysqli, not php_mysql

    Just above the line for php_mysqli.dll there is a line for php_mysql.dll. The i in php_mysqli stands for improved. You want to enable the new improved MySQL extension. The one without the i is obsolete, and some of its features are incompatible with current versions of MySQL.

    Keep scrolling even further down in the file, and look for a line that starts with ;session.save_path. Once again, remove the semicolon to enable this line, and set it to your Windows Temp folder:

    session.save_path = "C:\Windows\Temp"

    Save the changes you made and close your text editor.

That takes care of setting up PHP. Now you can set up your Apache server to use it as a plugin:

  1. Run Notepad as Administrator. This is necessary because the Apache configuration file, by default, can only be edited by an administrator. To do this, find the Notepad icon in your Start Menu (under All Programs > Accessories) and right-click on it. Click the Run as administrator menu item.

  2. Choose File > Open… in Notepad. Browse to the conf subfolder in your Apache installation folder (by default, C:\Program Files\Apache Software Foundation\Apache2.2\conf), and select the httpd.conf file located there. In order to make this file visible for selection, you’ll need to select All Files (*.*) from the file type drop-down menu at the bottom of the Open window.

  3. Look for the existing line in this file that begins with DirectoryIndex, shown here:

         <IfModule dir_module>  
           DirectoryIndex index.html  
         </IfModule>

    This line tells Apache which filenames to use when it looks for the default page for a given directory. Add index.php to the end of this line:

         <IfModule dir_module>  
           DirectoryIndex index.html index.php  
         </IfModule>

  4. All of the remaining 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 lines to the very end of the file:

         LoadModule php5_module "C:/PHP/php5apache2_2.dll"  
         AddType application/x-httpd-php .php  
         PHPIniDir "C:/PHP"

    Make sure the LoadModule and PHPIniDir lines point to your PHP installation directory, and note the use of forward slashes (/) instead of backslashes (\) in the paths.

    PHP and Future Apache Versions

    Historically, major new versions of the Apache server have required new versions of the .dll file you see referenced in the LoadModule line above. If you take another look in your PHP installation directory, for example, you’ll see there are also php5apache.dll and php5apache2.dll files there. These files were provided for use with Apache 1.3 and Apache 2.0, respectively.

    By the time you read this, it’s possible that Apache has undergone another major release (for instance, Apache 2.3), which might need yet another new .dll file. For example, Apache 2.3 might require you to use a new file named php5apache2_3.dll.

    If you are using a subsequent version of Apache, and if you do see a .dll file that looks like it might correspond to your Apache version, try adjusting the LoadModule line accordingly. You can always return and edit this file again later if Apache fails to load PHP correctly.

  5. Save your changes and close Notepad.
  6. Restart Apache using the Apache Service Monitor system tray icon. If all is well, Apache will start up again without complaint.
  7. Double-click the Apache Service Monitor icon to open the Apache Service Monitor window. If PHP is installed correctly, the status bar of this window should indicate the version of PHP you have installed, as shown in the figure below.
  8. Click OK to close the Apache Service Monitor window.

The PHP version number indicates Apache is configured to support PHP

With MySQL, Apache, and PHP installed, you’re ready to proceed to the section called “Post-Installation Set-up Tasks”.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links