Skip to main content

Compile PHP 7 on Mac OS X 10.11 "El Capitain"


Apple has made a numerous changes to the way OS X (10.11) "El Capitain" uses open source elements like PHP and OpenSSL. Compiling PHP from source requires a bit more modifications.
This article is a follow-up on my previous post Installing PHP 7 with XDebug, Apache and MySQL on OS X Yosemite

OpenSSL

Get the latest OpenSSL from openssl.org and unpack it so you can work with the sources.
Then just execute:
./Configure shared darwin64-x86_64-cc
make depend
make
sudo make install

PHP 7

Download the latest PHP 7 source code from php.net/download and unpack it in a temporarily directory, I like to use /tmp.
My configure command arguments:
./configure \
--prefix=/opt/php7 \
--enable-cli \
--enable-mbstring \
--enable-gd-native-ttf \
--enable-gd-jis-conv \
--enable-sockets \
--enable-exif \
--enable-ftp \
--enable-intl \
--enable-soap \
--enable-zip \
--with-apxs2 \
--with-iconv=/usr \
--with-config-file-path=/etc/php7 \
--with-config-file-scan-dir=/etc/php7/ext \
--with-libxml-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/ \
--with-openssl=/usr/local/ssl \
--with-curl \
--with-gd \
--with-libedit \
--with-mcrypt=/usr/local/bin \
--with-mysqli \
--with-zlib \
--with-pdo-mysql \
--with-mysql-sock=/tmp/mysql.sock
This gives me most of the modules and extensions I require for doing my PHP work. For concrete situations I prefer to use virtual machines or Docker containers to mimic the production environment as much as possible, but for small work it's easy to have it running natively on my local machine.
WARNING: Compile OpenSSL instead of using the build-in SSL as it's not compatible!
Edit PHP's Makefile and find the line that begins with EXTRA_LIBS. In this line, replace the following references -lcrypto with /usr/local/ssl/lib/libcrypto.dylib and -lssl with /usr/local/ssl/lib/libssl.dylibor where you have installed the latest OpenSSL library.
A convenient way is to use vi and use :s/\-lcrypto/\usr\/local\/ssl\/lib\/libcrypto.dylib/g to immediately replace all instances of -lcrypto in the line. Do the same for -lssl as well.
Once done, just execute makemake test and sudo make install to install the latest PHP 7 version on your MacBook.
You might want to replace the build-in php with your new compiled version of PHP.
cd /usr/bin
sudo mv php php-orig
sudo ln -sf /opt/php7/bin/php php
When you execute php -v you should see something like the following:
PHP 7.0.8 (cli) (built: Jul 19 2016 11:36:18) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
    with Xdebug v2.4.0RC2, Copyright (c) 2002-2015, by Derick Rethans

Apache

It's nice to have PHP on command line, but you might want to have it run in your apache configuration as well.
If you look at the output of "make install" you will see the line:
[activating module `php7' in /private/etc/apache2/httpd.conf]
This means PHP has modified your apache configuration and included the php7 module, you only need to activate it.
In /etc/apache/httpd.conf find the following line:
LoadModule php5_module libexec/apache2/libphp5.so
And comment it out, as we're no longer use php 5, like this:
#LoadModule php5_module libexec/apache2/libphp5.so
Locate the line:
#LoadModule php7_module        libexec/apache2/libphp7.so
And remove the comment hash (#) in front of it
LoadModule php7_module        libexec/apache2/libphp7.so
Restart apache with sudo /usr/sbin/apachectl restart
Now put the following file (info.php) in your document root (see /etc/apache/httpd.conf where this is)
>?php phpinfo();
And point your browser to that file on your local machine, in my case it is http://localhost/info.php.
PHP 7.0.8 running on "El Capitain"


You're now ready to unleash the PHP7 power! Have fun and let me know in the comments if it worked out for you. If not, let's work together how we can solve the issue.

Comments

Popular posts from this blog

PHP 7 and Apache on macOS Sierra

I posted several talks about compiling PHP from source, but everyone was trying to convince me that a package manager like Homebrew was a more convenient way to install. The purpose of Homebrew is simple: a package manager for macOS that will allow you to set up and install common packages easily and allows you to update frequently using simple commands. I used a clean installation of macOS Sierra to ensure all steps could be recorded and tested. In most cases you already have done work on your Mac, so chances are you can skip a few steps in this tutorial. APACHE AND PHP WITH HOMEBREW I’ve made this according to the installation instructions given on GetGrav. The installation procedures These installation procedures will set up your macOS Sierra with PHP 7.1 and Apache 2.4. Install Xcode command line tools (if not done yet)xcode-select --install Install Homebrew/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" Set up for in…

Sessions in PHP 7.1 and Redis

In case you have missed it, PHP 7.1.0 has been released recently. Now you can’t wait to upgrade your servers to the latest and greatest PHP version ever. But hold that thought a second… With PHP 7 lots of things have changed underneath the hood. But these changed features can also put unexpected challenges on your path. Our challenge One of these challenges that we faced was getting PHP 7.1 to play nice storing sessions in our Redis storage. In order to store sessions in Redis, we needed to install the Redis PHP extension that not only provides PHP functions for Redis, but also installs the PHP session handler for Redis. Because we upgraded our servers to PHP 7.1, we were looking to use the latest provided version for this Redis extension: redis-3.1.0. Once installed, we bumped against a nasty problem. Warning: session_start(): Failed to read session data: redis (path: tcp://127.0.0.1:6379) Searching the internet for this error, we didn’t got many hits that could point us into a dire…

Speeding up database calls with PDO and iterators

When you review lots of code, you often wonder why things were written the way they were. Especially when making expensive calls to a database, I still see things that could and should be improved.
No framework development When working with a framework, mostly these database calls are optimized for the developer and abstract the complex logic to improve and optimize the retrieval and usage of data. But then developers need to build something without a framework and end up using the basics of PHP in a sub-optimal way.

$pdo = new \PDO( $config['db']['dsn'], $config['db']['username'], $config['db']['password'] ); $sql = 'SELECT * FROM `gen_contact` ORDER BY `contact_modified` DESC'; $stmt = $pdo->prepare($sql); $stmt->execute(); $data = $stmt->fetchAll(\PDO::FETCH_OBJ); echo 'Getting the contacts that changed the last 3 months' . PHP_EOL; foreach ($data as $row) { $dt = new \DateTime('2015-04-…