Tuesday, August 11, 2009

Setting Up Your Work Environment

fter you decide to use PHP and MySQL, your first task is to get access to them. A work setting already set up for Web application development
might be ready and waiting for you with all the tools that you need. On the other hand, it might be part of your job to set up this work setting yourself. Perhaps your job is to create a whole new Web site. In this chapter, I describe the tools that you need and how to get access to them.

The Required Tools

To put up your dynamic Web site, you need to have access to the following three software tools:

A Web server: The software that delivers your Web pages to the world

MySQL: The RDBMS (Relational Database Management System) that will store information for your Web database application

PHP: The scripting language that you’ll use to write the programs that provide the dynamic functionality for your Web site

I describe these three tools in detail in Chapter 1.

Finding a Place to Work

To create your dynamic Web pages, you need access to a Web site that pro­ vides your three software tools (see the preceding section). All Web sites include a Web server, but not all Web sites provide MySQL and PHP. These
are the most common environments in which you can develop your Web site:

A Web site put up by a company on its own computer: The company — usually the company’s IT (Information Technology) department — installs and administers the Web site software. Your job, for the pur­ poses of this book, is to program the Web site, either as an employee of the company or as a contractor.

A Web site that’s hosted by a Web hosting company: The Web site is located on the Web hosting company’s computer. The Web hosting com­ pany installs and maintains the Web site software and provides space on its computer where you can install the HTML (HyperText Markup Language) files for a Web site.

A Web site that doesn’t yet exist: You plan to install and maintain the Web site software yourself. It could be a Web site of your own that you’re building on your own computer, or it might be a Web site that you’re installing for a client on the client’s computer.

How much you need to understand about the administration and operation of the Web site software depends on the type of Web site access that you have. In the next few sections, I describe these environments in more detail and explain how you gain access to PHP and MySQL.

A company Web site

When the Web site is run by the company, you don’t need to understand the installation and administration of the Web site software at all. The company is responsible for the operation of the Web site. In most cases, the Web site
already exists, and your job is to add to, modify, or redesign the existing Web site. In a few cases, the company might be installing its first Web site, and your job is to design the Web site. In either case, your responsibility is to write and install the HTML files for the Web site. You are not responsible for the operation of the Web site.

You access the Web site software through the company’s IT department. The name of this department can vary in different companies, but its function is the same: It keeps the company’s computers running and up-to-date.

If PHP and/or MySQL aren’t available on the company’s Web site, IT needs to install them and make them available to you. PHP and MySQL have many options, but IT might not understand the best options — and might have options set in ways that aren’t well suited for your purposes. If you need PHP or MySQL options changed, you need to request that IT make the change; you won’t be able to make the change yourself. For instance, PHP must be installed with MySQL support enabled, so if PHP isn’t communicating cor­ rectly with MySQL, IT might have to reinstall PHP with MySQL support enabled.

In order for the world to see the company’s Web pages, the HTML files must be in a specific location on the computer. The Web server that delivers the Web pages to the world expects to find the HTML files in a specific directory. The IT department should provide you with access to the directory where the HTML files need to be installed. In most cases, you develop and test your Web pages in a test location and then transfer the completed files to their
permanent home. Depending on the access that IT gives you, you might copy the files from the test location to the permanent location, or you might trans­ fer the files via FTP (File Transfer Protocol), which is a method of copying a file from one computer to another on a network). In some cases, for security reasons, the IT folks won’t give you access to the permanent location, prefer­ ring to install the files in their permanent location themselves.

In order to use the Web software tools and build your dynamic Web site, you need the following information from IT:

The location of Web pages: You need to know where to put the files for the Web pages. IT needs to provide you with the name and location of the directory where the files should be installed. Also, you need to know how to install the files — copy them, FTP them, or use other methods. You might need a user ID and password in order to install the files.

The default file name: When users point their browsers at a URL, a file is sent to them. The Web server is set up to send a file with a specific name when the URL points to a directory. The file that is automatically sent is the default file. Very often the default file is named index.htm or index.html, but sometimes other names are used, such as default.htm. Ask IT what you should name your default file.

A MySQL account: Access to MySQL databases is controlled through a system of account names and passwords. IT sets up a MySQL account for you that has the appropriate permissions and also gives you the
MySQL account name and password. (I explain MySQL accounts in detail in Chapter 5.)

The location of the MySQL databases: MySQL databases need not be located on the same computer as the Web site. If the MySQL databases are located on a computer other than that of the Web site, you need to know the hostname (for example, thor.companyname.com) where the databases can be found.

