splogo
sp
sp sp sp sp sp sp
rght-yellowstrip sp
curve

BlogOverdrive is a content generator that will automatically add content to your blog from any RSS feed.

For your Blog Content, Website Content, SEO Content or Blog Marketing give BlogOverdrive a try today!

Content Provider

July 15, 2007 | Comments Off on Content Provider

 

Developing with php and mysql on mac os x 10.4

In this post Ill go through the process of setting up a web site running on php, mysql and apache on mac os x 10.4 (tiger).

Im running a PowerPC G5, but this setup should work on any os x 10.4 install.

Outcome

The result of this howto should be that you have a website you can access in a browser on your mac at http://www.mysite.test

It will run php and mysql locally and use your DNS and apache config files to make the site work as if it was on a remote webserver you could purchase from a typical hosting provider. But it will not need access to the internet at all.

This way you can work completely locally on your Mac (i.e. not on a network) and see changes to your code instantly as soon as you save them. Also, all links on your site should work fine when you transfer them to your web server.

My aim is to use this setup with off-the-shelf PHP/MySQL open source applications such as CMS Made Simple and WordPress , but you could do any kind of bespoke coding with this setup.

Ill be working predominantly on the Terminal command line, so if youre not confident doing so youll probably want to find another tutorial. I dont use tools such as PHPMyAdmin, so if youre looking for a web front-end this aint the place for you. This tutorial may help with some of the process, though.

Youll need administrator priviledges for your os x user account.

Disclaimer

I am a professional web developer. However, I am by no means an expert in these matters! I have been using Linux on the command line for 3 years in a professional capacity but consider myself a dabbler. Ive been using os x since version 10.1 but this is my first real foray into the command line interface of os 10.4

I have a resonable knowledge of LAMP and this will be evident in the various config files were editing throughout. However, it has to be said my knowledge of the many intricacies of unix is limited.

I hope these instructions are helpful, but I cant be held responsible if anything goes wrong!

Overview

The process is broadly as follows:

  1. Install and enable MySQL
  2. Set up Apache
  3. Set up DNS
  4. Set up PHP
  5. Test your site
  6. Make MySQL and PHP talk to each other

Step 1. Install and enable MySQL

The default install of os x 10.4 (the one youre most likely to have) does not come with mysql installed. (OS X server does).

Go and get it from the MySQL for Mac download page .

For ease of use choose the package format from the list, this will give you a dmg image file that will mount itself on your desktop.

Go into the disk image in the finder, and run (open) the mysql install package.

Also, run the propSet file. This will add a handy MySQL manager to your System Preferences.

Start MySQL by going into system preferences and clicking the start button.

Well be doing a lot of work on the command line, so lets access it now.

Start the Terminal application by (in the finder) going to Applications > Utilities > Terminal.app

You should see a command prompt.

To make mysql accessible easily on the command line well have to add an alias to the bash user preferences (if you dont know what this means, dont worry).

After each of these lines, press return.

Type cd

This changes directory to your home directory

Type ls -al

This lists the contents of your home directory. It should contain the same folder names as are shown when (in the finder) you click on your name with the house next to it. You should see something like this:

drwxr-xr-x 24 chrischris816 May 18 22:05 .drwxrwxr-t5 root admin170 Nov 122006 .-rw-r–r–1 chrischris3 Nov 122006 .CFUserTextEncoding-rw-r–r–1 chrischris12292 May 18 21:29 .DS_Storedrwx—— 44 chrischris 1496 May 18 21:29 .Trash-rw——-1 chrischris 1104 May 18 20:41 .bash_history-rw——-1 chrischris143 May 18 21:58 .mysql_history-rw-r–r–1 chrischris270 May 18 20:41 .profiledrwx——3 chrischris102 Nov 132006 .ssh-rw——-1 root chris 5372 May 18 22:05 .viminfodrwxr-xr-x3 chrischris102 Nov 30 21:34 Applicationsdrwx—— 23 chrischris782 May 17 23:30 Desktopdrwx—— 34 chrischris 1156 Dec3 02:03 Librarydrwx——8 chrischris272 Apr8 19:18 Moviesdrwx——7 chrischris238 Mar 10 19:52 Music-rw-r–r–1 chrischris45755 Apr9 00:52 PB_UploaderApplet.logdrwx—— 16 chrischris544 May 13 13:48 Picturesdrwxr-xr-x4 chrischris136 Nov 122006 Publicdrwxr-xr-x 10 chrischris340 Apr7 13:19 Shareddrwxr-xr-x 10 chrischris340 Jan5 23:22 Sites

You can see a whole load of cryptic information about your files and folders on the left, along with your name (twice) as the owner and group owner of the items. Then comes the file date information.

Anyway, what were interested in are the file and folder names listed on the right.

All the files starting with a dot (.) are hidden files, thats why you dont see them in the finder. We need to edit the .profile file so that we can access MySQL easily from the command line.

Do this with the command sudo vi .profile

Enter your password. You should see some code in the file.

Press I to enter insert mode in vi. Move the cursor with the arrow keys to the bottom of the file.

Add the following lines:

alias mysql=/usr/local/mysql/bin/mysql
alias mysqladmin=/usr/local/mysql/bin/mysqladmin
Press ESC. Then type :wq and press return.

Now you can access MySQL easily from the command line.

Now set a root password for MySQL. To do so, type the following, replacing the word mypassword with your actual password:

mysqladmin -u root password mypassword

MySQL is now nice and secure.

Log in with: mysql -root -p

Type your password.

You should now see the mysql command prompt.

