Installing PHP 5.3 using MacPorts

I stopped using macports ages ago, if you aren’t using vagrant or something similar then you probably want to check out homebrew as an alternate mac package manager.

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.

Updates:

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 http://svn.macports.org/repository/macports/trunk/base/
cd base
./configure
make
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" libphp5.so

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"

to:

DocumentRoot "/Library/WebServer/Documents"

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

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

to

<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
</IfModule>

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. http://testing.example.com. 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.

127.0.0.1    localhost
127.0.0.1    testing.example.com

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

<VirtualHost *:80>
    ServerAdmin webmaster@testing.example.com
    DocumentRoot "/opt/local/apache2/vhosts/testing.example.com"
    ServerName testing.example.com
    ServerAlias www.testing.example.com
    ErrorLog "logs/testing.example.com-error_log"
    CustomLog "logs/testing.example.com-access_log" common
</VirtualHost>

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/testing.example.com
cd /opt/local/apache2/vhosts/
sudo chmod -R 755 testing.example.com

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 http://testing.example.com) 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

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

  • Your style is very unique in comparison to other people I have
    read stuff from. Many thanks for posting when you’ve got the
    opportunity, Guess I’ll just book mark this site.

    Feel free to visit my blog post … legitimate make money online programs

  • Howdy! I know this is kind of off topic but I was wondering which blog platform are you using for this site?

    I’m getting sick and tired of WordPress because I’ve had issues with hackers and
    I’m looking at options for another platform.
    I would be great if you could point me in the direction of a good platform.

  • The particular Outlook Connector is limited to email, calendar plus contacts.

  • Having read this I believed it was really informative.
    I appreciate you spending some time and energy to put this short
    article together. I once again find myself spending
    a lot of time both reading and leaving comments.
    But so what, it was still worth it!

  • So one or the other watched the video, but there
    was no comment. Antarctic and Arctic species of octopi can survive extreme
    low temperatures in the waters they live in due to a different structure of a oxygen transport
    pigment in octopi blood according to research conducted by
    Michael Oellermann, Hans P. Do not try to attempt going
    to an outdoor adventure without getting enough skills about
    the exact first aid solutions.

    my web site :: reviews on Blackoutusa

  • It is really a great and helpful piece of information. I’m happy that you shared this helpful
    information with us. Please stay us up to date like this.
    Thanks for sharing.

  • This is the right webpage for anybody who wishes to find out about this
    topic. You know so much its almost hard to argue with you (not that I really would want to…HaHa).
    You definitely put a brand new spin on a subject which has been written about for ages.
    Great stuff, just great!

  • Informative article, exactly what I wanted to
    find.

  • People who have herpes deserve loving relationships in the same
    way anyone else does.

  • Valuable info. Lucky me I discovered your website unintentionally, and I am stunned why this
    coincidence did not came about in advance! I bookmarked it.

    Feel free to surf to my homepage; ios 8 untethered jailbreak

  • Choose a few quality ads and avoid anything thst flashes or shakes at the reader, which
    is very distracting and disruptive. This is especially
    true for website publishers and affiliate marketers that do not have high
    daily page impressions. The number of followers, delivery time and rates are
    three components which vary from one plan to another.

  • Ever wonder about the “great mystique” of being an inventor.
    Just like any other form of art, the style a picture is drawn in and
    the color it is given can affect the feel that it gives off.
    The new logo is only the two initials C and N in black and white colors.

  • After I originally commented I appear to have
    clicked the -Notify me when new comments are added-
    checkbox and from now on every time a comment is added I receive 4 emails with the exact same comment.
    Is there a means you are able to remove me from that service?
    Cheers!

  • After I originally commented I seem to have clicked on the -Notify me when new comments
    are added- checkbox and now every time a comment is added I get
    4 emails with the exact same comment. Perhaps there is an easy
    method you are able to remove me from that service?
    Cheers!

  • Hello to all, as I am actually eager of reading this website’s post to be updated
    regularly. It carries pleasant stuff.

  • Wszyscy pami?tajmy, i? zdrowe od?ywianie si? to grunt i musimy o tym pami?ta? zawsze.

    Zdrowa ?ywno?? to te? grzyby ró?nego rodzaju.
    Grzyby suszone, które musimy spo?ywa? z uwagi na du?? ilo?? witamin i ogólnie
    na dobre czynno?ci dla zdrowia cz?owieka (oczywi?cie
    pomijajmy grzyby truj?ce). Tego rodzaju grzyby mog? wyleczy? lub pomóc z walk? z chorobami ró?nego rodzaju przyk?adowo.
    To nie jest oszustwo, gdy? nie raz udowodniono ich dobry wp?yw dzi?ki ró?nego
    rodzaju eksperymentom. Kupujmy wi?cej grzybów leczniczych do których
    warto do??czy? boczniaki, pieczarki i inne. Gdzie kupi? takie grzyby?
    Oczywi?cie na naszej stronie!

  • As they had been placing their coats on Grandad noticed Uncle Tom
    had remaining his umbrella.

  • Glossing over some of the details of the original
    movie, Spirit of Vengeance picks up with Johnny Blaze (Nicolas Cage) laying low in a
    remote area of eastern Europe, trying to avoid human contact — particularly with those of an evil nature, who would trigger his demonic
    Rider alter ego to send them where the sun don’t
    shine. How to Download Free Movies & Music Safely is an article that
    will help you “to drastically lower the costs of your entertainment habits” with safe
    downloads of music and movies. On the previously discussed sites that offer links in order
    to websites which you will see movies, most of them allow the visitor to look through film name or even by type the industry great tool to find your
    favorite film extremely fast.

  • Hello just wanted to give you a brief heads up and let you know a few of
    the images aren’t loading correctly. I’m not sure why but I think its
    a linking issue. I’ve tried it in two different browsers and both show the same
    outcome.

  • Appreciation to my father who informed me concerning this weblog,
    this weblog is genuinely amazing.

  • My spouse and I stumbled over here by a different web address and thought I should check things
    out. I like what I see so now i am following you. Look forward
    to looking over your web page for a second time.

  • Have you ever considered about adding a little bit more
    than just your articles? I mean, what you say is valuable
    and all. But just imagine if you added some great pictures or video clips to give
    your posts more, “pop”! Your content is excellent but with images and videos, this site could certainly be one of the greatest
    in its field. Awesome blog!

  • I blog oftern and I seriously thank you for
    your information. Youur article has really peaked my interest.
    I will book mark your site and keesp chescking for new details about once a week.
    I opted in for your Feed too.

  • I blog frequently and I really thank you for your information.
    This article has truly peaked my interest.
    I will take a note of your website and keep checking for new information about
    once per week. I subscribed to your RSS feed as well.

  • Hotline Miami 2: Wrong Number is planned for release in 2014 on Pc, Mac, Linux, PlayStation 4 and PS Vita.

    my web site hotline miami Wrong Number pc download

  • Your style is really unique in comparison to other folks I have read stuff from.
    Thank you for posting when you’ve got the opportunity, Guess I’ll just bookmark this page.

  • Acting professional and singer Patrick Bruel was
    certainly one of France’s biggest stars over the ’90s, first making
    his / her name being a teen idol and leading a return to traditional French chanson inside
    new millennium. Bruel was given birth Patrick Benguigui
    within Tlemcen, Algeria, on, may 14, 1959. His / her father abandoned the family unit
    when Patrick was simply a year old, and 1962, after Algeria gained its independence, his mummy moved to France, negotiating inside the Paris suburb involving
    Argenteuil. An excellent soccer player in the youth,
    Patrick first decided upon the idea of being artist after seeing Michel Sardou perform in 75.

    As good fortune could have it, acting would get him his first accomplishment;
    first-time overseer Alexandre Arcady ran an advertising
    seeking a fresh man with a French-Algerian (or “pied-noir” in French slang)
    accent for his film Le Coup dom Sirocco. Benguigui (as having been still called)
    responded and gained the part. The year, he spent
    a while in New york, where he met Gérard Presgurvic, later to get
    his primary composer.

    Source: http://blog.nordicchoicehotels.com/no/se-gjore/guide-perfekte-svenske-midtsommerfeiringen/

  • Magnificent goods from you, man. I have understand your stuff previous to and you are just too great.
    I actually like what you have acquired here, really like what
    you’re saying and the way in which you say it.
    You make it enjoyable and you still take care of to
    keep it wise. I can’t wait to read far more from you.
    This is really a terrific website.

  • Hey there, You’ve done an excellent job. I’ll definitely digg it and
    personally recommend to my friends. I am confident they’ll be benefited from this
    site.

  • You are so awesome! I don’t suppose I’ve read a single thing like that before.

    So nice to find someone with unique thoughts on this subject.
    Really.. thank you for starting this up. This web site is one
    thing that’s needed on the web, someone with some originality!

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>