Quantcast

Test drive dynamic websites locally

Ivan's picture

This is a beginner step by step tutorial that will help you set up your Mac, so that you can run PHP and MySQL locally and test the functionality of your websites. This post is written to graphic designers, who know little about programming, but are learning to incorporate PHP engines with MySQL databases into their web sites and want to test their designs in action or want to extend their services to include basic PHP functionality. So, sorry if I sound like I'm spoonfeeding you and being overly conversational. :)

You can upload websites to your hosting servers and test them on-line, but it's much faster and easier to test them on your own machine locally. Most importantly locally you can create as many databases as you want for each of your web projects, while most hosting companies only allow one. This set-up will allow you to install opensource engines like phpBB, WordPress or Drupal. You can also start taking snippets of code from different sources and testing them on your Mac.

You need to run three applications to be able to start your own PHP webserver:

  1. Apache: Web sharing application that serves pages from your hard disk to your browser.
  2. PHP: PHP engine that translates your PHP code into html code.
  3. MySQL: Database engine, that allows you to have databases, which PHP can communicate with to fill up the html pages with content.

Apache
Switch on Personal Web SharingFirst of all let me start with the good news. If you have Mac OS X 10.3 (Panther), than you already have Apache and PHP installed already. You just need to start them.

To start Apache, you simply need to switch on the System Preferences /Sharing /Personal Web Sharing.

PHP
To start PHP you need to copy the line of code below, than open Terminal and paste it in. Than press enter (make sure that when you copy commands you don't copy any additional spaces or linebreaks, only the code):
sudo pico /etc/httpd/httpd.conf

You will need to enter your admin password. This will open a Terminal based word processor called pico. Welcome to the eighties. :)

You will need to delete two instances of "#" from the opened file to make the lines active. The two lines are the following:
#LoadModule php4_module
#AddModule mod_php4.c

Select and copy the first line. To search in this editor you will need to press CTRL-W (instead of Apple-F). Paste in the first line to the search field and press return. Once Pico located the line your we were looking for, you need to press right arrow to align with the letter "L" and a backspace to delete the "#". Now copy the second line and do a search in Pico again. Once you have deleted the second "#" you will need to save your document by pressing CTRL-O (instead of Apple-S). Press return again. Finally quit the editor by pressing CTRL-X (instead of Apple-Q).

Switch on PHP

You are still in in Terminal and you will need to restart your Apache, so that it starts PHP, so copy the following line to the Terminal:
sudo apachectl graceful

It should give you this message:
/usr/sbin/apachectl graceful: httpd gracefully restarted

MySQL
First you need to download MySQL. You will need to install MySQL-Standard, and than the MySQLStartupItem.

Now go back to Terminal and enter the following command:
sudo -b /usr/local/mysql/bin/safe_mysqld

Finally you need to add a password to your MySQL root user. Change the "yourpasswordhere" in the code below to a password you can easily remember:
/usr/local/mysql/bin/mysqladmin -u root password yourpasswordhere

Test your PHP and MySQL setup
phpinfo.php in browserTo test PHP download this file double click to decompress and put it into your Library /Webserver /Documents/. This is the folder that you will copy your sites into.

Now open your browser and type this in as a url:
http://localhost/phpinfo.php

To test your database you can simply type this into the Terminal:
/usr/local/mysql/bin/mysql test

MySQL running

The message you receive means that you opened the database named test and you're ready to work on it. You can manage your databases from the Terminal but there is an easier way to do it. For now just simply type the following to quit the MySQL prompt:
\q

MySQL graphical interface
If you prefer a more graphic interface to manage databases download this little application and double click it to open the archive.

Open the config.inc.php file in the /phpMyAdmin folder in any text editor such as TextEdit or Pico. You will need to edit this file to contain your MySQL root password.

Set up phpMyAdmin config file

Search for "root". Under the root username there will be a password field. Enter your password in there and look further. There should be more than one insance of root in the document. Enter your password under each one. Save the file. Put the phpMyAdmin folder that now contains the edited config file into your webserver folder Library /Webserver /Documents/.

