Article

Home » Server-side Coding » PHP & MySQL Tutorials » Build Your Own Database Driven Web Site Using PHP & MySQL, Part 1: Installation
SitePoint Feature Article

About the Author

Kevin Yank

author_kev1 Kevin began developing for the Web in 1995 and is a highly respected technical author. He wrote Build your own Database Driven Website using PHP and MySQL, a practical step-by-step guide published by SitePoint, and he's co-author of the SitePoint Tech Times, a bi-weekly newsletter for technically-minded web developers. Kev believes that any good webmaster should have seen at least one episode of MacGyver.

View all articles by Kevin Yank...

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

By Kevin Yank

July 2nd, 2009

Reader Rating: 9

Page: 1 2 3 4 5 6 7 Next

In this book, I’ll guide you as you take your first steps beyond the static world of building web pages with pure HTML. Together, we’ll explore the world of database driven web sites and discover the dizzying array of dynamic tools, concepts, and possibilities that they open up. Whatever you do, don’t look down!

Okay, maybe you should look down. After all, that’s where the rest of this book is. But remember, you were warned!

Before you build your first dynamic web site, you must gather together the tools you’ll need for the job. In this chapter, I’ll show you how to download and set up the two software packages you’ll need. Can you guess what they are? I’ll give you a hint: their names feature prominently on the cover of this book! They are, of course, PHP and MySQL.

If you’re used to building web sites with HTML, CSS, and perhaps even a smattering of JavaScript, you’re probably used to uploading to another location the files that make up your site. Maybe this is a web hosting service that you’ve paid for; maybe it’s a free service provided by your Internet Service Provider (ISP); or maybe it’s a web server set up by the IT department of the company that you work for. In any case, once you copy your files to their destination, a software program called a web server is able to find and serve up copies of those files whenever they are requested by a web browser like Internet Explorer or Firefox. Common web server software programs you may have heard of include Apache and Internet Information Services (IIS).

PHP is a server-side scripting language. You can think of it as a plugin for your web server that enables it to do more than just send exact copies of the files that web browsers ask for. With PHP installed, your web server will be able to run little programs (called PHP scripts) that can do tasks like retrieve up-to-the-minute information from a database and use it to generate a web page on the fly before sending it to the browser that requested it. Much of this book will focus on writing PHP scripts to do exactly that. PHP is completely free to download and use.

For your PHP scripts to retrieve information from a database, you must first have a database. That’s where MySQL comes in. MySQL is a relational database management system, or RDBMS. We’ll discuss the exact role it plays and how it works later, but briefly it’s a software program that’s able to organize and manage many pieces of information efficiently while keeping track of how all of those pieces of information are related to each other. MySQL also makes that information really easy to access with server-side scripting languages like PHP. MySQL, like PHP, is completely free for most uses.

The goal of this first chapter is to set you up with a web server equipped with PHP and MySQL. I’ll provide step-by-step instructions that work on recent Windows, Mac OS X, and Linux computers, so no matter what flavor of computer you’re using, the instructions you need should be right here.

Your Own Web Server

If you’re lucky, your current web host’s web server already has PHP and MySQL installed. Most do—that’s one of the reasons why PHP and MySQL are so popular. If your web host is so equipped, the good news is that you’ll be able to publish your first database driven web site without having to shop for a web host that supports the right technologies.

The bad news is that you’re still going to need to install PHP and MySQL yourself. That’s because you need your own PHP-and-MySQL-equipped web server to test your database driven web site on before you publish it for all the world to see.

When developing static web sites, you can often load your HTML files directly from your hard disk into your browser to see how they look. There’s no web server software involved when you do this, which is fine, because web browsers can understand HTML code all by themselves.

When it comes to dynamic web sites built using PHP and MySQL, however, your web browser needs some help! Web browsers are unable to understand PHP scripts; rather, PHP scripts contain instructions for a PHP-savvy web server to execute in order to generate the HTML code that browsers can understand. So in addition to the web server that will host your site publicly, you also need your own private web server to use in the development of your site.

If you work for a company that has an especially helpful IT department, you may find that there’s already a development web server provided for you. The typical setup is that you must work on your site’s files on a network drive that’s hosted by an internal web server that can be safely used for development. When you’re ready to deploy the site to the public, your files are copied from that network drive to the public web server.

If you’re lucky enough to work in this kind of environment, you can skip most of this chapter. However, you’ll want to ask the IT boffins responsible for the development server the same questions I’ve covered in the section called “What to Ask Your Web Host”. That’s because you’ll need to have that critical information handy when you start using the PHP and MySQL support they’ve so helpfully provided.

Windows Installation

In this section, I’ll show you how to start running a PHP-and-MySQL-equipped web server on a Windows XP, Windows Vista, or Windows 7 computer. If you’re using an operating system other than Windows, you can safely skip this section.

