Setting up Darwinports, Pear, Apache2, PHP5, MySQL5 and Symfony on OS X ‘Tiger’

Ok, the title is a bit long, but hopefully people looking for more information about it will have an easier time finding these notes than if I had used the title: “Installing geek stuff” :)

More after the jump.

Anyway, here are some notes regarding the installation of Apache2, PHP5 and MySQL5 via Mac Ports in order to install the 1.0.7 of the PHP5 framework Symfony.

Some steps involved can take quite a while because Mac ports (Darwin ports had a name change..) compiles the requested applications instead of grabbing binaries. This also means that you need to have some compiler available on your Mac. Usually if you have installed only the base system you will NOT have a compiler, so make sure you have also installed the developer tools which came with your OS X disks. In this tutorial I assume that you’re not afraid to use the terminal and that you’re using the Bash shell in OS X, which happens to be the default on ‘Tiger’.

Now you might wonder why on Earth can’t I just use MAMP or any other ’simple’ application to do all this work?

Well, first of all MAMP is in my opinion a pain to administer. It’s not very clear nor does it use conventional configuration standards (such as using opt/local). It also lacks as far as I know a commandline version of PHP 5 which we need for Symfony. Finally I like it when I can update all my software with a short command-line statement. Either manually or using a script to do this regular and automatically without having to download installers. Mac ports may seem daunting at first, but it really takes a lot of annoying stuff out of your hands and it Just Works.

Before we start, keep a good book at hand or combine the whole process with some domestic chores in between steps, because compiling and grabbing the sources might take some time.

Install Macports
Install Mac ports using the (Tiger, Universal) disk image. Get the latest version (highest version number) and follow the instructions.

Invoke Mac ports via the command-line using the excellent iTerm or the OS X included terminal and make sure you have the latest version of Mac Ports by typing:

sudo port selfupdate.

If all went well, you should see something like this:

MacPorts base version 1.520 installed
Downloaded MacPorts base version 1.520
The MacPorts installation is not outdated and so was not updated
selfupdate done!

Ignore my specific version numbers as you might have a newer version, but the statement above assures us that you have the latest version. Keep in mind to ocassionally invoke this command to update your version of mac ports. As a general rule I always first update mac ports before I install or update new software.

Install MySQL 5
After you made sure you have the latest version of Mac Ports we’re going to install MySQL 5. Type:

sudo port install mysql5 +server

Are you wondering what the +server means? Well, it’s a so-called ‘variant’. The Mac ports people describe it as:

Before you install a port, you may want to check what variations of that port are available to use. Variants provide additional configuration options for a port.

See here and here for more information regarding variants.

UPDATE: In the latest version of Mac Ports it does not seem to be necessary anymore to use the variants with mysql5 and you should be able to use only mysql5 and leave out the +server part.

If the source grabbing and compiling went ok, you will now have installed mysql5.

Before you can use it you need to create the first database. At least that is what I learned from this comment by Shane Crawford on the mysql site when I couldn’t get mysql to work. Type:

sudo /opt/local/lib/mysql5/bin/mysql_install_db --user=mysql

This ensure you have created at least the mysql user database so mysql can keep track of its own users and such.

As we don’t want to start mysql manually all the time we need to add it to our startup items (meaning the server will be automatically started when your machine boots up). Mac ports gives this as a hint and I just repeat it here for the sake of completeness. Type:

sudo launchctl load -w /Library/LaunchDaemons/org.darwinports.mysql5.plist

This command has just added the mysql5 daemon to your startup items and ensures that mysql will automatically start each time we startup our machine. Now you’re finished with installing mysql5, but we want to make sure it works without restarting our machine.Type:

cd /opt/local/var/db

This is where mysql5 will store it’s database files, now type:

ls -l

It should show you, something similar to this:

drwx—— 12 mysql admin 408 Dec 6 09:49 mysql5

This mysql5 directory will contain your databases’ files. Now we want to start the mysql5 server and test if it is alive. Type:

cd /opt/local/share/mysql5/mysql

Then we’ll start the server manually:

sudo ./mysql.server start

And finally we will try to connect to the server. Type:

mysqladmin5 -u root ping