Now lets, for the sake of it, create an empty database with a dedicated user.

create database mydatabase;

This creates an empty database.

grant all privileges on mydatabase.* to mydatabaseuser@’localhost’ identified by ‘mydatabasepassword’;

Obviously replace mydatabase, mydatabaseuser, and mydatabasepassword with appropriate values.

Were done with this step

Step 2. Set up Apache

Fortunately, Apache and PHP are installed by default on os x 10.4 (unlike MySQL) so the following steps should be easier.

Were going to edit the apache config files now to setup http://www.mysite.test

Edit the apache config file in your home directory (change my name for yours):

sudo vi /private/etc/httpd/users/chris.conf

Enter your password if necessary.

Youll now go in to the apache config file in the vi text editor.

Add the lines of code shown here into your apache config file.

The first part of the code is the Virtual Host container – the details about the domain. Go through each line and make the changes as necessary – the email address and site name.

Importantly, make sure the DocumentRoot line is pointing correctly at your site root. If you follow these instructions the path shown there should be fine. Replace my name with yours. Finally add the filenames of the error logs (you can just change the site name).

The bottom line tells apache to recognise index.php files as the default page in a directory.

Next we have to restart apache. But before we do that we have to make sure our document root exists, otherwise apache wont like it. In the finder, click on your Sites folder. Then create a folder (SHIFT-APPLE-N) and call it www.mysite.test. Go into this folder and create another called htdocs. This stands for hypertext documents and is where well put all your site files.

To double check this has worked, go into the Terminal and type:

ls -al /Users/chris/Sites/www.mysite.test/htdocs

Make sure this path is exactly the same as the DocumentRoot line in the apache config file. You should see a couple of dots listed as files. If you get a No such file or directory error then youll need to check the locations of your folders.

To triple check everything will be ok with apache, run a configtest:

sudo apachectl configtest

You should get a syntax ok reply. If not, check that all the paths in your apache config file are pointing at folders that actually exist.

If all is well, we can restart apache.

sudo apachectl restart

You should see an apache restarted confirmation. You can also restart apache through System Preferences > Sharing > Personal Web Sharing but I thought it would be useful to keep the control on the command line in this instance.

So now, our Mac know where to look for our site. Next, we need to tell our Mac which server our site is on – itself.

Step 3. Set up DNS

This is really easy. So before I go through the commands, a bit of background.

For those of you who dont know, DNS is essentially what makes the internet work. When you type in a web site address, such as http://www.google.com or any other, your computer (instantly) uses DNS to find out which server the site is on and then contacts that server to ask for the files. Its at that point that apache (or any other web server) takes over and serves the files.

So we need to use DNS to tell our Mac that http://www.mysite.test is actually stored on our own Mac. Sounds obvious, but it has to be done.

DNS works in a chain, if one server doesnt have any information about the site requested it passes on the request to another server. At the very top of this chain is a file on your Mac with DNS info in it that will only apply on that specific machine (there is a similar file on PCs) – it overrides any other DNS settings that may exist on the internet by providing an answer to the DNS request at the very first opportunity – on your own machine. This file is called the hosts file.

If none of this makes sense, dont worry. You can find out more at Wikipedia if youre interested .

First we need to find out the IP address of your Mac. To do this, go into System Preferences > Sharing > Personal Web Sharing and make a note of the IP address (four numbers separated with dots). Its likely to be in the form 10.0.1.1 or 192.168.0.1, or similar.
We can now edit the DNS hosts file as follows:

sudo vi /private/etc/hosts

Then add the following line at the bottom of the file:

192.168.1.2 www.mysite.test

So basically, type the IP address you noted earlier, then press TAB, then enter your domain. Save it and exit vi with ESC then :wq

Thats it for this step.

Step 4. Set up PHP

Again, PHP is installed on os x so we just need to tweak some settings to enable it.

Access your main apache config file to do this:

sudo vi /etc/httpd/httpd.conf

Then search for php by typing:

/php

Press return. You should jump to a line that looks like this:

#LoadModule php4_modulelibexec/httpd/libphp4.so

Un-comment it by deleting the # at the beginning of the line. Now press ESC then the letter n twice to find the next occurrence of php.

#AddModule mod_php4.c

Un-comment this line too. Save and exit with ESC then :wq

Now restart apache again, first testing the syntax.

sudo apachectl configtest

sudo apachectl restart

PHP should now be enabled.

Step 5. Test your site

Now we need to add some HTML or PHP files to our site to test it. Lets just make a very basic file in vi.

vi /Users/chris/Sites/www.mysite.test/htdocs/index.php

Type this code into the file, after pressing the letter I (for insert text mode). Then exit and save.
This will test both php the web server setup. To test it, open a browser and type http://www.mysite.test

You should see the PHP info file. If you dont, please let me know any issues you come across!

Step 6. Make MySQL and PHP talk to each other

Thanks to this blog post , I found out how to do this. By default OS X points PHP to the wrong MySQL socket.
Well need to make changes to the PHP config. You have to first make a copy of the default file and then change the new files permissions.
sudo cp /private/etc/php.ini.default /private/etc/php.ini

sudo chmod 755 /private/etc/php.ini

sudo vi /private/etc/php.ini

Now search for the correct line by typing:

/mysql.default.socket

Press return to be taken to the line. Change the line to:

mysql.default_socket = /tmp/mysql.sock

You can also make any other changes you may need (for example, change the memory limit). Then save, quit and restart apache.

You should be good to go!

Please let me know any comments, innacuracies or tips in relation to this article. All will be greatfully received.


Comments