 |
 |
 |
 |
 |
 |
|
Minh Huynh
Macromedia
|
|
|
Note: this article is quite dated. We encourage you to instead read Setting up a PHP development environment for Dreamweaver that was written for Dreamweaver CS3.
Creating dynamic web applications on an Apple
Macintosh computer has become easier with the
release of Mac OS X, which supports Apache,
PHP, and MySQL. In this article, I will cover
how all these technologies fit in with Macromedia
Dreamweaver MX and how to set up each application
server component to work with Dreamweaver MX.
Use the steps in this article to guide you
in getting started with these technologies.
While I don't address security in this article,
it is important and I recommend that you learn
about it at some point.
At the time of writing, my configuration
is Mac OS X version 10.2.3 (Jaguar), Apache
Web server version 1.3.27, PHP application
server version 4.2.3, MySQL database server
version 3.23.53, and Dreamweaver MX version
6.1.
|
|
Apache Web Server Configuration
By default, when you install Mac OS X 10.2
(Jaguar) it also installs Apache web server software.
Almost everything is configured and ready to host
your website. However, if you would like to run PHP,
you must make minor configuration changes. The default
Apache installation also installs PHP, but disables
it. To use PHP, you must enable the PHP module, as
follows: |
| 1 |
Launch Terminal
from /Applications/Utilities.
|
| 2 |
Open the Apache configuration
file and edit the UNIX shell prompt:
sudo pico /etc/httpd/httpd.conf. |
| 3 |
When prompted for a password,
enter the Mac OS X administrator account password. |
| 4 |
Uncomment the following lines: |
| |
LoadModule php4_module libexec/httpd/libphp4.so
AddModule mod_php4.c
|
| 5 |
Add the following lines (add
these lines above: AddType
application/x-tar .tgz): |
| |
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
|
| |
Note: All
the commands for the pico editor display on
the screen and you can start any commands with
the Control key. Use the arrow keys to move
around. Pico is a text editor, mostly for UNIX
users to use at the UNIX command line. The
pico text editor resides in /usr/bin/ directory. |
| 6 |
Add a new index page for PHP
(index.php) so that the web server will recognize
it as the first page to load: |
| |
<IfModule mod_dir.c>
DirectoryIndex index.html index.php index.htm
</IfModule>
|
| 7 |
Save the httpd.conf file
by pressing ^X (control + x keys). When the
screen asks "Save Modified buffer?",
press "Y". When prompted for "File
Name to write : httpd.conf", press Return
once to accept it. This will return you to
the UNIX shell prompt.
|
|
| |
Stop
and Start Apache
There are two ways you can stop and start the Apache
web server. This first method, described below,
is the preferred way: use sharing in System Preferences,
as follows:
|
| 1 |
Launch System
Preferences.
|
| 2 |
Select Sharing and the Services
tab. |
| 3 |
To stop the Apache web service,
uncheck the "Personal Web Sharing" checkbox
or click the Stop button. |
| 4 |
Click the Start button to
start "Personal Web Sharing." This
will turn on the Apache web service. |
|
| |
The
second method is to stop and start Apache web server
through the /usr/sbin/apachectl script
from the UNIX shell, as follows (note that the first
method, above, is preferable): |
| 1 |
Launch the
Terminal utility.
|
| 2 |
To stop the Apache web service
enter the following at the UNIX shell prompt: sudo
/usr/sbin/apachectl stop.
(Enter the Macintosh administrator password
when prompted for a password.) |
| 3 |
To start the Apache web service,
enter the following at the UNIX shell prompt: sudo
/usr/sbin/apachectl start.
(Enter the Macintosh administrator password
when prompted for a password.) |
Note: You
can restart the Apache web server without
first stopping it by entering
sudo /usr/sbin/apachectl
restart.
|
|
Testing the Apache Web Server with PHP
Use the Macintosh text editor, textedit to
create a PHP file to test your Apache configuration.
This determines if you successfully enabled the PHP
application server module. To test the PHP module,
follow these steps: |
| 1 |
Launch TextEdit
|
| |
| • |
By default,
when you create a text file using TextEdit,
it will give the file an RTF extension.
You need a .PHP extension; therefore,
change TextEdit's preferences using the
following steps: |
| |
| a |
Select
TextEdit from the menu and select
Preferences. |
| b |
Under New Document
Attributes, select Plain text. |
| c |
Under Saving,
uncheck append ".txt" extension
to plain text files. |
| d |
Close the Preferences
window. |
|
|
| 2 |
Copy the following PHP code
into the TextEdit window: |
| |
|
| 3 |
Choose File > Save As...
Save the file as phpinfo.php into your home
directory. Under Where, choose Home. Then,
save the file in the "Sites" folder. |
| 4 |
To test this file, browse
it at the following URL: http://localhost/~UserName/phpinfo.php where ~UserName is
the name of your account. If you have configured
everything correctly, the following appears: |
| |
|
| |
 |