The PHP file extension: When PHP is installed, the Web server is instructed to expect PHP statements in files with specific extensions. Frequently, the extensions used are .php or .phtml, but other exten­ sions can be used. PHP statements in files that don’t have the correct extension won’t be processed. Ask IT what extension to use for your PHP programs.

You will interact with the IT folks frequently as needs arise. For example, you might need options changed, you might need information to help you inter­ pret an error message, or you might need to report a problem with the Web site software. So a good relationship with the IT folks will make your life much easier. Bring them tasty cookies and doughnuts often.

A Web hosting company

A Web hosting company provides everything that you need to put up a Web site, including the computer space and all the Web site software. You just create the files for your Web pages and move them to a location specified by the Web hosting company.

About a gazillion companies offer Web hosting services. Most charge a monthly fee (often quite small), and some are even free. (Most, but not all, of the free ones require you to display advertising.) Usually, the monthly fee varies depending on the resources provided for your Web site. For instance, a Web site with 2MB of disk space for your Web page files would cost less than
a Web site with 10MB of disk space.

When looking for a place to host your Web site, make sure that the Web host­
ing company offers the following:

PHP and MySQL: Not all companies provide these tools. You might have to pay more for a site with access to PHP and MySQL; sometimes you have to pay an additional fee for MySQL databases.

A recent version of PHP: Sometimes the PHP versions offered aren’t the most recent versions. You certainly shouldn’t even consider a Web site that has access only to PHP 3. You want PHP 4 at least. Preferably, you want access to PHP 5.

Other considerations when choosing a Web hosting company are

Reliability: You need a Web hosting company that you can depend on — one that won’t go broke and disappear tomorrow, and one that isn’t run­ ning on old computers, held together by chewing gum and baling wire, with more downtime than uptime.

Speed: Web pages that download slowly are a problem because users will get impatient and go elsewhere. Slow pages could be a result of a Web hosting company that started its business on a shoestring and has a shortage of good equipment — or the Web hosting company might be so successful that its equipment is overwhelmed by new customers. Either way, Web hosting companies that deliver Web pages too slowly are unacceptable.

Technical support: Some Web hosting companies have no one available to answer questions or troubleshoot problems. Technical support is often provided through e-mail only, which can be acceptable if the response time is short. Sometimes you can test the quality of the com- pany’s support by calling the tech support number, or test the e-mail response time by sending an e-mail.

The domain name: Each Web site has a domain name that Web browsers use to find the site on the Web. Each domain name is regis­ tered for a small yearly fee so that only one Web site can use it. Some Web hosting companies allow you to use a domain name that you have registered independently of the Web hosting company, some assist you in registering and using a new domain name, and some require that you use their domain name. For instance, suppose that your name is Lola Designer and you want your Web site to be named LolaDesigner. Some Web hosting companies will allow your Web site to be LolaDesigner. com, but some will require that your Web site be named LolaDesigner. webhostingcompanyname.com, or webhostingcompanyname.com/~ LolaDesigner, or something similar. In general, your Web site will look more professional if you use your own domain name.

Backups: Backups are copies of your Web page files and your database that are stored in case your files or database are lost or damaged. You want to be sure that the company makes regular, frequent backup copies of your application. You also want to know how long it would take for backups to be put in place to restore your Web site to working order
after a problem.

Features: Select features based on the purpose of your Web site.
Usually a hosting company bundles features together into plans —
more features = higher cost. Some features to consider are

• Disk space: How many MB/GB of disk space will your Web site require? Media files, such as graphics or music files, can be quite large.

• Data transfer: Some hosting companies charge you for sending Web pages to users. If you expect to have a lot of traffic on your Web site, this cost should be a consideration.

• E-mail addresses: Many hosting companies provide you with a number of e-mail addresses for your Web site. For instance, if your Web site is LolaDesigner.com, you could allow users to send you e-mail at me@LolaDesigner.com.

• Software: Hosting companies offer access to a variety of software for Web development. PHP and MySQL are the software that I dis­ cuss in this book. Some hosting companies might offer other data­ bases, and some might offer other development tools such as FrontPage extensions, shopping cart software, and credit card validation.

• Statistics: Often you can get statistics regarding your Web traffic, such as the number of users, time of access, access by Web page, and so on.

One disadvantage of hosting your site with a commercial Web hosting com­ pany is that you have no control over your development environment. The Web hosting company provides the environment that works best for it — probably setting up the environment for ease of maintenance, low cost, and minimal customer defections. Most of your environment is set by the com­ pany, and you can’t change it. You can only beg the company to change it. The company will be reluctant to change a working setup, fearing that a change could cause problems for the company’s system or for other customers.

