PHP & MySQL Setup

From Eunuchs to UNIX

Page 1: Turning on the Apache Web server-->>
Page 2: Enabling PHP-->
Page 3: Installing mySQL-->>
Page 4: Installing and configuring phpMyAdmin-->

Enabling PHP

PHP is a scripting language that provides, among other things, the "middleware" that connects the "frontend" (the user’s browser) to the "backend" (the database.) As one example, suppose you have a contact page for a business on your Web site. The actual contact information resides in a database on the server. The browser may offer a page with a search field to allow users to look up a contact based on specified criteria. When the user enters a name or job function, the PHP scripts are what connect the search to the database and return the information to the browser. PHP is a server-side scripting language as opposed to a client-side or browser interpreted scripting language such as JavaScript. Your scripts are hidden from the user, and only HTML is returned to the browser. Best of all, your code is not dependent upon the browser flavor and version of the user.

Mac OS X comes with PHP already installed. We do have to enable the scripting language in the Apache configuration file, however. This file is called httpd.conf, and is located in a hidden folder inside the path: /etc/httpd/httpd.conf.

*Alternate method here.

Go into your Applications folder and open the Utilities folder. Find and launch the Terminal. You will be presented with a prompt that includes your user name. Type the following command exactly as you see it here: sudo pico /etc/httpd/httpd.conf

sudo pico

Let's break down the meaning of what you have just typed. Sudo stands for "Substitute User Do" and gives you the power of the "Super User" of the computer. Believe it or not, this is not you. (Warning! Fail to treat the Super User with the respect it demands, and you can totally mess up the Mac OS X.) Next you typed Pico to call forth one of the UNIX text editors. You are going to use it to edit the Apache configuration file, the path to which we are pointing in the last part of the command. Press the return key, and you will be asked for the administrative password. Enter it, and then you will find yourself transported to Pico.

Now we are going to use some of Pico's commands to move around the httpd. conf file. We use the control key with various letters to carry out commands. Let's start with the control and "w" keys. These will bring up a search field. We want to find the LoadModule and AddModule for PHP in the config file. In the black search area, type LoadModule. Hit return and you will be taken to where the LoadModule section of the configuration file begins. Press your down arrow key a couple of times to progress down to the LoadModule list. You will see all kinds of modules, most of which will have the # character next to them. This means they are commented out, or disabled. To enable a module you must remove the # character. Keep pressing your down arrow key till you get to the end of the LoadModule list. You should see the following module for php: #LoadModule php4_module libexec/httpd/libphp4.so
Use your left arrow key till you are over the # sign. Use the control and d keys to delete it.

After you do this, press your arrow key a few times more till you see the AddModule list. Again, in Pico you need to key down till you get to the end of the list. You will see the AddModule for php. Again, remove the # character: #AddModule mod_php4.c (Note: For those of you who have earlier versions of Mac OS X, be aware that the Load and Add modules for php do not exist in the httpd.config file. You're the Super User now, right? Go ahead and add them yourselves. Add the LoadModule and AddModule to the end of each respective list exactly as you see them listed in the above paragraphs.)

Let's save what we just did. To save, we "write out" the file, or choose the control and "O" (letter) keys. You will be presented the name of the file to write out (the httpd.conf file.) Press enter to save your work.

There's one last thing to do, and then you can test that you successfully enabled PHP. We have to add the MIME type. So go ahead and use your control and "w" keys again. Type in "AddType application" without the quotes and press enter. You should now see the line that says "AddType application/x-tar .tgz" Under that line type in the following two lines: AddType application/x-httpd-php .php and AddType application/x-httpd-php-source .phps
(Note: If you are using earlier versions of Mac OSX, these lines will already be there. You will just need to take off the comment mark.)

AddType applicaton

Now you're ready to "write out" or save the configuration file again. After you save it, exit the configuration file by choosing the control and "x" keys. Once you exit Pico, you will then need to exit Terminal. Type "exit," then close the Terminal window. You also need to go into the "Sharing" panel and stop and restart your Web server so that the new settings apply.
Now we have reached the moment you were waiting for. Let's check to see that we have successfully enabled PHP by creating a little test PHP page. Go into a text editor (BBedit is good) and type the following lines:

<HTML>
<HEAD><TITLE>PHP Test</TITLE></HEAD>
<BODY>
<?
phpinfo()
?>
</BODY>
</HTML>

Now save this as test.php. Drop it in the root directory of your Web server (Library>Webserver>Documents) Open your browser and type this URL: http://127.0.0.1/test.php If everything went smoothly, you should see a page with all kinds of information about PHP Version 4.1.2.

PHP Info Test

Alternate Method

  1. In the finder, select the Go menu.
  2. Type in /etc/httpd
  3. Click Go.
  4. Drag httpd.conf to your desktop to copy it.
  5. Open it in a text editor.
  6. Use the Find dialog in BBEdit to find the LoadModule, etc.
  7. Make your alterations as per the rest of the tutorial.
  8. When you are through making your edits, go into the terminal to copy this edited version of the config file to the necessary place.
  9. Type sudo cp ~/Desktop/httpd.conf /etc/httpd/httpd.conf
  10. Press return and enter your password.
  11. Press return again and that's it!

Next Step: Install mySQL-->
Back to Unix Tips Page-->