Installing PHP 5.3 using MacPorts

I’ve read a few posts on how to installed PHP on my mac using macports, but quite a few seem out of date or incomplete. I hope this is a guide on how I think it should be done from start to finish using easier to understand small steps.


28/08/09: Now works with Snow Leopard and this post has been updated reflect the latest variant changes to the PHP 5.3 install

03/09/09: Typo fixes and corrections from feedback

22/07/11: Updates for installing on OS-X 10.7 Lion

What? But my mac comes with PHP & Apache!

Well the first question should really be why use MacPorts instead of using the Apache/PHP that comes with OS-X. Basically the answer to that is that by default it’s not really setup for serious developer use, just the average end user who wants to play around with a script of hack together a small website. You could also use MAMP, but personally I don’t think it give me the level of control that I need as I try and develop with an environment as close to my production system that I can (i.e. a base unix environment). MAMP also hasn’t been updated for a while! Once you’re fully setup with MacPorts updating is a breeze and as the whole system has been compiled onto your machine in theory it should be a lot faster than a prebuilt universal binary.

Pre-Configure: Turn off the default web server

So the first thing you should do is to turn off Apache…

This is pretty simple, go to System Preferences, type Apache in it’s spotlight. It will highlight Sharing, but you just need to press return. Just make sure Web Sharing is disabled.

Setting up MacPorts

Install Xcode

Next you must make sure you’ve installed Apple’s Xcode Developer Tools (version 4.1 for Lion, version 3.1 or later for Leopard, or 2.4.1 or later for Tiger), found on the Mac App Store or at the Apple Developer Connection site or on your Mac OS X installation CDs/DVD. This basically installs a more complete unix developer base with compilers are other useful stuff needed for MacPorts.

Install MacPorts

After installing Xcode you can download and install MacPorts. The best way to install it is to use the DMG installer on the MacPorts site. On the other hand if you want to compile it from source then follow these steps:

Open up the Terminal

svn co
cd base
sudo make install

I recommend whichever way you install macports you should run this command to make sure everything is up to date:

sudo /opt/local/bin/port -v selfupdate

If nothing as gone wrong so far then you should have MacPorts fully setup. Only one more thing to do before installing PHP/MySQL and Apache

Update your system paths

Just so I don’t have to keep on typing /opt/local/bin all the time i’m going add that directory to my PATH. This information can be stored in your .profile which runs every time you start a new terminal. If you don’t have a .profile file use these steps to create one:

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

and that’s that. If you open a new terminal window (yes do that now) then those directories will be searched for terminal commands before the ones in the default path.

Setting up the LAMP stack

Enable SSL – if you want to!

Before installing PHP, i’m going to make sure that the version of curl installed is compiled with SSL enabled. If you don’t need or care about SSL then you can safely skip this step.

sudo port install curl +ssl

You should note that regardless if you run the command above first or one of the ones below then MacPorts will download a whole bunch or ports that are required to install that port or one of its dependencies. Also these commands may take sometime, so give it a while or go make a cup a tea.

Install MySQL

This one is fairly simple, well kind of…

sudo port install mysql5-server

Note: a lot of blogs recommend installing just the mysql5 port, but if you want the option to have MySQL start automatically at startup you’ll want to install the server. The mysql5-server port installs the mysql5 port anyway, so you can’t lose!

If you want to run MySQL at startup then run this command after the port finishes installing

sudo port load mysql5-server

Finally to make sure that the database if fully setup you must run this command (before starting up any mysql process!)

sudo -u _mysql mysql_install_db5

And make sure you pay attention to its instructions! Personally I didn’t do anything here as this is a development setup for me, so I prefer to keep my settings loose. Although if you want MySQL commands in your terminals path (like port) then run these commands and start a new terminal:

cd ~
echo 'export PATH=/opt/local/lib/mysql5/bin:$PATH' >> .profile

Create /opt/local/etc/mysql5/my.cnf, add the following to it and save

[mysqld_safe] socket = /tmp/mysql.sock

Also to remain compatible with other programs that may have been coded to look for the socket file in its original location then add this symbolic link:

sudo ln -s /tmp/mysql.sock /opt/local/var/run/mysql5/mysqld.sock