Access to MySQL databases is controlled via a system of accounts and pass­ words that must be maintained manually, thus causing extra work for the hosting company. For this reason, many hosting companies either don’t offer MySQL or charge extra for it. Also, PHP has a myriad of options that can be set, unset, or given various values. The hosting company decides the option settings based on its needs, which might or might not be ideal for your purposes.

It’s pretty difficult to research Web hosting companies from a standing start — a search at Google.com for Web hosting results in almost 6 million hits. The best way to research Web hosting companies is to ask for recommendations from people who have experience with those companies. People who have used a hosting company can warn you if the service is slow or the computers are down often. After you gather a few names of Web hosting companies from satisfied customers, you can narrow the list to the one that is best suited to your purposes and is the most cost-effective.

Setting up and running your own Web site

If you’re starting a Web site from scratch, you need to understand the Web site software fairly well. You have to make several decisions regarding hard­ ware and software. You have to install a Web server, PHP, and MySQL — as well as maintain, administer, and update the system yourself. Taking this route requires more work and more knowledge. The advantage is that you have total control over the Web development environment.

Here are the general steps that lead to your dynamic Web site (I explain these steps in more detail in the next few sections):

1. Set up the computer.

2. Install the Web server.

3. Install MySQL.

4. Install PHP.

If you’re starting from scratch, with nothing but an empty space where the computer will go, start at Step 1. If you already have a running computer but no Web software, start at Step 2. Or if you have an existing Web site that does not have PHP and MySQL installed, start with Step 3.

Setting up the computer
Your first decision is to choose which hardware platform and operating system to use. In most cases, you’ll choose a PC with either Linux or Windows as the operating system. Here are some advantages and disadvan­ tages of these two operating systems:

Linux: Linux is open source, so it’s free. It also has advantages for use as a Web server: It runs for long periods without needing to be rebooted; and Apache, the most popular Web server, runs better on Linux than Windows. Running Linux on a PC is the lowest cost option. The disad­ vantage of running Linux is that many people find Linux more difficult to install, configure, administer, and install software on than Windows.

Windows: Unlike Linux, Windows is not free. However, the advantages are that most people feel that Windows is easier to use, and because it’s widely used, many people can help you if you have problems.

I assume that you’re buying a computer with the operating system and soft­ ware installed, ready to use. It’s easier to find a computer that comes with Windows installed on it than with Linux, but Linux computers are available. For instance, at this time, Dell, IBM, and Hewlett-Packard offer computers with Linux installed.

If you’re building your own hardware, you need more information than I have room to provide in this book. If you have the hardware and plan to install an operating system, Windows is easier to install, but Linux is getting easier all the time. You can install Linux from a CD, like Windows, but you often must provide information or make decisions that require more knowledge about your system. If you already know how to perform system administration
tasks (such as installing software and making backups) in Windows or in Linux, the fastest solution is to use the operating system that you already know.

For using PHP and MySQL, you should seriously consider Linux. PHP is a pro­ ject of the Apache Software Foundation, so it runs best with the Apache server. And Apache runs better on Linux than on Windows. Therefore, if all other things are equal and the computer is mainly for running a Web site with a Web database application, Linux is well suited for your purposes.

Other solutions besides a PC with Windows or Linux are available, but they’re less popular:

Unix-based: Other free, Unix-based operating systems are available for PCs, such as FreeBSD (which some people prefer to Linux) or a version of Solaris provided by Sun for free download.

Mac: Mac computers can be used as Web servers. Most newer Macs come with PHP installed. Installing PHP and MySQL on Mac OS X is fairly simple. There are fewer Mac users, however, so it can be difficult to find help when you need it. One good site is www.phpmac.com.

Installing the Web server
After you set up the computer, you need to decide which Web server to install. The answer is almost always Apache. Apache offers the following advantages:

It’s free. What else do I need to say?

It runs on a wide variety of operating systems. Apache runs on Windows, Linux, Mac OS, FreeBSD, and most varieties of Unix.

It’s popular. Approximately 60 percent of Web sites on the Internet use Apache, according to surveys at www.netcraft.com/survey and at www.securityspace.com/s_survey/data/. This wouldn’t be true if it didn’t work well. Also, this means that a large group of users can provide help.

It’s reliable. After Apache is up and running, it should run as long as your computer runs. Emergency problems with Apache are extremely rare.