All-in-one Installation

I normally recommend that you install and set up your web server, PHP, and MySQL individually, using the official installation packages for each. This is especially useful for beginners, because it gives you a strong sense of how these pieces all fit together. If you’re in a rush, however, or if you need to set up a temporary development environment to use just for a day or two, the following quick-and-dirty solution may be preferable.

You can skip ahead to the section called “Installing Individual Packages” if you want to take the time to install each piece of the puzzle separately.

WampServer (where Wamp stands for Windows, Apache, MySQL, and PHP) is a free, all-in-one program that includes built-in copies of recent versions of the Apache web server, PHP, and MySQL. Let me take you through the process of installing it:

  • Download the latest version from the WampServer web site. After downloading the file (as of this writing, WampServer 2.0g is about 16MB in size), double-click it to launch the installer, as shown in the figure below. The WampServer installer
  • The installer will prompt you for a location to install WampServer. The default of c:\wamp shown in Figure 1.2, “The default installation directory is a good choice” is an ideal choice for most purposes, but if you have strong feelings about where it’s installed, feel free to specify your preferred location. The default installation directory is a good choice
  • At the end of the installation, WampServer will ask you to choose your default browser. This is the web browser it will launch when you use the included system tray icon tool to launch your browser. If you have Firefox installed it will ask if you’d like to use it as your default browser. If you answer No, or have a different browser installed, it will ask you to select the executable file for the browser you want to use. As shown in Figure 1.3, “The default choice of Internet Explorer is fine”, it selects Internet Explorer (explorer.exe) for you, which is fine. If you’re using an alternative browser such as Safari or Opera, you can browse to find the .exe file for your browser if you want to. The default choice of Internet Explorer is fine
  • As WampServer is installed, it fires up its built-in copy of the Apache HTTP Server, a popular web server for PHP development. Windows will likely display a security alert at this point, like the one in Figure 1.4, “This security alert tells you Apache is doing its job”, since the web server attempts to start listening for browser requests from the outside world. This security alert tells you Apache is doing its job If you want to make absolutely sure that Apache rejects connections from the outside world, and that only a web browser running on your own computer can view web pages hosted on your development server, feel free to click Keep blocking. WampServer has its own built-in option to block connections from the outside world when you want to, however, so I recommend clicking Unblock in order to have the flexibility to grant access to your development server if and when you need to.
  • Next, as shown in Figure 1.5, “Fill in your Internet Service Provider’s SMTP server address if you know it”, the WampServer installer will prompt you for your SMTP server and email address. A PHP script can send an email message, and these settings tell it the outgoing email server, and the default “from” address to use. Type in your email address, and if you can remember your Internet Service Provider’s SMTP server address, type it in too. You can always leave the default value for the time being, though, and set it manually if and when you need to send email using a PHP script.

Fill in your Internet Service Provider’s SMTP server address if you know it

Once the installation is complete, you can fire up WampServer. An icon will appear in your Windows System Tray. Click on it to see the WampServer menu shown below.

The WampServer menu

By default, your server can only be accessed by web browsers running on your own computer. If you click the Put Online menu item, your server will become accessible to the outside world.

To test that WampServer is working properly, click the Localhost menu item at the top of the WampServer menu. Your web browser will open to display your server’s home page, shown below.

The home page provided by WampServer confirms Apache, PHP, and MySQL are installed

When you’re done working with WampServer, you can shut it down (along with its built-in servers) by right-clicking the System Tray icon and choosing Exit. When you’re next ready to do some work on a database driven web site, just fire it up again!

Later in this book, you’ll need to use some of the programs that come with the MySQL server built into WampServer. To work properly, these programs must be added to your Windows system path.

To add the MySQL command prompt programs that come with WampServer to your Windows system path, follow these instructions:

  1. Open the Windows Control Panel. Locate and double-click the System icon.

  2. Take the appropriate step for your version of Windows:
    • In Windows XP, switch to the Advanced tab of the System Properties window.
    • In Windows Vista or Windows 7, click the Advanced system settings link in the sidebar.

  3. Click the Environment Variables… button.

  4. In the list labeled User variables for user, look for a variable named PATH.
    • If it exists, select it and click the Edit… button.
    • If there’s no variable, click the New… button and fill in the Variable name by typing PATH.

  5. Add the path to WampServer’s MySQL bin directory (The exact path will depend on where you’ve installed WampServer and which version of MySQL it contains. On my system, the path is C:\wamp\bin\mysql\mysql5.1.34\bin. Use Explorer to take a look inside your WampServer installation’s files to figure out the exact path on your system.) as the Variable value:
    • If the Variable value is empty, just type in the path.
    • If there is already text in the Variable value field, add a semicolon (;) to the end of the value, then type the path after that.

  6. Click the OK button in each of the open windows to apply your changes.

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