Install PHP & Apache

Ok we’re finally installing PHP with MySQL support and Apache (FastCGI is just being installed separately for use with some debugging and profiling tools).

sudo port install php5 +apache2 +pear +fastcgi php5-mysql +mysqlnd

Like MySQL if you want Apache to start automatically when you start your computer then run this command after the port installs (as shown while the ports are installing)

sudo port load apache2

Configure PHP

To customize php, copy /opt/local/etc/php5/php.ini-development (if this is a development server) or /opt/local/etc/php5/php.ini-production (if this is a production server) to /opt/local/etc/php5/php.ini and then make changes (see below).

i.e. for development:

cd /opt/local/etc/php5/
sudo cp php.ini-development php.ini

i.e. for production:

cd /opt/local/etc/php5/
sudo cp php.ini-production php.ini

To customize php edit (using your favourite editor) /opt/local/etc/php5/php.ini to configure it properly, make sure you set the correct timezone (for myself it’s this):

date.timezone = Europe/London

Configure Apache

If this is your first install, you need to activate PHP in your web server.To enable PHP in Apache, run

cd /opt/local/apache2/modules
sudo /opt/local/apache2/bin/apxs -a -e -n "php5"

Open /opt/local/apache2/conf/httpd.conf in your favourite text editor (it’s vi for me, but feel free to use pico, emacs or whatever!).

sudo vi /opt/local/apache2/conf/httpd.conf

If you want to change the default MacPorts Apache document root to match default Apple’s Apache (personally I didn’t, but I found this information on another blog – so it might be useful to some), change:

DocumentRoot "/opt/local/apache2/htdocs"


DocumentRoot "/Library/WebServer/Documents"

If you changed the DocumentRoot, change the Directory directive from:

<Directory "/opt/local/apache2/htdocs">


<Directory "/Library/WebServer/Documents">

For PHP to work correctly you must add index.php to the dir_module directive:

<IfModule dir_module>
    DirectoryIndex index.html index.php

Add a new mimetype so that Apache will direct files ending in .php to the PHP module for processing. Add the following within the <IfModule mime_module> block. Without this, all you’ll see is the text of your PHP scripts

AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps

And finally, to enable user directories, uncomment:

Include conf/extra/httpd-userdir.conf

But if you are going to use virtual hosts (as described below) then uncomment this line in addition to (or instead of) the line above:

Include conf/extra/httpd-vhosts.conf

Save and close the httpd.conf file.

Advanced Post-Configuration

Configure Apache Virtual Hosts

Now unless you just want to access your new web server using http://localhost you might want to set up some virtual hosts for Apache. I personally only need one virtual host atm i.e. Virtual hosts need to be configured in two places, the hosts file (/etc/hosts) and the Apache virtual hosts file (/opt/local/apache2/conf/extra/httpd-vhosts.conf).

Firstly add this line to /etc/hosts after the localhost has been defined i.e.    localhost

Then you must edit the virtual hosts file /opt/local/apache2/conf/extra/httpd-vhosts.conf and add this configuration

<VirtualHost *:80>
    DocumentRoot "/opt/local/apache2/vhosts/"
    ErrorLog "logs/"
    CustomLog "logs/" common

The document root can be anything you want as long as apache has permissions to read that directory i.e. in this case you can:

sudo mkdir -p /opt/local/apache2/vhosts/
cd /opt/local/apache2/vhosts/
sudo chmod -R 755

You can do this process for as many virtual hosts as you want. Also I personally keep all my websites stored in my user directory – yes bad on a server, but this is my personal development machine. Finally don’t forget to comment out or remove the example vhosts from http-vhost.conf or you’ll get warnings while starting up Apache, they are harmless to leave in, but it’s probably for the best to remove any potential errors

Installing extra PHP modules

There are plenty of extra PHP modules available from MacPorts, you can get a list of them by using this command. Although you won’t need any of the MySQL ones as you’ve already compiled it into PHP.

port search php5-

Here is the list of PHP5 ports I installed

sudo port install php5-openssl php5-curl php5-gd php5-iconv php5-http php5-mcrypt php5-xdebug

Updating MacPorts

