Mac OS X Leopard + Apache 2 + SSL + PHP / macports (gd, etc)

UPDATE 2008/Dec: I am no longer suggesting anyone to go such lengths as following these instructions. There is MAMP for OSX and also XAMPP for OSX. XAMPP is really a nice app, it has the latest php5 (and php4 as well), mysql 5.x, and also has an apache 2 server with ssl enabled ;) In short it is cool. I have also managed to compile the advanced php cache (APC) for PHP5 as a module - which will be included from PHP6 onwards as a standard library. So go and grab XAMPP, use that and save yourself the headache.

Let me share with you how I was able to set up my local development environment using the Mac OS X leopard built-in Apache (2.2.6) web-server.

First of all I have one of the latest macbook pro laptops. These have the core 2 duo intel processors, which are capable of 64 bit operations. (If you have a simple core duo - then it’s 32 bit only and you won’t face the same problems as me.) Unfortunately - as most of you web developers using php have already found out - the Apple supplied php library for apache is really a stripped-down version of PHP 5.2.4. You can figure it out if you run “php -m” in the terminal.

It does not have the GD library, nor postgresql, not even IMAP - and I could go on and on. I was really in need of having the GD library (used for generating graphics runtime). I have tried to compile all from source - but failed (several times).

And then comes the macports project to the rescue! I would not even consider using the Leopard built-in web-server, but unfortunately SSL on the macports apache 2 on leopard (64-bit) fails miserably (google it if interested). Also I do not like how the MAMP / XAMPP projects are working (fixed path in /Applications, etc.). In contrast the Apple apache works with SSL like a charm.

Step by step instructions:

1., As macports right now cannot compile SSL for its apache 2 subpart (on core 2 duo machines at least) I had to figure out a way to get the Apple supplied apache server to have SSL. As you know for SSL to work you need to generate a self-signed certificate (or buy one from godaddy or verisign or whatever root cert auth. you fancy). This is how you do that.

a., Instructions on generating and setting up ssl on OS X by Apple
b., in Terminal type:

openssl genrsa -des3 -out server.key 1024
> [enter your passphrase, a simple password we will remove soon]
openssl req -new -key server.key -out server.csr
> [you can accept all the defaults, it does not matter, or customize it to your liking]
openssl x509 -req -days 3650 -in server.csr -signkey server.key -out server.crt
> [will ask for passphrase]
cp server.key server.key.bak
openssl rsa -in server.key.bak -out server.key
> [passphrase needs to be typed]

OK, so now you have 4 files in your directory, but you only need server.key and server.crt Copy them to /etc/apache2 as root (”sudo -s” first and then your password in terminal)

c., download self signed keys - Please only use them for local development and testing, do not use onthis on production websites! Copy them to the previously mentioned place.

2., Now we have the ssl certificates, and need to setup apache on OS X. As you know the local web server (apache) is turned on/off in the system preferences menu / sharing / web sharing check box. Turn it off. You need to have server.crt and server.key in /etc/apache2. Check it.

In terminal type nano /etc/apache2/httpd.conf
Find the line that says “#LoadModule ssl_module libexec/apache2/mod_ssl.so” and delete the # before it. Save it by hitting ctrl+O. Ctrl+X to exit.

Check that the line Include “/private/etc/apache2/extra/httpd-ssl.conf” line is in there. Itis towards the bottom. I think by default it is there, but please check.

In terminal type nano /etc/apache2/extra/httpd-ssl.conf
You need to uncomment and edit the line that starts with

SSLCertificateFile
To this:
SSLCertificateFile “/private/etc/apache2/server.crt”

And the line that starts with

SSLCertificateKeyFile
To this:
SSLCertificateKeyFile “/private/etc/apache2/server.key”

Save with ctrl+O, then exit with ctrl+X.

3., That’s it. Now you can turn web-sharing back on in the preferences panel. I think by default if you now enter http://localhost or https://localhost into your browser you should see the documents from under /Library/WebServer/Documents to come up onto your screen. I have made some further adjustments to the apache config files so that whenever I type in localhost or https://localhost all the files are coming from my home/Sites (/Users/your user name/Sites) directory - this is not documented here, but should be pretty simple for all of you to achieve.

4., So now we have SSL - it means we can now move onto getting a new php version with macports. The main drawback of using macports at the moment is that it only compiles stuff in 32 bit. As Leopard now supports 64 bit quite well - they even have the built in apache webserver in 32 / 64 bit for intel / powerpc - if you have a core2duo intel prcessor we need to make sure apache only has 32 bit for the following to work.

First turn off web-sharing in preferences if it’s on.
Then in terminal go like:

sudo -s
> [password]
cd /usr/sbin
cp httpd httpd.bak
rm httpd
lipo httpd.bak -thin i386 -output httpd

