PHP and Web Hosting with Mac

In the summer of 2003 I was what you might call programming illiterate. I have always used and loved Macintosh computers, but if you asked me how to construct a web page, let alone set up a server with PHP capabilities, I would have been about as useful as a professional tennis player teaching advanced microbiology. That was until I enrolled in a community college HTML course, and came across a useful site on the internet and an excellent book on PHP & MySQL, all of which teach you how to construct and run PHP scripts from your Mac OSX.

Starting up the Server

As it turns out, the Mac OSX operating system (the original OSX, Jaguar, Panther, and now Tiger) ships with a little gem of a web server called Apache, a dynamic and very easy to use piece of Open Source software that lets you host a web site from your own computer.

Starting up the server is easy. Just open System Preferences, click on the Sharing pane, select ‘Personal Web Sharing,’ and click on the Start button to activate the server. VoilÃ?¡, your site should appear in your browser at the address “127.0.0.1.”
If this web address seems strange to you, just think of it as the defualt address used to access the site or web page stored on your computer. Every web address you enter into your browser is also some combination of four numbers, separated by periods (.). The name ‘www.yahoo.com’, for example, is used to replace the numbered address, so that it is easier for you to type and remember. The address “127.0.0.1” is the number used to designate the “localhost” address, or in other words, the default address of the site hosted by the computer you are currently on.

At “127.0.0.1” you should see a web page saying:

If you can see this, it means that the installation of the Apache web server software on this system was successful. You may now add content to this directory and replace this page.

If your sever wasn’t started successfully , you should see a pop-up window saying: “Not connected to the Internet: Could not open the page ‘http://127.0.0.1/’ because your computer isn’t connected to the Internet.” You don’t have to be connected to the internet to see your page; this just means the server didn’t start up correctly.

Assuming that the server started up correctly, go to “Macintosh HD/Library/Web Server/Documents,” trash everything (you won’t be able to trash the two “.gif” files or the “man” folder; just ignore these) and put in your own html page. Alternatively, if you don’t want to trash these files just yet, you can put them in a folder in Macintosh HD/Library/Web Server/Documents called “Apache,” or whatever name you choose. This time when you go back to “127.0.0.1” you should see your own web page duly displayed.

Now, you may be wondering, “since I started up this so-called ‘server’ on my computer, can my friends actually visit my page?” The answer is yes. Remember the Sharing pane of the System Preferences Application? At the bottom of this pane will be a message saying “View this computer’s website at ‘yournewwebaddress.com'”. Point your friends to that address and they should see your brand new self-made and self-hosted page. Of course you don’t have to stop there. You can create a whole site for your friends and colleagues to view, complete with internet technologies. We’ll explore one of those next.

Adding PHP

Now comes the more technical part. How do you add PHP? You have to locate the Apache server’s configuration file, named “httpd.conf,” and alter it to activate the PHP features. First locate the file by opening terminal and typing “httpd -V”. This prints the version and build parameters of the Apache server. You should see a list of build parameters all beginning with “-D” . Locate the one called “SERVER_CONFIG_FILE” and open it in a text editor such as Pico on your Terminal screen (On my computer the file was located at “/etc/httpd/httpd.conf,” so I entered “pico /etc/httpd/httpd.conf’). Warning: This httpd.conf file is extremely sensitive. Adding or deleting even one character incorrectly can cause your web server not to start.