Which should result in:

mysqld is alive

Now, we’re certain mysql has been installed correctly. Please have a look at how to secure and configure your mysql5 server further. If you want to make sure the server start automagically the only thing you have to do is to restart your machine and type the last command which should result also in the ‘mysqld is alive’ message.

Installing Apache 2
We will continue with installing Apache2. Please type:

sudo port install apache2

After compiling Apache2, you will need to add the apache2 server to your startup items if you want it to start automagically. Just like we did with the mysql 5 server. Mac ports again gives this as a hint after the installation and I just repeat it here for the sake of completeness. Type:

sudo launchctl load -w /Library/LaunchDaemons/org.macports.apache2.plist

Note: OS X comes with a bundled Apache 1.3 which I switch off to prevent confusion and possible problems. I would suggest you do the same. Go to System Preferences->Sharing and make sure Personal Web Sharing is switched off.

Because we installed our own Apache server, this also means we will not find the log files of our freshly installed Apache 2 in the default /var/log/httpd/ directory, but instead in the /opt/local/apache2/logs/ directory. You can change this by changing the default configuration of the Apache 2 server and set the log directory to /var/log/httpd/. This has the added benefit that the logs will be easily available by the Console application. I’ll leave this to be an exercise to you. Feel free to ask questions about it in the comments below. I’ll try to answer them to the best of my ability, although I’m not an Apache gure by far :)

Now to make sure our freshly installed Apache2 uses the ~/Sites (BTW ~ stands for our home directory. In my case Users/bjorn) directory as its webroot we need to edit the configuration file.

Type:cd /opt/local/apache2/conf/

Make a backup of the original configuration, just in case. Type:

sudo cp httpd.conf ~/httpd.conf.original

Now, in case any of our changes prevent us to use the Apache server you just remove the editted configuration and replace it with the backup. Just type the following within the conf directory and you’ll have the original configuration and can try again:

sudo rm httpd.conf
sudo mv ~/httpd.original.conf httpd.conf

Let’s continue and make ~/Sites as our default webroot. Edit the httpd.conf file. Type:

sudo nano httpd.conf

Now search (CTRL-w) the file for: DocumentRoot. It will look similar to this:

DocumentRoot “/opt/local/apache2/htdocs”