MacPorts is simple to update, every few weeks I just run the command below to check for any outdated ports (i’ll also update this post with any changes to this process I discover)

sudo port outdated

to update all installed ports to the latests and greatest

sudo port upgrade outdated

That’s It

Now if you restart your computer and add code into the document root of your website (in this case then you should have a fully working web server with PHP and MySQL. Ok it wasn’t easy, but for me it’s the best solution over the longer term

70 comments to Installing PHP 5.3, Apache2 and MySQL5 using MacPorts

  • Some genuinely excellent info , Sword lily I found this. “Three things you can be judged by your voice, your face, and your disposition.” by Ignas Bernstein.

  • It is in reality a nice and useful piece of information. I’m glad that you simply shared this helpful info with us. Please stay us up to date like this. Thanks for sharing.

  • Methods to Move Data from Outlook 2011 to Windows Outlook:.
    You no longer can remove these troublesome programs from your computer,
    you do not have the permissions to see them, and you certainly don’t have the permissions to manipulate them. In extreme cases the Java freeze-ups error requires full uninstall and re-install of the plugin.

  • Hey there, I think your website might be having browser compatibility issues.
    When I look at your blog site in Ie, it looks fine but when opening in Internet
    Explorer, it has some overlapping. I just wanted
    to give you a quick heads up! Other then that, excellent blog!

  • I am not sure wherе уou’re getting your info, but great topic. I needs to spend some time learning more or understanding more. Thanks for magnificent info I was looking for this info for my mission.

  • I am extremely impressed together with your writing abilities as neatly as with the format to your blog. Is this a paid theme or did you modify it your self? Either way keep up the nice quality writing, it’s rare to see a nice weblog like this one these days..

  • I like it when people come together and share opinions. Great
    blog, continue the good work!

  • constantly sustain the actual rate for 2013 mentor hand bags, number more rapid upwards Baotui. enquired: madness. Most current mentor hand bags Are not able to discuss on the noses to have separate, proclaimed: Many thanks. If given a hand to your ex, lost his balance at Favorite Chloe pouch ‘s mind, possibly even as a result of inhibit lower! Meixin Yan Teng as well in the near future were standing away and additionally proclaimed: Eastern side Buddy people strange, utilizing only drooling giving java.

  • Excellent get the job done! This is the type of information that are meant to always be discussed over the net. Waste on bing with no lengthier placement this particular article second! Come on through along with speak with my web page. Thank you Implies)

  • Could You actually Seen Who Samsung Unveils Corby HandsetsSamsung has got intrdouced not one but two brand-new telephones in order to it has the to line of hardware Coby, typically the the younger generation during European countries that should be adedd. The fresh devices incllude typically the Coprby Corby (TXT) and also Coprby (PRO), which can be proposed beside typically the originaal Corby S3650-Mobile, currently availlable. The actual Cotrby (TXT) is as well knnown given that the B3210 and possesses any 2. 2-inch LCD, 2MP digital camera having training video assistance, bundled FM tuner, adjustable goes over, and also 40 MB associated with note of storage area expandable through a microSD unit card slot machine game. His phone works together ADVANTAGE GPRS networking sites Quad-band. The actual mobile phone handset might be availabe during European countries through to the terminate associated with Sept. The actual Crby (PRO) handet features a QWERTY kyboard and also runs to the 900 2100MHz HSDPA 7. 2Mbps interact. The actual phpone includes 2. 8-inch touchscreen display screen LCD assistance and any 3MP digital camera. The actual digital camera may also document any training video. Otehr benefits involve a powerful Radio having RDS, extractible go over, Wi-Fi, Bluuetooth, Mirco UNIVERSAL SERIAL BUS, 100 MB storage area having a microSD unit card slot machine game even more. The actual mobile phone handset will probably cruise ship during Nov. More or less everything Reasons for having The actual LG KP500 Cellular phone PhoneMobile phonees are generally around merely transmission tools. Nowadays, most people wish to be popular thewse tools, gammes gaming systems and also supplier associated with World-wide-web companies. Wonderful to find out that will thhese modest gadgets are generally effectively discover all of these valuable featurers. The actual LG KC910 is without a doubt one particular great telephones who are avaoilable to the mobile phone speaking economy. It pohne comes with a reflection that will finds any heavy imprint in your heads associated with potential customers. In case you are serious abotu typically the digital camera, which could thrust any couplpe associated with photographs associated with large consequently that LG phne is without a doubt mandatory for yourself. It gives you 8-megapixel digital camera certainly is the imzage a resolution associated with 3264 a 2448 pixels. Morover, that machine has also benefits enjoy automatic target, training video recordinng, and also vidoe digital camera contact. Gamerts will probably possibly be happpy this particular machine, while she has progressed efatures good performance. There are a lot of video game titles that you could participate in aytime and also anywhere you want to. Around the toher present, that machine will be dark colored coloor, that’s the most accepted colouring everywhere. From investing in that machine means that that you could give and also experience e-maipls having attachemnts within the far more rapiid manner. It phoone might be ideal for laborers whom frequently should determine ones own netmail whle they may be not really before ones own Computer systems. LG KP500 Cereal bar a second device coming from LG may be a proficient supplier. It shape Cereal bar cellphone has got several benefits that will undoubtedly earn typically the mpobile customers are generally dependnet with them. It amazing appliance will be a number of shades, enjoy dark colored, Vandyke red, anodized silcver and also golden sophisticated. May buy that cellphone during such shades. There are a number people that want to acquire telephones as stated by thheir favoruite shades and also horoscope. It communicationms machine features a digital camera 3. 2 huge pixels whicch typically the impression reslution associated with 2048 a 1536 pxels. It digital camera may take prime quality imaegs, as well as download and read tese photographs on the web, preserve on your pc or perhaps remove them associated with prrint and also, consequently, proposed these folks. Around standby form, it has the solar battery is without a doubt 350 days and also conversation that machine.

  • Amazing points totally, you merely gotten a whole new visitor. What might anyone advise in relation to the placed that you simply made a day or two before? Any favourable?

  • As a general rule charcoal grills are cheap than gas grills.
    6 out of 5, which shows that this gadget has met the
    expectations of its users in every way. You can interpret how elementary they are or which ones customers had difficulties with scene up.

    Visit my blog post … Miracle Grill Mat

  • Stefan

    Hi, thank you SO much for this guide! Saved my day.

    Just one thing:

    “Also to remain compatible with other programs that may have been coded to look for the socket file in its original location then add this symbolic link:

    sudo ln -s /tmp/mysql.sock /opt/local/var/run/mysql5/mysqld.sock

    Shouldn’t this be the other way around? Like sudo ln -s /opt/local/var/run/mysql5/mysqld.sock /tmp/mysql.sock
    For me, /tmp/mysql.sock did not exist, but the other one did, so this seemed more logical.

  • Good post! We will be linking to this great post on
    our website. Keep up the great writing.

    my homepage … stealth attraction full video [Norine]

  • Was In Fact checking around . I like your concepts. Do blog considerably more of this issue ok. Cheers

  • Both of these are true, but not relevant to our situation. With our economy lagging, the president should
    be jumping at any opportunity to create jobs instead of bending
    to the will of special (interests) at the expense of out of work Americans.
    that need to be learned in order to be prepared for the next
    Great Depression.

  • Hudson is believed to have fled after assaulting his ex-wife in her
    home two days after their divorce was finalized.
    It was the wail of a hyena that is lost in the woods. You briefly met Artanis in the Prophecy
    missions in the campaign (or Starcraft: Brood War if you played the first
    Starcraft’s expansion pack), and winning a daunting 1,000 team league matches as Protoss unlocks his portrait for you.

  • I wanted to thank you for this good read!! I absolutely enjoyed every little bit of it.
    I’ve got you book-marked to check out new stuff you post…

  • Many years ago, Net sports activities book betting was risky.
    ‘The teams, the broadcast contract and our showcase games this year are going to
    generate tremendous excitement about this League and the great seasons to come.
    In a very short time he built a very, very thorough
    roster that can beat you in a number of ways.

  • Fascinating blog! Is your theme custom made or did you download it from somewhere?
    A design like yours with a feww simple adjustements would really make
    my blog shine. Please let me know where you got your design. Many thanks

    Check out my page … deer hunting 2014 game cheats

Leave a Reply




You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>