At the bottom of the screen, you will see a list of commands that you can execute in Pico, similar to the menu system at the top of modern software programs. Each command is accompanied by a carat (^) symbol (meaning the “Control'” key) and a letter. You will use the “Where is” command to locate the lines of the ‘httpd.conf’ file that will allow you to activate PHP. Type “Control-w” and enter “PHP,” and Pico will jump to the first instance where “PHP” appears in the document. Pressing “Control-w” subsequently will call up each instance in the text where “PHP” occurs, one by one. The first two lines you will come across are
#LoadModule php4_module libexec/httpd/libphp4.so
and
#AddModule mod_php4.c
Erase the hash marks by placing the cursor to the right of them and pressing “delete”. Now keep searching until you come across:
#AddType application/x-httpd-php .php
and
#AddType application/x-httpd-php-source .phps

and erase the hash marks there too, if there are any. To exit pico, press “Control-x”. Pico will ask you if you want to save. Pressing “y” will save the file with the changes in place. But wait! What happened? Pico gives you an error message saying that it cannot write to this file. Why not? The answer lies in the way that the Unix system operates.

Unix is based on owners and permissions. For every file on your computer, there is a designation for the owner, the group, and others (everyone else). Each of these designations is granted certain permissions to any given file; the read, write, and execute permissions. Since the “httpd.conf” file does not belong to you, you have no say as to what can be done with it.
The Terminal command that allows you to change the owner of a file is “chown”. Make sure you”re in the directory “/etc/httpd” (this will be displayed on your terminal screen after the name of your computer but before the new command line and user name, e.g. “Steve-iBook:/etc/httpd steve$”). Now, to execute the “chown” command correctly, you must type “chown” followed by the name of the user you wish to own the file, followed by the file name. This would be your user name. So, if your user name is “bernard”, you would type “chown bernard httpd.conf”.

Note: your user name is the name you chose when you first started up your brand new computer or when you installed OSX. It can be found in the “Accounts” or “Users” pane of System Preferences next to “Short Name”.

You will be stopped by an error message that says:
chown: httpd.conf: Operation not permitted

To bypass this, start your command with “sudo”, which allows you to operate as if you were another user.

So, type in “sudo chown steve httpd.conf”. You will see the following warning message:

We trust you have received the usual lecture from the local SystemAdministrator. It usually boils down to these two things:
#1) Respect the privacy of others.
#2) Think before you type.

Password:

This error message is in place because you”re overriding the standard roadblock keeping you from altering the intricate files that your Mac needs to run properly. In the right hands, these bypass commands can be used to do things like add functionality to your web server. In the wrong hands, you can royally screw up your computer. Consider yourself warned.

Enter the main password that you use to log onto your computer, and Terminal should execute the command.

Now, coming around full circle, you can open httpd.conf in Pico, make the two necessary changes (see above), and this time when you exit with “Control-x” and you are asked if you want to save, you can type “y”, and Pico will permanently write the changes into the “httpd.conf” file.

Note: if Pico still doesn”t allow you to save the “httpd.conf” file, it means that the owner doesn”t have the proper permissions (namely the “write” permission) for “httpd.conf”. You can change this by typing “sudo chmod u+w httpd.conf” in Terminal.
Phew! That was a lot to take in. What we”ve basically done here is ask the computer gods for the key to the httpd.conf room, gone inside, and flipped the switches necessary to allow it to use PHP. Aren”t we crafty? To finish up, simply stop and restart your web server in the “Sharing” pane of System Preferences.

Fun with PHP

PHP, short for Hypertext Preproccesor, is a scripting language designed for use on the web. You create PHP scripts by either inserting them into HTML pages, or simply by creating them in standalone form. a PHP script is made up of an opening tag (). To run a PHP script, enter it into a text editor, save the file with a .php extension, and open it in your new web server.

Displaying the Date:
To run a PHP script that tells you the current date, copy this down in your favorite Text Editor:
echo date(“m/d/y”);
?>
Now save it as “date.php” in “Macintosh HD/Library/Web Server/Documents”, and open up your browser to “127.0.0.1/date.php”.
You should see a page containing the current date. We”ve made use here of both the “echo” and “date()” commands.

PHP Comparisons:
PHP can also be used to compare values. For example, try running this script:
$boy1 = 12;
$boy2 = 14;

if ($boy1 > $boy2)

else

?>

Here we”ve set up a program that compares two variables, “$boy1” and “$boy2,” and tells us which one is older. Try changing around the ages to see if you get a different response.

Arrays:

Another useful function of PHP is to create arrays. Arrays are basically lists or tables containing values. Copy this to your text editor:
$animals = array (Yak, Capybara, Elephant, Emu, Zebra, Aardvark, Crocodile, Whale, Titmouse);

for ($i=0; $i<=1000; $i++) ?>

Saving this page as “array.php” and opening it in a browser will list all eight animals in your array. Now, add the line “sort ($animals);” right after the line with the list of animals, and your PHP program will alphabetically sort your array for you. The cool thing about this is you can add any number of values to your array and PHP will sort them breezily. Also try “rsort($animals)” to sort your list in reverse order. For more information on PHP try googling it on the web. You should find thousands of scripts to keep you entertained.

PHP Beyond:

The real power of PHP lies in its relationship with MySQL, a web server technology that allows you to create databases into which you or end-users can store information that you can then choose to post on your web page, or view privately on your server (in this case, your home computer). As you might have guessed, since PHP is so effective at dealing with arrays, it is a perfect complement to MySQL. In fact, along with HTML, PHP and MySQL can be used to create web applications. Some great features of web applications include the ability to link HTML forms directly to your database, to allow end-users access to information of their choice directly from your database, and to create sessions, where information follows an end-user around on every page of your site so they don”t have to continuously re-enter it. For more information on PHP”s MySQL capabilities, see Janet Valade”s excellent book, PHP & MySQL For Dummies.

We”ve covered a lot here. You”ve learned how to start our own web server, enable PHP, and write some simple scripts to get you up and running. We”ve also touched on the world beyond simple PHP scripting, and some of the things you can do with it. But before you start serving your own web applications, be sure you read up on security issues; remember, when you ask someone for sensitive information to store in your database, it is possible for others to steal that data. By taking the necessary steps to make sure your server is secure, however, you can easily avoid these perils.

Leave a Reply

Your email address will not be published. Required fields are marked *


9 × = sixty three