Copy the line (CTRL-k, CTRL-u) and place a hash (#) in front of the original line. Change the new line into:

DocumentRoot “/Users//Sites”

Where is the exact name you use to login in your machine. In my case its bjorn.

The end result should look like:

#
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
#DocumentRoot “/opt/local/apache2/htdocs”
DocumentRoot “/Users/bjorn/Sites”

Now save the file (press CTRL-x, press Y and then ENTER).

We need to restart the Apache2 server so the changes have effect. Type:

cd /opt/local/apache2/bin/

And then:

apachectl configtest

This will test if the changes made to the config file we’re correct and helps to make sure you haven’t made a typo. You would see now something similar to this:

Syntax OK

Now, we’re going to restart the server:

apachectl restart

And if everything went ok, you would see something like this:

restart: httpd started

Now we want to make sure it all works, we will put a a file called index.html in the /Users//Sites directory. The contents of the file are:


< !DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“http://www.w3.org/TR/2000/REC-xhtml1-20000126/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml” xml:lang=”en” lang=”en”>
<head>
<meta http-equiv=”content-type” content=”text/html; charset=iso-8859-1″ />
<title>The server works</title>
</head>
<body>

<h1>THE SERVER WORKS!</h1>

</body>
</html>

Now open up a browser and type in the url bar:

127.0.0.1

You should now see the message: The server works

Apache seems to be installed correctly, but afer we’ve installed php5 we can now for sure…so keep your fingers crossed.

Install php5
After setting up Apache 2 and Mysql 5 its time to install PHP 5. Type:

sudo port install php5 +apache2 +mysql5

We alse include the variants apache2 and mysql5 in order so php 5 will have the necessary tools to work with Apache 2 and Mysql 5. If everything went ok, this should have installed php 5 for you.

Install PEAR
Now that we’ve installed php 5 we also should be able to use PEAR. I say, should because I’ve had some issus with PEAR. Mainly because updating PEAR was more troublesome that I’d expected. Nonetheless let’s make sure PEAR uses our fresh installed php5. Type:

sudo pear config-set php_bin /opt/local/bin/php

This command has set the php interpreter for PEAR to be the one we installed. If you’ve installed multiple versions of php doing this ensures that we will use the correct php version with PEAR.

Now, we need to make sure PEAR is up-to-date. Type:

sudo pear upgrade pear --onlyreqdeps

This should update PEAR and we use the switch –onlyreqdeps to make sure any required dependancies are also installed.

Ok, we’re finished now with setting up PEAR and all the other tools and now we can setup Symfony.

Install Symfony
We will use the PEAR install method for Symfony (see also the installation chapter of the Symfony book for notes on installing Symfony). This makes it easy to update to a newer version of Symfony. Think of PEAR along the same lines as Mac ports. It makes it easy to keep up-to-date and allows you to install nice and useful components for PHP development. Type:

sudo pear channel-discover pear.symfony-project.com

This adds the Symfony channel to the PEAR installer. Now PEAR knows about Symfony, which is not an official PEAR project and therefor it cannot use the default channel. However PEAR allows for the addition of non-PEAR projects using different channels. See also the PEAR documentation on channels.

PEAR knows about Symfony and we’re ready to install. Type:

pear install symfony/symfony

This installs Symfony in the PEAR default data directory:

/opt/local/lib/php

We can check if Symfony is installed by typing:

symfony -v

This should show the version of Symfony installed. If you get an error along the lines of:

-bash: symfony: command not found

You’ve probably haven’t got the symfony executable in your path. Which means it cannot find symfony because it doesn’t know its location. To fix this we need to add the symfony location to our path. Type:

echo export PATH=/opt/local/bin:$PATH >> ~/.profile

This command will append the default path of the symfony executable file, which resides in the default mac ports location to your bash profile even if none is present at the moment. After doing this you need to restart your terminal application in order for the changes to take effect. After the restart you should be able to use the symfony command without any problems. You can check if the changes took effect by using this command:

echo $PATH

You should see a list of all sorts of directories including:

/opt/local/bin

Now the Symfony executable file should be part of your path and after invoking the symfony command
you should see a list of possible options. Have a look at the Askeet tutorial for an introduction into starting a new Symfony project.

I hope this howto was useful to you. Feel free to ask any questions and I will try to answer them.

ps: Next time I plan on explaining my workflow in setting up web projects.

6 Responses to “Setting up Darwinports, Pear, Apache2, PHP5, MySQL5 and Symfony on OS X ‘Tiger’”

  1. rpsblog.com » A week of symfony #38 (17-&gt;23 September 2007) Says:

    [...] Setting up Darwinports, Pear, Apache2, PHP5, MySQL5 and Symfony on OS X ‘Tiger’ [...]

  2. randy melder Says:

    apache is not configured with the php anywhere… where/how should I glue in the libphp5.so?

  3. BjornW Says:

    @randy:

    I don’t think that I had to change/set that manually.

    If you do need to do it manually, have a look at your Apache config (in my case /opt/local/apache2/conf/httpd.conf) and add the following line:

    LoadModule php5_module modules/libphp5.so

    In this case I presume that your libphp5.so is in the modules dir (/opt/local/apache2/modules/).

    I hope this helps!

  4. Clayton Says:

    For those who need to compile in, for example, soap, do the following:

    Download php5:
    sudo port fetch php5

    Edit the config file:
    sudo vi `port dir php`/Portfile

    In the configure.args area add in a line that looks like this “–enable-soap \” before the last line.

    Compile from source/ install as normal:
    sudo port -s install php5 +mysql5 +apache2 +pear +tidy # etc

    you can type the following to see a list of the variants that can be compiled/installed with php5:
    port variants php5

  5. BjornW Says:

    @Clayton:

    Thanks for your addition.

  6. Pilot Says:

    I might have missed something somewhere, but following your steps didn’t fully enable php.
    I had to add the line

    AddType application/x-httpd-php .php

    Then it works fine!

    If there is another way it should have been done, can you notify me?
    Thanks

Leave a Reply