|
| |
Creating
a MySQL Account
Before installing MySQL, you must create a user account
named mysql and
a group called mysql (the
MySQL database will be running under the same username
and group). The mysql name and group should have
been created when you installed Mac OS X 10.2 (Jaguar).
This has nothing to do with installing MySQL database.
The system creates this account and group by default.
To verify that your Macintosh has this account, follow
these steps: |
| |
| 1 |
Launch NetInfo
Manager by going to /Applications/Utilities/. |
| 2 |
If the NetInfo Manager's domain
list does not start, select Domain > Open
and enter: / for
the Domain entry. Click OK |
| 3 |
Under the second column of
the Domain list, you should see a list of things
such as aliases, groups..........users. |
| 4 |
Select users to verify that
the user mysql exists. |
| 5 |
Select groups under the second
column to verify that group mysql exists. |
| 6 |
If the user and group named mysql do
not yet exist, create them as follows: |
| |
| a |
The easiest
way to create any new account is to use
the System Preferences Accounts. |
| b |
Create the mysql user
account the same way you would create
any new user account. Use the following
steps:
|
| |
| i |
Click
New User. |
| ii |
Enter MySQL User
for the Name. |
| iii |
Enter mysql for
the Short Name. |
| iv |
Do not enter a
password and uncheck both "Allow
user to administer this computer" and "Allow
user to log in from Windows". |
| v |
Click Save. Click
Ignore when the system prompts
you with "No password has
been set for this user." |
|
| c |
Now that you've created
the mysql user account, launch the NetInfo
Manager (/Applications/Utilities). Before
you can change or verify the properties
or values of the mysql user account,
you need to authenticate yourself by
choosing Security > Authenticate and
when prompted for a password, enter the
Mac OS X administrator account password. |
| d |
Click the users column
and search for the mysql account
that appears to the right of the users
column. |
| e |
Click mysql.
The mysql account
should have the following properties: |
| |
|
| |
| Property |
Value(s) |
| expire |
0 |
| realname |
MySQL User |
| name |
mysql |
| passwd |
* |
| uid |
74 |
| _writers_passwd |
mysql |
| change |
0 |
| shell |
/dev/null |
| home |
/dev/null |
| gid |
74 |
|
| |
|
| • |
Uid and gid can be any
number that you have not yet used. Both
properties use Number 74 by default. |
| • |
If you need to change
or add any information, you must authenticate
yourself by
selecting Security > Authenticate
before you can make any changes. |
| • |
After you authenticate
yourself ,
you can double click any field under " Property
and Value" and modify the entries.
For example, if you need to delete any
field such as picture (picture is one
of the available properties), select
the property and click Delete. |
| • |
If you need to add a
new property, such as sharedDir (sharedDir
is one of the available properties),
click New. |
|
|
Note: Using
the System Preferences/Accounts to create the mysql account
will make the account appear in the System Accounts.
However, the account will not appear on the Macintosh
Login screen. |
Installing MySQL Database
By default, if you have Mac OS X 10.x server installed,
this process has already installed MySQL database
for you, too. If you have to install MySQL, however,
it's a simple process that takes no longer than
a half hour (I went through this process). Compiling
your own MySQL has several benefits over the precompiled
binary. These benefits include adding or removing
features that you do not need, adding/removing
the configurations, and always having the latest
version. For most systems, however, the precompiled
binary should be sufficient. Below, I will walk
you through the precompiled binary of MySQL.
You can download the precompiled MySQL binary
from http://www.entropy.ch/software/macosx/mysql/.
Once you've downloaded the MySQL installer from
the entropy.ch website,
double click the MySQL installer package (mysql-3.23.53.pkg)
and follow the instructions. By default, the MySQL
installation installs in the /usr/local folder.
Note that all steps hereafter will be in UNIX.
If you know UNIX, this benefits you; however, if
you are not familiar with UNIX, don't panic. It's
very simple if you follow these steps below carefully.
|
| |
| 1 |
Double click the MySQL Installer
to install the MySQL database. |
| 2 |
Launch the Terminal utility. |
| 3 |
At the UNIX shell prompt,
enter:
cd /usr/local/mysql
|
| 4 |
Enter:
sudo ./scripts/mysql_install_db
This will execute the shell script to create
the MySQL system tables and set up all the
necessary files.
|
| 5 |
Enter:
sudo chown -R mysql:mysql
data
This UNIX command changes both the owner
and group of the data directory to mysql.
|
| 6 |
While staying
in the /usr/local/mysql/ directory,
start the MySQL database. Enter:
sudo ./bin/safe_mysqld
--user=mysql &
|
| 7 |
If you need to set up the
password for the root account (this root
account is the super user of MySQL database),
enter:
/usr/local/mysql/bin/mysqladmin
-u root password 'new-password'
|
| 8 |
To
test if you can log in using the root account,
enter:
/usr/local/mysql/bin/mysql
-uroot -p mysql
When prompted for a password, enter the
newly created password. The mysql> database
prompt appears.
|
|
Note:
If you encounter any problems, repeat steps 1 through
8 above. You must first completely remove the MySQL
installation by running /usr/bin/rm
-rf /usr/local/mysql/. |
| |
Creating
a New MySQL Database User and Table
After you install the MySQL database on your system,
the installation should create two databases: mysql
and test. In the following example, you will not
use the test database, but you will create a new
test database for test purposes. If you are still
logged in to the MySQL database from step 8 as root
user in the "Installing MySQL Database" section
above, create a MySQL user. If you are not logged
in as root, you must log in following step
8 above.
Once you log in at the mysql> prompt, run the
MySQL statement/query or script:
|
| 1 |
Enter the command
to change the database: |
| |
|
| 2 |
Create a new database called
dreamweavermx: |
| |
CREATE DATABASE dreamweavermx;
|
| 3 |
Now you are ready to create
a MySQL username called dwmx,
set the password to mm,
grant select, insert, delete and update permissions,
and only allow users to log in from localhost: |
| |
GRANT SELECT, INSERT, DELETE, UPDATE ON dreamweavermx.* TO dwmx@localhost IDENTIFIED BY 'mm';
|
| 4 |
Enter the command to reload
the system tables: |
| |
|
| 5 |
Enter the command to query
the database table: |
| |
SELECT db, host, user FROM db WHERE user='dwmx';
|
| |
You will see your query result. |
| 6 |
Change the database to dreamweavermx: |
| |
|
| 7 |
Create a table called movies
in the dreamweavermx database by copying the
following MySQL script and pasting it at the mysql> prompt:
|
| |
CREATE TABLE movies (
movies_id INTEGER AUTO_INCREMENT NOT NULL,
PRIMARY KEY (movies_id),
firstname VARCHAR(20) NULL,
lastname VARCHAR(20) NULL,
email VARCHAR(35) NULL
);
|
| 8 |
Enter the command to describe
the movies table: |
| |
|
| |
You will see the table definition. |
| 9 |
Insert seven records into
the movies table by copying the following insert
statements and pasting them into the mysql
prompt: |
| |
INSERT INTO movies VALUES(1,'Harry','Potter','harry@harrypotter-fake.com');
INSERT INTO movies VALUES(2,'Ron','Weasley','ron@harrypotter-fake.com');
INSERT INTO movies VALUES(3,'Hermione','Granger','hermione@harrypotter-fake.com');
INSERT INTO movies VALUES(4,'Draco','Malfoy','draco@harrypotter-fake.com');
INSERT INTO movies VALUES(5,'Albus','Dumbledore','albus@harrypotter-fake.com');
INSERT INTO movies VALUES(6,'Minerva','McGonagall','minerva@harrypotter-fake.com');
INSERT INTO movies VALUES(7,'Rubeus','Hagrid','rubeus@harrypotter-fake.com');
|
| 10 |
Query the movies table and
confirm that you inserted seven records: |
| |
SELECT count(*) FROM movies;
|
| |
This should return seven records. |
| 11 |
Finally, log out from the
MySQL database with the following command: |
| |
|
|
| |
Using
Dreamweaver MX to Create PHP/MySQL Applications
You will use Dreamweaver MX and what you
have learned in this article to create PHP/MySQL
applications. Since I'm logging to my Mac OS X system
with a username admin with
administrator privileges, I will set up my site definitions
in Dreamweaver using my home directory for the location
of my saved PHP files. |
| 1 |
Launch Dreamweaver
MX. |
| 2 |
Select Site > New Site. |
| 3 |
Select the Advanced tab if
it's not already selected |
| |
| a |
Enter the
following information under the Local
Info category (Click here to
see a sample of a screen shot of the
Local Info category): |
| |
| • |
Site
Name: Assign a name
for your site, such as PHP-MySQL
Site. |
| • |
Local
Root Folder: Choose
any folder in your system by
clicking the Browse folder icon. |
| • |
Check the Enable
Cache button. |
|
| b |
Remote Info (Click here to
see a sample of a screen shot of the
Remote Info). |
| |
| • |
Access:
Local/Network |
| • |
Remote
Folder*: HardDrive:Users:admin:Sites: |
| • |
Check the Refresh
Remote File List Automatically
checkbox. |
|
| c |
Testing Server (Click here to
see a sample of a screen shot of the
Testing Server). |
| |
| • |
Server
Model: PHP MySQL |
| • |
This Site
Contains: Dreamweaver
MX Pages Only should be selected. |
| • |
Access:
Local/Network |
| • |
Testing
Server Folder*: Should
be the same as the "Remote
Folder" in the Remote Info
entry. |
| • |
URL Prefix**:
http://127.0.0.1/~admin/ |
|
|
| 4 |
Click OK to save your site
definition and click Done when you are finished. |
|
| |
| Next you
will create your first PHP page, which will retrieve
all of the records from the database that you created
and display them on the page. |
| |
| 1 |
Create a new page
in Dreamweaver MX by selecting File > New > General > Dynamic
Page > PHP > Click the Create button. |
| 2 |
Save the page as harrypotter.php
by selecting File > Save As. |
| 3 |
Enter the following text anywhere
on your page: "Harry Potter - The Casts". |
| 4 |
To create a database connection
and recordset, select Window > Databases. |
| 5 |
Under Databases, click the
plus button and choose MySQL Connection. Enter
the following information in the connection
dialog box: |
| |
| a |
My
Connection: PHPMySQLConn |
| b |
MySQL Server: localhost |
| c |
User Name: dwmx |
| d |
Password: mm |
| e |
Database: dreamweavermx
In this step, you can leave the Database
field blank and click Select to choose
the database from a list. |
| f |
Click the Test button
to the test the connection. If everything
tests OK, you should see the message "Connection
was made successfully." |
| g |
Click OK to close the
testing window. |
|
| 6 |
Click OK to save the connection. |
| 7 |
To get to the Bindings tab
and create a recordset, select Window > Bindings.
Click the plus button, choose Recordset, and
enter the following information. |
| |
| a |
Name: rsPHPMySQLConn |
| b |
Connection: PHPMySQLConn (Select
from the pop-up menu) |
| c |
TableName: movies |
| d |
Columns: All |
| e |
Click the Test button.
You will see all seven records return. |
| f |
Click OK to close the
test dialog. |
| g |
Click OK again to save
the recordset. |
|
| 8 |
To add a dynamic table to
the page, place the cursor below the page heading "Harry
Potter - The Casts" and select Insert > Application
Objects > Dynamic Table. |
| 9 |
Enter the following in the
Dynamic Table dialog: |
| |
| |
Recordset: rsPHPMySQLConn (select
from the pop-up menu) |
| |
Show: pick
All Records |
| |
Border: 1 |
Click
OK.
|
|
| 10 |
This inserts the dynamic table
on the page. |
| 11 |
Select File > Save to save
the page. |
| 12 |
Next, transfer the files harrypotter.php
and Connections/PHPMySQLConn.php to the Remote
Site. |
| 13 |
Launch your browser and enter
http://localhost/~admin/harrypotter.php into
the Address bar and press the Return key. Your
page will display seven records, as shown below: |
| |
|
| |
 |
| |
|
| |
See the source
code for the harrypotter.php page in this
TXT file. |
|
| |
| * If you
would like to use the Apache Web server root directory
to host your site, the default root directory is
in /Library/WebServer/Documents/. Ensure that the
account that logged in to the Macintosh has read
and write permissions to this directory.
** If you are using the /Library/WebServer/Documents/
for your Remote Folder and Testing Server Folder,
your URL Prefix will be http://localhost/ or http://127.0.0.1/.
|
| |
|
| |
|
About the author
Minh Huynh has been with Macromedia for about
three years and is currently a Technical Support
Engineer for Dreamweaver and Contribute products.
In his spare time, he enjoys traveling, coxing,
photography, and eating in new restaurants.
|
|
|
|