It’s customizable. The open source license allows programmers to modify the Apache software, adding or modifying modules as needed to fit their own specific environment.

It’s secure. Free software is available that runs with Apache to make it into an SSL (Secure Sockets Layer) server. Security is an essential issue if you’re using the site for e-commerce.

Apache is automatically installed when you install most Linux distributions. Most recent Macs come with Apache installed. For most other Unix flavors, you have to download the Apache source code and compile it yourself, although some binaries (programs that are already compiled for specific operating systems) are available. For Windows, you need to install a binary file — preferably on Windows NT/2000/XP, although Apache also runs on Windows 95/98/Me. As of this writing, Apache 1.3.28 and 2.0.47 are the cur­ rent stable releases. (Information on Apache versions is available in Appendix C.) See the Apache Web site (httpd.apache.org) for information,
software downloads, documentation, and installation instructions for various operating systems. The Web site provides extensive documentation that is improving all the time.

Other Web servers are available. Microsoft offers IIS (Internet Information Server), which is the second most-popular Web server on the Internet with approximately 27 percent of Web sites. Sun offers iPlanet (formerly Netscape Enterprise Server), which serves less than 5 percent of the Internet. Other Web servers are available, but they have even smaller user bases.

Installing MySQL
After setting up the computer and installing the Web server, you’re ready to install MySQL. You need to install MySQL before installing PHP because you need to provide the path to the MySQL software when you install PHP.

But before installing MySQL, be sure that you actually need to install it. It might already be running on your computer, or it might be installed but not running. For instance, many Linux distributions automatically install MySQL. Here’s how to check whether MySQL is currently running:

Linux/Unix/Mac: At the command line, type the following:

ps –ax

The output should be a list of programs. Some operating systems (usu­ ally flavors of Unix) have different options for the ps command. If the above comment does not produce a list of the programs that are run­ ning, type man ps to see which options you need to use.

In the list of programs that appears, look for one called mysqld.

Windows: If MySQL is running, you should see it in your system tray at the bottom of your screen, possibly as a traffic signal with a green light. If you cannot find an icon for it, it’s probably not running.

Even if MySQL isn’t currently running, it might be installed, just not started. Here’s how to check to see whether MySQL is installed on your computer:

Linux/Unix/Mac: Type the following:

find / -name “mysql*”

If a directory named mysql is found, MySQL has been installed.

Windows: Look for a program called WinMySQLadmin, which starts and stops MySQL, among other functions. You might be able to find it on the Start menu (choose Start➪Programs). If not, look for it in a MySQL direc­ tory, which is probably at c:\mysql\bin.

If MySQL is installed but not started, here’s how to start it:

Linux/Unix/Mac:

1. Change to the directory mysql/bin.

This is the directory that you should have found when you were checking whether MySQL was installed.

2. Type safe_mysqld &.

When this command finishes, the prompt is displayed.

3. Check that the MySQL server started by typing ps -ax.

In the list of programs that appears, look for one called mysqld.

Windows:

1. Start the WinMySQLadmin program.

If you can’t find it on the menu, navigate to the program, which is probably at c:\mysql\bin\winmysqladmin.exe, and then double-click it.

2. Right-click in the WinMySQLadmin window.

A submenu appears.

3. Select the menu item for your operating system — Win 9x or Win
NT (which includes Win 2000 and XP).

4. Click Start the Server.

If MySQL isn’t installed on your computer, you need to download it and
install it from www.mysql.com. The Web site provides all the information and software that you need. (You can find detailed installation instructions in Appendix A.)

Installing PHP
After you install MySQL, you’re ready to install PHP. As I mention earlier, you must install MySQL before you install PHP because you need to provide the path to the MySQL software when you install PHP. If PHP isn’t compiled with MySQL support when it is installed, it won’t communicate with MySQL.

Before you install PHP, check whether it’s already installed. For instance, some Linux and Mac distributions automatically install PHP. To see whether PHP is installed, search your disk for any PHP files:

Linux/Unix/Mac: Type the following:

find / -name “php*”

Windows: Use the Find feature (choose Start➪Find) to search for php*.

If you find PHP files, PHP is already installed, and you might not need to rein­ stall it. For instance, even if you installed MySQL yourself after the PHP was installed, you might have installed it in the location where PHP is expecting it. Better safe than sorry, however: Perform the testing that I describe in the next section to see whether MySQL and PHP are working correctly together.

If you don’t find any PHP files, PHP is not installed. In order to install PHP, you need access to the Web server for your site. For instance, when you install PHP with Apache, you need to edit the Apache configuration file. All the information and software that you need is provided on the PHP Web site (www.php.net). I provide detailed installation instructions in Appendix B.

