Working with PHP 5 in Mac OS X 10.5 (Leopard)
OS X is a great development platform for working with PHP. Leopard
comes with Apache, PHP and many other development tools, such as
subversion already installed. Leopard brings a much needed upgrade from
Tiger’s tired PHP 4 to a very modern version of PHP 5.2.4. This is a
guide for setting up a PHP development environment under 10.5 using the
version of PHP that ships with leopard.
Enable Developer Tools
These steps may not be strictly necessary for this process, but I find it useful to do them.
First, enable your root password.
You may also want to install XCode Tools from your Leopard disk (or grab the latest from Apple developer tools). The tools are required is you are going to compile any extensions for PHP.
Editing Configuration Files
We will have to edit several configuration files that exist as part
of the unixy underpinnings of OS X. I’m going to recommend the free
text editor, TextWrangler
for this purpose. Normally, the finder hides the configuration files
from view. However, in the finder, you can use the “Goto Folder…”
option under the “Go” menu to view these files. This option if
available via command-shift-G. Actually, this option is available in
any file open dialog in OS X via command-shift-G. In addition, Text
Wrangler will allow you to browse these files with its “open hidden…”
option. But, the much easier option is selecting “Open file by name…”
(command-D) and just typing the full path and filename. To save many of
these files, you will need to enter your root password. Be Careful.
PHP is installed in Mac OS X by default, but not enabled. To enable
it, we must edit the apache 2 configuration file, which is located at /etc/apache2/httpd.conf. Find the line which loads the PHP 5 module, which looks like this:
#LoadModule php5_module libexec/apache2/libphp5.so
The line is currently commented out. All we have to do is remove the comment symbol, #, so the line looks like this:
LoadModule php5_module libexec/apache2/libphp5.so
Go to the sharing panel in system preferences and enable “Web Sharing.” This will start the apache server.
Another way to do this is to type the following in the Terminal application:
sudo apachectl start
You will be prompted to enter your root password. After that, your
apache server should now be running. If you need to restart the server
from the terminal, you can type this:
sudo apachectl restart
If you find this tedious to type, there is a script that you can download to do this later in this post.
Visiting our Web Site
Now, lets check our work. In the sharing panel, you can click on the
URL under “Your computer’s website.” Alternatively, in the web browser,
go to the url http://localhost/.
localhost is a special name that means “My computer.” If your web
server is working, you should see a page titled “Test Page for Apache
Installation.” If you go to http://localhost/manual/, you can read an Apache 2.2 manual, hosted from your own server. But, this don’t tell you that PHP is working.
For that, we’ll have to create a very simple php program. Create a new file in TextWrangler and type the following:
< ?php phpinfo(); ?>
(Don’t just copy and paste this. Note that there should be no space between the < and the ?php. The WordPress software I use for this blog inserts an extra space.)
Save this using the file name info.php in the /Library/WebServer/Documents/
directory. (start from the top level directory of your hard drive, not
the library directory in your home directory. Now you should be able to
visit the PHP page you just created by visiting http://localhost/info.php. You should see the PHP logo and a big table of configuration information.
Showing the World
For security purposes, you should consider that anything you put in your WebServer/Documents
folder will be available across the web. If you have information that
you want to keep private, think twice about putting it there, unless
you know how to protect it.
But, if you want people to see the pages that you are sharing, there
can be a few obstacles. You can give out the URL that is listed in the
sharing control panel under “Your computer’s website.” However, if you
are behind a NAT
router, such as I am, this IP address based url will only work for
other computers on your network and not for the internet as a whole.
You may have to configure network router or firewall in order to
discover your true ip address and to route web server requests to that
IP to your computer. Doing this is beyond the scope of this tutorial.
Additionally, IP address based urls don’t make good urls to share. IP
addresses can change. If you plan to host a permanent web site, you may
want to purchase a domain name and point it to your Mac. This also, is
beyond the scope of this tutorial.
Perhaps the best option is to purchase both a domain name and
professional hosting. Apache based PHP Hosting is widely available and
cheap. You can get support from a good host on uploading your files to
the remote server. I’m going to presume that you will use one of the
many excellent PHP hosting options and are only configuring PHP on your
own machine for education, testing or development purposes.
Enabling a Personal Website
If you clicked on the URL under “Your Personal Website,” you might
have gotten a page that says forbidden. This is because in the default
configuration in Leopard, unlike in Tiger, does not allow Apache to
serve documents from home directories. If you want to enable this
feature, you have to create a new Configuration file.
Create a new file with the following contents and save it to /etc/apache2/users/jeff.conf.
Options Indexes MultiViews FollowSymLinks
Allow from all
Replace “jeff” with your user name, which is also the name of your home
directory. Exact capitalization is imporant. This tells the Apache
server that it is ok to serve web content out of the ~jeff directory. You will have to restart Apache for this to take effect.
You may also have to create a Sites
folder in your home directory to hold the files you want to serve.
Leopard will automatically bless this folder with a special Icon.
If you want to experiment with or work on more than one site at a
time, the single directory in WebServer Documents and the Personal
Websites configuration don’t work well. Projects collide and files
outside of your home directory can be harder to work with. The answer
to this is to setup virtual hosting. Lets turn our Personal Website
sharing solution into a virtual hosting solution that allows us to work
with multiple websites as subdirectories of our Sites folder.
So, lets create a sample site, called mysite. We’ll create a folder
called “mysite” as a sub folder of our Sites folder. Capitalization is
Now, we are going to want to access our site with an easy to use domain name, so that our url is http://mysite/. There is an easy way to create new domain names that are only for personal use. To do this, we can add it to our /etc/hosts file. Add the following lines at the end of this file:
# My local aliases
127.0.0.1 is a special IP address designation that never changes and
corresponds to localhost to mean this computer. We are telling our Mac
that the name mysite is hosted on the local computer. This rule is only
in effect on the same machine. If you go to a different machine, you
cannot use the http://mysite/ url.
Now we need to configure apache for virtual hosting. We are going to have to edit our /etc/apache2/users/jeff.conf file. Change the contents of this file to the following:
Options Indexes MultiViews FollowSymLinks
Allow from all
Remember to replace “jeff” with your user name. Place your info.php test file into the mysite directory and rename it to index.php. Now, restart your apache server. When you visit http://mysite/, you should now see the familiar php logo and information page.
If you want to add another site, just add a second line in your hosts
file, another subdirectory of Sites and append the following to your
apache configuration file:
Sharing with the World, Part II
Sharing your virtual hosted sites with the world is more complicated
if you don’t have a domain name setup. You can, however, add your hosts
files entries to other computers that you want to share with. However,
you have to change the 127.0.0.1 IP address to the IP address of your
computer, taking into account any NAT.
There is a special case of this. If you are using parallels, perhaps
for test viewing your pages in internet explorer, you may want your
virtual hosted sites to be available. The good news is that Windows
also supports a hosts file. Here is how to edit your windows hosts file.
The big problem is knowing what IP address to use. You can’t use
127.0.0.1 on the windows side because that is the virtual windows
machine, not your Mac’s address. You can use the IP address shown on
your network system preferences panel, 192.168.1.100 for me. But, this
number is subject to change and you will have to re-edit your hosts
file on the windows side.
If you are using Parellels, be sure to upgrade to the new beta version
for Leopard, build 5540. Once you’ve done that, if you visit the
network panel in system preferences and select the “Parallels
Host-Guest” network, you will see the IP address that parallels assigns
to your host machine. (assuming you are using Shared Networking.) You
can then use this IP address in your windows hosts file. You may also
be able to change “Using DHCP” to “Using DHCP with Manual address” and
re-entering this number if you have a problem with the number changing.
Here, my number is 10.37.129.3:
MySQL has a binary distribution for Mac OS X. They also have reasonably good documentation on installing MySQL on Mac OS X for their distribution. Note that Leopard specific packages for MySQL have not been created yet.
So far, the MySQL preferences panel from the Tiger release is broken and does not correctly start and stop MySQL (bug report. You can do this from the terminal window with
sudo /usr/local/mysql/support-files/mysql.server start
To shutdown the server type:
sudo /usr/local/mysql/support-files/mysql.server stop
If you find this tedious to type, you can download WebDevCP,
which is a small AppleScript application that I made. Launching
WebDevCP launches both Apache and MySQL. Quitting the application shuts
them both down. usually. Launching and quitting requires a password. No
warranty on this thing. It was just something I was using personally
and figured others might find useful.
Bring the mysql.sock to PHP
One problem that has come about with MySQL and Leopard is the location of the mysql.sock file. Previously, the default location for this file was in the /tmp directory. That location has now moved to the /var/mysql
directory. PHP will look for it there. Unfortunately, the default
location from the MySQL will still place it in the old location. We can
fix this by creating a my.cnf configuration file in the /etc directory. Save a file with the following contents to /etc/my.cnf:
socket = /var/mysql/mysql.sock
socket = /var/mysql/mysql.sock
In the terminal window, type the following commands to create the directory for the sock file:
sudo mkdir /var/mysql
sudo chown _mysql /var/mysql
One drawback to this is that if you have installed the MySQL GUI tools, they will look for the mysql.sock
file at the old location. You can enter the new socket in the
connection dialog under More Options, there is a box labeled “connect
using socket.” Just enter /var/mysql/mysql.sock.
Another solution is to change the php.ini file to expect the socket in a different location. I’m going with the my.cnf option because I expect the MySQL will have a Leopard version out in a few days that changes the default location.
Where is PEAR?
OS X has traditionally had problems with PEAR.
Many point updates would overwrite the included version of PEAR with an
older, and perhaps insecure version. Sadly, Apple has fixed this by not
including PEAR at all in their OS. This is a big inconvenience for
people wanting to use Apple’s default version of PHP, versus a third
party distribution. So, lets get PEAR installed. Type the following in
the terminal window to download the PEAR installer:
curl http://pear.php.net/go-pear > go-pear.php
after that, type
sudo php -q go-pear.php
To run it. Hit enter to select the default locations. PEAR will be
installed, but it won’t be ready to use until we modify our php.ini file.
PHP .ini configuration
Now we need to make some changes to our php configuration file.
Leopard has an empty configuration file by default, but provides a file
which you can use as a template. From the terminal window, type:
sudo cp /etc/php.ini.default /etc/php.ini
Now, edit the /etc/php.ini file. Find the include_path setting:
;include_path = ".:/php/includes"
And change it to
include_path = ".:/usr/share/pear"
This enables our PEAR installation. You may also want to make some
changes which will improve your ability to debug PHP. FInd the line
log_errors = Off
and change it to
log_errors = On
You have to then restart Apache for these PHP changes to go into effect.
Errors and Omissions
Thats all there is to using the version of PHP delivered with OS X.
If you find this confusing, you are probably better off with something
like XAMPP or MAMP. I’ll probably end up compiling my own versions of
PHP, but that is a different blog post. I’ve already had problems with
this configuration when I tried to install XDebug via PECL. One last
thing, if you run into problems, you can check the apache2 error_log
file using the Console application.