News:

Choose a design and let our professionals help you build a successful website   - ITAcumens

Main Menu

Mac OS X and Linux & Post-Installation Setup Tasks

Started by sivaji, Jan 11, 2008, 01:34 PM

Previous topic - Next topic

sivaji

Mac OS X and Linux

Because Mac OS X is based on the BSD operating system, much of its internals work just like any other Unix-like OS (e.g. Linux). From this point forward, owners of Mac OS X servers can follow the instructions provided for Unix/Linux systems unless otherwise indicated. No separate instructions are provided for Mac OS X unless they differ from those for other Unix-like systems.

Post-Installation Setup Tasks

No matter which operating system you're running, once PHP is installed and the MySQL server is in operation, the very first thing you need to do is assign a root password for MySQL. MySQL allows authorized users only to view and manipulate the information stored in its databases, so you'll need to tell MySQL who is an authorized user, and who isn't. When MySQL is first installed, it's configured
with a user named root that has access to do pretty much anything without even entering a password. Your first task should be to assign a password to the root user so that unauthorized users can't tamper with your databases.

To set a root password for MySQL, open a command prompt (or Terminal window) and type the following command in the bin directory of your MySQL installation:

mysql -u root mysql

This command connects you to your newly-installed MySQL server as the root user, and chooses the mysql database. After a few lines of introductory text, you should see the MySQL command prompt (mysql>). To assign a password to the root user, type the following two commands (pressing Enter after each one):

mysql>UPDATE mysql.user SET Password=PASSWORD("new password") ->WHERE User="root";
Query OK, 2 rows affected (0.12 sec)
Rows matched: 2 Changed: 2 Warnings: 0
mysql>FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.24 sec)

Be sure to replace new password with the password you want to assign to your root user.

With that done, disconnect from MySQL with the quit command:

mysql>quit
Bye

Now, to try out your new password, request that the MySQL server tell you its current status at the system command prompt:

mysqladmin -u root -p status

Enter your new password when prompted. You should see a brief message that provides information about the server and its current status. The -u root argument tells the program that you want to be identified as the MySQL user called root. The -p argument tells the program to prompt you for your password before it tries to connect. The status argument just tells it that you're interested in
viewing the system status.

If at any time you want to shut down the MySQL server, you can use the command below. Notice the same -u root and -p arguments as before:

mysqladmin -u root -p shutdown

With your MySQL database system safe from intrusion, all that's left is to configure PHP. To do this, we'll use a text file called php.ini. If you installed PHP under Windows, you should already have copied php.ini into your Windows directory. If you installed PHP under Linux using the instructions above, you should already have copied php.ini into the PHP lib folder (/usr/local/ php/lib), or wherever you chose to put it. The Mac OS X installation program will have placed the file in /usr/local/php/lib for you automatically.

Open php.ini in your favorite text editor and have a glance through it. Most of the settings are fairly well explained, and most of the default settings are fine for our purposes. Just check to make sure that your settings match these:

register_globals = Off
magic_quotes_gpc = On5
extension_dir = the directory where you installed PHP6

Save the changes to php.ini, and then restart your Web server. To restart Apache under Linux (or Mac OS X), log in as root and type this command:

shell#apachectl graceful

You're done! Now, you just need to test to make sure everything's working (see the section called "Your First PHP Script").

If Your Web Host Provides PHP and MySQL

If the host that provides you with Web space has already installed and set up MySQL and PHP for you, and you just want to learn how to use them, there really isn't a lot you need to do. Now would be a good time to get in touch with your host and request any information you may need to access these services.

Specifically, you'll need a user name and password to access the MySQL server they've set up for you. They'll probably also have provided an empty database for your use, which prevents you from interfering with the databases of other users who share the same MySQL server, and you'll want to know the name of your database.

There are two ways you can access the MySQL server directly. Firstly, you can use telnet or secure shell (SSH) to log in to the host. You can then use the MySQL client programs (mysql, mysqladmin, mysqldump) installed there to interact with the MySQL server directly. The second method is to install those client programs onto your own computer, and have them connect to your host's MySQL server.
Your Web host may support one, both, or neither of these methods, so you'll need to ask.

