If you want to use SSH or SFTP with PHP you need the SSH2 extension. Unfortunately MAMP doesn’t come with it out of the box. Last year I had written about how to add the SSH2 extension to MAMP, on PHP 5.6. In the meantime two major changes ocurred which pretty much nullified the process: OS X El Capitan was released requiring new prerequisites to be installed and PHP 7 was included which requires an entirely new approach to installing SSH2 (it’s no longer as simple as using pecl). In this article we’ll discuss the process required to get the SSH2 extension installed on PHP7 in MAMP.
OS X 10.11 El Capitan pre-requisites
OS X 10.11 El Capitan no longer includes the development headers for the OpenSSL library which are required to build libssh2. Therefore, we have to install a copy of the library using Homebrew:
brew update brew install openssl
This will install OpenSSL in the directory /usr/local/opt/openssl
wget https://www.libssh2.org/download/libssh2-1.7.0.tar.gz tar xvzf libssh2-1.7.0.tar.gz cd libssh2-1.7.0 ./configure --with-libssl-prefix=/usr/local/opt/openssl make sudo make install
Important note: the
--with-libssl-prefix=/usr/local/opt/openssl part should only be used if you followed the instructions of the previous section for installing OpenSSL through Homebrew. Otherwise provide the directory where the OpenSSL library is located on your system.
If you are missing autoconf you may try to install it using Homebrew:
brew install autoconf
Make it possible to use pecl with MAMP
You need to download the PHP sources matching your PHP version. In the case of MAMP that’s still PHP 7.0.0. Therefore we need to do the following:
cd /Applications/MAMP/bin/php/php7.0.0/ wget http://de1.php.net/get/php-7.0.0.tar.bz2/from/this/mirror -O php-7.0.0.tar.bz2 tar xjf php-7.0.0.tar.bz2 mv php-7.0.0 php mkdir include mv php include/ cd include/php ./configure
Install SSH2 and activate it
The SSH2 module is only available through PECL for PHP 5. PHP 7 has a different API for modules, meaning that the SSH2 module cannot be compiled. That would be a disaster if it wasn’t for some kind folks who picked up the development and ported the module to PHP 7. We will need to download, compile and install the PHP 7 module for SSH2:
wget https://github.com/Sean-Der/pecl-networking-ssh2/archive/php7.zip cd pecl-networking-ssh2-php7 phpize ./configure make sudo make install
Now open MAMP Pro’s main window and hit ⌘-4 or go to File ‣ Edit Template ‣ PHP 7.0.0. Find the Extensions section and add the line
Save the file, close the editor window and restart Apache. To confirm that everything is installed launch a phpinfo() page and loog for ssh2. It should be present.
For the CLI executable of PHP you also need to edit another file,
/Applications/MAMP/bin/php/php7.0.0/conf/php.ini Again add the line
To confirm that everything is installed porperly for the CLI PHP binary do
php -i | grep ssh
you should get something like
Registered PHP Streams => https, ftps, compress.zlib, compress.bzip2, php, file, glob, data, http, ftp, phar, zip, ssh2.shell, ssh2.exec, ssh2.tunnel, ssh2.scp, ssh2.sftp ssh2 libssh2 version => 1.7.0