Now switch back the web-sharing and check if it still works. Apache is now running in 32 bit mode. :) with ssl.

5., Installing macports and compiling php with mysql / gd / etc.

This will take some time. First of all install the Xcode tools from your leopard disk, and also install the X11 SDK as well (the GD library needs that!) Then you can download and install macports. Get the version with leopard (10.5) support.

After installing you should make sure that macports is in the path. I have created a ~/.bash_profile file (in my home folder) and added the following to the top:

PATH=/opt/local/bin:/opt/local/sbin:$PATH

As macports will install all its files to the /opt/local directory you do not need to worry about OS X updates overwriting these files ever. You can also try a program called PortAuthority which is made for managing the macports packages in a nice graphical interface. I have it installed on my system, but never really relied on it.

So now you should basically be able to run a port version command in terminal which should spit out something like “Version: 1.5xxx”

As a normal user in terminal do this:

sudo port selfupdate (and then your password, this will update the base packages)
sudo port install mysql5 (wait… it fetches and compiles all stuff needed)
sudo port install apache2 +no_startupitem (do not start automatically - we will not use it)
sudo port install php5 +apache2 +mysql5 +sqlite (then wait a long time…)

Now we should have the php5 binary compiled with all kinds of goodness and installed into /opt/local/apache2/modules/libphp5.so

All we need to do to have it in the apple apache2 instead of the stock apple php one is to change /add a line in /etc/apache2/httpd.conf

LoadModule php5_module /opt/local/apache2/modules/libphp5.so

Save and done. Restart the local web server (same thing in preferences / sharing / web sharing tick off then on). You now have php5 installed with ssl on your Leopard. Nice and easy. Kind of :)

6., There might be problems with macports installing and configuring all the stuff, you can issue a sudo port clean (package name) anytime if something went wrong. Also some php5 init variables might need some customization I am not talking about right now - for instance having your local php connect through a UNIX socket to your local (macports installd) mysql. Etc.

This is not for the faint of heart. I know, I have had my share of mistakes before I could come to find this (working!) solution, but if you’re doing serious PHP development work on a Macintosh give it a try. There are no such tested and complete guides on the internet like this one. If you run into some troubles or errors - please comment here and I will fix it as soon as possible. Thanks, and I hope this all makes your day a bit happier.