If your host allows you to log in by telnet or SSH to do your work, you'll need a user name and password for the login, in addition to those you'll use to access the MySQL server (they can be different). Be sure to ask for both sets of information. If they support direct access to the MySQL server, you'll want to download a program that lets you connect to, and interact with, the server. This book assumes you've downloaded from http://www.mysql.com/ a binary distribution of MySQL that includes the three client programs (mysql, mysqladmin, and mysqldump). Free packages are available for Windows, Linux and other operating systems. Installation basically consists of finding the three programs and putting them in
a convenient place. The rest of the package, which includes the MySQL server, can be freely discarded. If you prefer a more graphical interface, download something like MySQL Control Center[20]. I'd  recommend getting comfortable with the basic client programs first, though, as the commands you use with them will be similar to those you'll include in your PHP scripts to access MySQL databases.

Many less expensive Web hosts support neither telnet/SSH access, nor direct access to their MySQL servers. Instead, they normally provide a management console that allows you to browse and edit your database through your Web browser (though some actually expect you to install one yourself, which I'll cover briefly in Chapter 2). Although this is a fairly convenient and not overly restrictive solution, it doesn't help you learn. Instead, I'd recommend you install a MySQL server on your own system for  experimentation, especially in the next chapter.Once you're comfortable working with your learning server, you can start using the server provided by your Web host with the Web-based management console. See the previous sections for instructions on installing MySQL under Windows, Linux, and Mac OS X.

Your First PHP Script

It would be unfair of me to help you get everything installed and not even give you a taste of what a PHP-driven Web page looks like until Chapter 3, so here's a little something to whet your appetite.

Open your favorite text or HTML editor and create a new file called today.php. Windows users should note that, to save a file with a .php extension in Notepad, you'll need to either select All Files as the file type, or surround the file name with quotes in the Save As dialogue; otherwise, Notepad will helpfully save the file as today.php.txt, which won't work (see the note earlier in this chapter for more information). Mac OS users are advised not to use TextEdit to edit .php files, as it saves them in Rich Text Format with an invisible .rtf file name extension. Learn to use the vi editor in a Terminal window or obtain an editor that can save .php files as plain text.

Whichever editor you use, type this into the file:

File: today.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Today's Date</title>
<meta http-equiv="content-type"
content="text/html; charset=iso-8859-1" />
</head>
26 Order this 350 page hard-copy PHP/MySQL book now!
Chapter 1: Installation
<body>
<p>Today's Date (according to this Web server) is
<?php
echo date('l, F dS Y.');
?></p>
</body>
</html>

If you prefer, you can download this file, which, along with the rest of the code in this book, is contained in the code archive. See the Preface for details on how to download the archive.

Save the file, and place it on your Website as you would any regular HTML file, then view it in your browser. Note that if you view the file on your own machine, you cannot use the File > Open... feature of your browser, because your Web server must intervene to interpret the PHP code in the file. Instead, you must move the file into the root document folder of your Web server software (e.g. C:\inetpub\wwwroot\ in IIS, or C:\Program Files\Apache Group\Apache\htdocs\ in Apache for Windows), then load it into your browser by typing http://localhost/today.php. This process allows the Web server to run the PHP code in the file and replace it with the date before it's sent to the Web browser.

Pretty neat, huh? If you use the View Source feature in your browser, all you'll see is a regular HTML file with the date in it. The PHP code (everything between <?php and ?> in the code above) was interpreted by the Web server and converted to normal text before it was sent to your browser. The beauty of PHP, and other server-side scripting languages, is that the Web browser doesn't have to know anything about it — the Web server does all the work!

Don't worry too much about the exact code I used in this example. Before too long you'll know it like the back of your hand.

If you don't see the date, then something is wrong with the PHP support on your Web server. Use View Source in your browser to look at the code of the page. You'll probably see the PHP code there in the page. Since the browser doesn't understand PHP, it just sees <?php ... ?> as one long, invalid HTML tag, which it ignores. Make sure that PHP support has been properly installed on your Web server, either in accordance with the instructions provided in previous sections of this chapter, or by your Web host.
Am now @ Chennai