Now type this into your browser to open a graphical interface:
http://localhost/phpMyAdmin/

phpMyAdmin MySQL graphical intercafeFrom now on you can create any number of databases with phpMyAdmin and create any number of folders with your websites in your Library /Webserver /Documents/ folder and you will be able to access them and test them in your browser by typing:
http://localhost/yourwebsitename/

Visit MacZealots for instruction about how to install WordPress or Drupal.

Thanks to Will for the technical guidance.

Anonymous's picture

I prefer CocoaMySQL (also ope

I prefer CocoaMySQL (also open source) to phpMyAdmin for managing MySQL databases. Self-contained Cocoa app, nicer UI, etc. Take a look.

Ivan's picture

wow thanx! that IS pretty coo

wow thanx! that IS pretty cool. it's almost bringing iTunes like simplicity to MySQL!

you just need to fille out:
host: localhost
username: root
password: (whatever you choose above, when you activated MySQL)

Anonymous's picture

Don't forget that you can als

Don't forget that you can also get ColdFusion for MacOSX, both Macromedia's one and one from New Atlanta called Blue Dragon. Go get 'em!

nick_numbers's picture

This works great,what do you

This works great,
what do you have to do in order to test .asp or .net on a mac without a testing server.

Ivan's picture

as far as i know asp will not

as far as i know asp will not run on OS X. :(

Anonymous's picture

any help for the 4.1 version

any help for the 4.1 version of mysql? i can't seem to get it working.

Anonymous's picture

I too am having problems when

I too am having problems when I tried to follow these directions but installing MySQL v4.1.9. I also installed it with phpMyAdmin 2.6.1. All of the steps worked fine except for the last one. When I open phpMAdmin (in Safari) I got the following error message:

#1251 - Client does not support authentication protocol requested by server; consider upgrading MySQL client

Has anyone had this happen to them or know how to resolve the issue?

Thanks!

Henry's picture

MySQL implemented a new passw

MySQL implemented a new password hashing method (can't remember the exact version they did it in, but it was pretty recent - maybe 4.1). That means many older scripts can't log in properly to the databases. But all is not lost! Run the following command in terminal:

mysql -u root-p

SET PASSWORD FOR user@localhost = OLD_PASSWORD('password');

where password is the password you wish to use and user is your username (other than that, leave the command exactly as it is - do not substitute the "OLD_PASSWORD" bit for your password!)

Sarva's picture

hi guys how do i preview an a

hi guys how do i preview an asp document which i am designing using Dreamweaver? i am running on win2000

thanks

alison's picture

Hi, I've followed your instru

Hi, I've followed your instructions, everything worked until i tried to change the password typing
/usr/local/mysql/bin/mysqladmin -u root password mypassword.

I got this...

/usr/local/mysql/bin/mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user: 'root@localhost' (Using password: NO)'

I tested the database and it seems ok but when i try to open phpinfo.php it just shows the code not the actual info?

what should i do?

Ivan's picture

i've replied to you in the fo

i've replied to you in the forum: link

dada1958's picture

Great tutorial & thread!

It seems that I have connection with MySQL database thanks to CocoaMySQL!
Did you know that you can download and use pMachine Pro for free? You only have to register. Good for Freeway Pro users to know that they can continue to use it; there is a workflow combined with pMachine.
Good CB is back :)


dada's weblog

paul burd's picture

CocoaMysql-SBG

It looks as though MySQL 4.1 breaks both PHPMyAdmin and CocoaMySQL, mentioned earlier in this tip. Fortunately, there is a new version of CocoaMySQL, it's now called CocoaMysql-SBG - http://www.versiontracker.com/dyn/moreinfo/macosx/26072

Here's a blurb from the site... "An updated version of the CocoaMySQL tool. Includes primarily updated compatibility with the new authentication scheme in MySQL version 4.1."

paul burd \\ multimedia designer
portfolio \\ weblog

User login

Partner With Us













Latest critique

Duwamish River Festival