12 Responses to “Mac OS X Leopard + Apache 2 + SSL + PHP / macports (gd, etc)”

  • haig Says:

    thanks for the indepth tutorial.
    it all seemed to be working out fine but then gave me some errors when i tried:
    sudo port install apache2 no_startupitem

    it gave me:
    Error: Requested variants do not match original selection.
    Please perform ‘port clean apache2′ or specify the force option.
    Error: The following dependencies failed to build: apache2 curl libmcrypt libxslt mhash sqlite3
    Error: Status 1 encountered during processing.

    can you help? Port Authority is no help, it crashes every time I open it.

    thanks

  • kcs Says:

    Try:

    sudo port clean apache2
    Then sudo port install sqlite3
    (sqlite3 might also need to be cleaned with sudo port clean sqlite3)
    Then again try sudo port install apache2 (plus sign)no_startupitem

    And follow the rest of the tutorial

  • haig Says:

    thanks for the followup…

    Sqlite3 intalled without problems.
    but after the “sudo port install apache2 no_startupitem” command I get another error.

    This time:
    http://blog.haigarmen.com/misc/apache2_install_error.png

    not entirely sure what’s going on
    any ideas?

    thanks

  • kcs Says:

    Hm.

    From this dump I have no idea what could have gone wrong.
    However I suggest you cleaning apache - maybe cleaning the whole /opt directory and restarting from scratch.

    One thing maybe. type ’set’ in Terminal and look for the line that starts with PATH= - It should have these:

    PATH=/opt/local/bin:/opt/local/sbin:/usr/bin:……(whatever does not matter really from this point on)

    Also if you type port installed - it should dump all the installed macports packages. For help here’s what I have on mine:

    The following ports are currently installed:
    apache2 @2.2.6_0 no_startupitem (active)
    apr @1.2.11_0 darwin_9 (active)
    apr-util @1.2.10_0 (active)
    bison @2.3_0 (active)
    bzip2 @1.0.4_1 (active)
    curl @7.17.0_0
    curl @7.17.1_0 (active)
    db44 @4.4.20_1 (active)
    expat @2.0.1_0 (active)
    fontconfig @2.4.2_1 macosx (active)
    freetds @0.64_0 mssql (active)
    freetype @2.3.5_0 (active)
    gettext @0.16.1_0 (active)
    glib2 @2.14.2_0 (active)
    gnupg @1.4.7_0 (active)
    ImageMagick @6.3.6-2_0 (active)
    jpeg @6b_2 (active)
    libiconv @1.11_6 (active)
    libmcrypt @2.5.8_0 (active)
    libpng @1.2.22_0 (active)
    libxml2 @2.6.30_0 (active)
    libxslt @1.1.22_0 (active)
    lynx @2.8.6rel.5_0 ssl (active)
    m4 @1.4.10_1 (active)
    MacPorts_Framework @0.0.1_0 (active)
    mc @4.6.0_1 (active)
    mhash @0.9.9_0 (active)
    mysql5 @5.0.45_3 (active)
    ncurses @5.6_0 (active)
    ncursesw @5.6_0 (active)
    openssl @0.9.8g_0 (active)
    p7zip @4.51_0 (active)
    pcre @7.4_0 (active)
    php5 @5.2.5_1 apache2 macosx mysql5 sockets sqlite (active)
    pkgconfig @0.22_0 (active)
    readline @5.2.007_0 darwin_9 (active)
    sqlite3 @3.5.1_0 (active)
    tiff @3.8.2_1 (active)
    unrar @3.7.8_0 (active)
    zlib @1.2.3_1 (active)

    Look for version numbers and stuff that you don’t have. I hope this helps a bit for you.

  • aaron Says:

    Has anyone gotten Posgresql working in PHP using this method? I have added the “+postgres” and it does not compile with Posgres support.

    Any ideas?

  • kcs Says:

    I have tried adding postgre and it failed. However now the macports package is updated try typing ‘port version’ it should be at least 1.600 (as of 2008 may 8th)

    I will try again as I have time to play with it. Postgre has several versions now included in the macports packages (port search postgresql yield 8.0, 8.1, 8.2 and 8.3 version) and I do not yet know with which version(s) the PHP 5.x distro is compatible.

    Maybe you can have a look around here

    http://www.entropy.ch/software/macosx/php/

    and probably using the “pacifist” app you can get the postgre module out of it, and include in the built in os x apache version (my tutorial does that with the macports packages)

    I’ll let you know once I have managed to do it. Or something similar

  • fresh off the press Says:

    I am new to this. And i started by turning web sharing on and off in system prefs. Then i got into all this stuff and was enabling php5 by erasing that”#” sign at the beginning of that line. I pressed a couple of keys and want to make sure i did what i did right. Is there a way to check if php5 enabling went smooth and a general way to see if i may have screwed anything up?

    When i start up its a bit slower and when i try to repair disk permissions it will not do it. AND on top of all that i never really got web sharing up and running and was not able to host anything. Yeah i’m in over my head but i like learning and want to know what is up (and don’t want to screw anything up).

    Any help anywhere?

    thanks you so much

  • kcs Says:

    Well, open up the /Applications/Utilities/Console app in OS X, fold out the apache2 ‘folder’, and look for the error_log line. choose that one, and then leave it open. now clear display, then go to web sharing and try to turn on web sharing. some lines should scroll through in the console window. they should help in deciding what’s wrong (many things could go wrong) - but if you can’t make anything out of it, just post them here - and i will look at it for you.

  • jason Says:

    hi im really surprised this worked so well… up until i attempted to restart apache and i got:

    httpd: Syntax error on line 114 of /etc/apache2/httpd.conf: Cannot load /opt/local/apache2/modules/libphp5.so into server: dlopen(/opt/local/apache2/modules/libphp5.so, 10): Library not loaded: /opt/local/lib/libaprutil-1.0.dylib\n Referenced from: /opt/local/apache2/modules/libphp5.so\n Reason: Incompatible library version: libphp5.so requires version 4.0.0 or later, but libaprutil-1.0.dylib provides version 3.0.0

    any ideas what is happening? thanks!

  • Elise van Looij Says:

    It’s a really attractive approach, this, use the apple Apache server and use MacPort to get the cool php. I’ve been using it for almost a year now (using other bloggers’ instructions), but I’m giving up and going back to an all MacPort AMP installation. The reason: I just kept running into all kind of problems like the commenters above described: modules not loading, incompatible architectures, permission conflicts (Drupal’s clean urls categorically refuses to work on my localhost), in short, a new problem every two months or so, and it simply takes too much time and energy.

  • kcs Says:

    You are absolutely right, that’s why I updated this post. I now suggest anyone to use XAMPP for OSX. It does work our of the box - even with drupal ;) (I also use drupal quite a bit). Macports is also fine, but once you work on multiple computers, xampp is just easier (no need to compile, etc. macports can also be copied, but you know, it seems easier to me)

  • blog.usrbin.de Says:

    Blitz und Indianerverschlüsselung…

    Ein Dankeschön an pengekcs für diese coole How-To wie man SSL für den Apache 2 aktiviert.

    Sehr zu empfehlen, wenn man lokal SSL-verschlüsselte Seiten mit Flash testen muss, in denen der Flashfilm Daten nachlädt.
    Denen sollte man nämlich folge…

Leave a Reply