Upgrading PHP on Mac and Ubuntu

In this post we list the steps of installing php 5.4, however, the steps are the same for any version. You may have to choose different binary names but for those you can look up in macports.com. In addition, once multiple versions of php binaries installed, it is easy to switch between the versions. It involves only two steps – activate proper version binaries all of which covered in this post


Step-1 Installing PHP 5.4
We use macports packaging management system to fetch artifacts

sudo port selfupdate
sudo port install php54
sudo port install php54-gd
sudo port install php54-mysql
sudo port install php54-apache2handler
sudo port activate php54
sudo port activate php54-gd
sudo port activate php54-mysql
sudo port activate php54-apache2handler

The activate commands may not need to be run since its auto activate when running ‘port install’, however. We list here for information

Step-2 Enable PHP in Apache
When installing php54-apache2handler module via MacPorts, it downloads php54.so binary for apache and places it in the /path/to/apache2/modules folder(in our case it is /opt/local/apache2)

So, you have to tell Apache load the new php54 version binary by replacing following lines in apahce configure file http.conf (/path/to/apache2/http.conf) as following:

LoadModule php5_module libexec/apache2/libphp5.so


LoadModule php5_module modules/mod_php54.so

Afterward, restart Apache service. This ensure that the php module is loaded.

Note: you may want to confirm that the new module ‘mod_php54.so’ with proper permissions is present (for mac 10.7 in /path/to/apache2/modules)

Step-3 Customize PHP

cd /opt/local/etc/php54
cp php.ini-development php.ini

Step-4. Verify Php Version
To verify, run:

php -v

The version may be different. This happens if you have php already installed, then you have to remove the default PHP configuration:

which php
sudo rm /usr/bin/php
sudo ln -s /path/to/php/executable php

Here, we removed the pointer to the old php executable and create reference to the new one. MacPorts downloads the new php executable in /opt/local/bin directory and name it ‘php55’

Aftewards, the ‘php -v’ should match with version you installed.

Switching PHP Version
To switch between versions you would run:

sudo port select --set php php54

And then updating /path/to/apache2/http.conf to load proper *.so module for the particular PHP

LoadModule php5_module modules/mod_php54.so

To see what php modules available and their exact naming see ‘/path/to/apache2/modules’

To see MacPorts logs

To see MacPorts logs


Uninstalling before upgrading
You may have to uninstall before you can install newer version. If so, make sure first deactivate/uninstall dependencies i.e. php5-gd and php5-mysql. Please see more
How to Deactivate One Macport Variant and Switch to Another One


For ubuntu

sudo apt-get install python-software-properties
sudo add-apt-repository ppa:ondrej/php5-oldstable
sudo apt-get update
sudo apt-get install php5
sudo apt-get install php5-mysql
sudo apt-get install php5-curl
sudo apt-get install php5-gd

The ‘php5-oldstable’ is for PHP 5.4. For the php5.5 there is ppa:ondrej/php5, however. The repository ppa:ondrej/php5 doesn’t seem to have lucid Ubuntu distribution as well it is upgrading to Apache2.4 which a lot of changes.

The ‘python-software-properties’ is installed in order to use ‘add-apt-repository’ command, however. you as well manually can add the repository at /etc/apt/sources.list as following:

deb http://ppa.launchpad.net/ondrej/php5-oldstable/ubuntu lucid main
deb-src http://ppa.launchpad.net/ondrej/php5-oldstable/ubuntu lucid main

If you may need to uninstall php on ubuntu:

sudo apt-get -y purge php*

or more conservative approach:

sudo apt-get -y purge libapache2-mod-php5 libapache2-mod-php5filter libexpect-php5 libgv-php5 libow-php5 php5 php5-adodb php5-auth-pam php5-cgi php5-cli php5-common php5-curl php5-dbg php5-enchant php5-exactimage php5-ffmpeg php5-fpm php5-geoip php5-gmp php5-idn php5-imagick php5-imap php5-interbase php5-intl php5-json php5-lasso php5-ldap php5-librdf php5-mapscript php5-memcache php5-memcached php5-mhash php5-midgard2 php5-ming php5-mssql php5-mysql php5-mysqlnd php5-odbc php5-pgsql php5-ps php5-pspell php5-radius php5-recode php5-remctl php5-rrd php5-sasl php5-snmp php5-sqlite php5-suhosin php5-svn php5-sybase php5-tidy php5-tokyo-tyrant php5-uuid php5-xcache php5-xdebug php5-xmlrpc php5-xsl

You can also search for all php packages as following:

sudo aptitude search php5|awk {'print $2'}|grep -v i386|grep -v "^A"|tr "\n"  " "


1. How to locate a dir from which the Apache is currently running on your machine

ps -ef | grep apache

2. How to locate a dir from which the current installation of PHP is running from on machine
Create php config info file – phpinfol.php and place the following.

<?php phpinfo(); ?>

Afterwards, open this file in the browser


1. Page Not Found

After doing everything and pointing to phpinfo.php, the browser returns page not found. the issue was that it was loading the php module from the wrong dirrectory. The http.conf was containing:

LoadModule php5_module modules/mod_php54.so

But needed to be:

LoadModule php5_module libexec/apache2/mod_php54.so
2. Exception: Stripe needs the CURL PHP extension. in require_once()[…]

One of the libraries(Stripe) is using CURL, so i forgot to install. To do so:

sudo port install php54-curl

For Ubuntu

sudo apt-get install php5-curl

This also activates the php-curl, so all is left to restart the apache server. Depending on you PHP Version, there may be different php-curl module to install. Please see macports.com for list of packages and versions

3. Stripe needs the Multibyte String PHP extension

Here the Stripe module is using MultiByte String lib that is missing. To install

sudo port install php54-mbstring  

Depending of your PHP Version, you may have to install a different version of mbstring lib. Afterwards, make sure to restart the apache server

3.PDOException: SQLSTATE[HY000] [2002] No such file or directory in lock_may_be_available()

(line 167 of /Users/maxit/Sites/drupal/commerce/first-commerce/includes/lock.inc)

By default, mysql is configured not to use TCP connection(particular for php55), so your sites will break. For solution see post Installing Mysql On Mac for ‘ERROR 2003 (HY000): Can’t connect to MySQL server on’