Testing, Testing, 1, 2, 3

Suppose you believe that PHP and MySQL are available for you to use, for one of the following reasons:

The IT department at your company or your client company gave you all the information that you asked for and told you that you’re good to go.

The Web hosting company gave you all the information that you need and told you that you’re good to go.

You followed all the instructions and installed PHP and MySQL yourself. Now you need to test to make sure that PHP and MySQL are working correctly.

Testing PHP

To test whether PHP is installed and working, follow these steps:

1. Find the directory in which your PHP programs need to be saved.

This directory and the subdirectories under it are your Web space. Apache calls this directory the Document Root. The default Web space for Apache is htdocs in the directory where Apache is installed. For IIS, it’s Inetpub\wwwroot. In Linux, it might be /var/www/html. The Web space can be set to a different directory by configuring the Web server (see Appendix C). If you’re using a Web hosting company, the staff will supply the directory name.

Below these lines, you should see a large table, which shows all the information associated with PHP on your system. It shows PHP informa­ tion, path and filenames, variable values, and the status of various options. The table is produced by the line phpinfo() in the test script. Anytime that you have a question about the settings for PHP, you can use the statement phpinfo() to display this table and check a setting.

4. Check the PHP values for the values that you need.

For instance, you need MySQL support enabled. Looking through the listing, find the section for MySQL and make sure that MySQL support is On.

5. Change values if necessary.

If you don’t have administrative access to PHP, you have to ask the administrator to change any values that need changing. If you installed PHP yourself and/or have administrative access to PHP, you can change the values yourself. (Changing PHP settings is discussed in Appendix B.)

Testing MySQL

After you know that PHP is running okay, you can test whether you can access MySQL by using PHP. Just follow these steps:

1. Create the following file somewhere in your Web space with the name
mysql_up.php.

You can download the file from my Web site at janet.valade.com.

<html>
<head><title>Test MySQL</title></head>
<body>
<!-- mysql_up.php -->
<?php
$host=”hostname”;
$user=”mysqlaccount”;
$password=”mysqlpassword”;

mysql_connect($host,$user,$password);
$sql=”show status”;
$result = mysql_query($sql);
if ($result == 0)
echo “<b>Error “ . mysql_errno() . “: “
. mysql_error() . “</b>”;
else
{
?>
<!-- Table that displays the results -->
<table border=”1”>
<tr><td><b>Variable_name</b></td><td><b>Value</b>
</td></tr>
<?php
for ($i = 0; $i < mysql_num_rows($result); $i++) {
echo “<TR>”;
$row_array = mysql_fetch_row($result);
for ($j = 0; $j < mysql_num_fields($result); $j++)
{
echo “<TD>” . $row_array[$j] . “</td>”;
}
echo “</tr>”;
}
?>
</table>
<?php } ?>
</body></html>

Chapter 2: Setting Up Your Work Environment 35

2. Lines 6, 7, and 8 of the program need to be changed. These lines are

$host=”host”;
$user=”mysqlaccount”;
$password=”mysqlpassword”;

Change host to the name of the computer where MySQL is installed — for example, databasehost.mycompany.com. If the MySQL database is on the same computer as your Web site, you can use localhost as the hostname.

Change mysqlaccountname and mysqlpassword to the appropriate values. (I discuss MySQL accounts and passwords in Chapter 5.) If your MySQL account doesn’t require a password, type nothing between the quotes, as follows:

$password=””;

3. Point your browser at mysql_up.php.

You should see a table with a long list of variable names and values. You don’t want to see an error message or a warning message. Don’t worry about the contents of the table. It’s only important that the table is dis­ played so that you know your connection to MySQL is working correctly.

If no error or warning messages are displayed, MySQL is working fine. If you see an error or a warning message, you need to fix the problem that’s causing the message.

Error and warning messages are usually fairly clear. The following is a common error message.

MySQL Connection Failed: Access denied for user:
‘user73@localhost’ (Using password: YES)

This message means that MySQL did not accept your MySQL account number or your MySQL password. Notice that the message reads YES for Using password but doesn’t show the actual password that you tried for security reasons. If you tried with a blank password, the message would read NO.

If you receive an error message, double-check your account number and password. Remember that this is your MySQL account number — not your account number to log on to the computer. If you can’t connect with the account number and password that you have, you might need to contact the IT department or the Web hosting company that gave you the account number. (For a further discussion of MySQL accounts and passwords, see Chapter 5.)

0 comments: