Urania

A blog named for the muse of Astronomy containing musings by an astronomer

SSH2 extension activation in PHP 5.3.0

Posted on October 13, 2009 by admin

This is an upgraded tutorial to getting SSH2 support under PHP 5.3.0 that I wrote a while back. Getting SSH2 support in PHP is useful for maintaining WordPress blogs. It would be nice if such an extension were not necessary, but this is the only way WordPress supports SSH administration through their web interface.

The big difference is that I will now be using the built-in Apache2 and PHP 5.3.0 under MacOS 10.6.1. I have gotten tired of the fact that everytime MacPorts updates it PHP installation, it tends to clobber the php.ini file for the site, requiring me to re-setup everything. Furthermore, I am also a bit tired of having MacPorts install its own version of everything I already have under MacOS X (such as Apache2, PHP, and MySQL). I understand their philosophy (and the practicality of it), but I am trying to keep my computer as clean as possible. As such, I am only using MacPorts to provide libssh2 to the SSH2 extension for PHP.

I noticed that the WordPress code had ssh2 support built-in, so all I need to is activate SSH2 support in the MacPorts installed PHP and I should be able to use SFTP in WordPress to handle the upgrades. I poked around and found this posting outlining the process for adding ssh2 support to Ubuntu. It guided me in developing this list of hints:

  1. If you haven’t already, start by installing libssh2 via MacPorts using the command:
    sudo port install libssh2
  2. Download the necessary SSH2 PHP extension using the build in PECL command
    sudo pecl download channel://pecl.php.net/ssh2-0.11.0
    The sudo is necessary because a lock file needs to be created during the download in /usr/lib/php, which is a protected directory. However, the file is downloaded to the current directory. Once the download is complete, we will need to unpack the SSH2 extension and go into its directory:
    tar xzvf ssh2-0.11.0.tgz
    cd ssh2-0.11.0
    If you try to do the normal phpize/configure/make sequence for compiling PHP extensions at this point, you will get a string of error messages ending with something like
    /private/var/tmp/apache_mod_php/apache_mod_php-53~1/Build/tmp/pear/download/ssh2-0.11.0/ssh2.c:1105: warning: passing argument 4 of 'add_assoc_stringl_ex' discards qualifiers from pointer target type

    This is occuring because there is an incompatibility between this ssh2 extension code and PHP 5.3.0. It can be patched by downloading the ssh2-php53.patch file from http://remi.fedorapeople.org/ssh2-php53.patch and applying the patch from the command line in the ssh2-0.11.0 directory
    curl -o ssh2-php53.patch http://remi.fedorapeople.org/ssh2-php53.patch
    patch < ssh2-patch53.patch
    Once you have done that, you can finish the SSH2 PHP extension compilation using
    phpize
    ./configure --with-ssh2=/opt/local
    make
    sudo make install
    The final command informed me the ssh2.so library was placed in /usr/lib/php/extensions/no-debug-non-zts-20090626/

  3. Now you need to make sure PHP loads the new module, so we open the PHP configuration file /etc/php.ini and edit the extension_dir line to point the extension directory above:
    extension_dir = "/usr/lib/php/extensions/no-debug-non-zts-20090626/"

    and then add the following line to the end of the section on “Dynamic Extensions”:
    extension=ssh2.so
    If you edited everything properly, a simple php -v from the command line should NOT trigger any errors.

  4. Finally, I restart the apache2 server so that the reconfigured PHP is loaded using
    sudo apachectl restart
    At this point, I checked (via the phpinfo(); command to see if the web server was supporting SSH. Near the bottom of the phpinfo(); listing is a listed of “Registered PHP Streams”. As noted here, it should incude “ssh2.shell”, “ssh2.exec”, “ssh2.tunnel”, “ssh2.scp”, and “ssh2.sftp”. If it does, you have enabled SSH support for Apache2 driven PHP pages under MacPorts.

  5. If you are doing this to get WordPress 2.7 automatic installation working, you will notice now when the automatic installation dialog box pops up, in addition to ftp and ftps, you now have an ssh option.

4 to “SSH2 extension activation in PHP 5.3.0”

  1. Derak says:
    I'm working with Mac OS X. I can install everything fine with no errors. When I type php -i | grep ssh2 I see ssh2 in my Registered PHP Streams. But with I go to my phpinfo() page in my browser ssh2 does not show up in my Registered PHP Streams. Any ideas?
  2. Ian says:
    Derak: have you checked to make sure that your CLI and your Apache PHP configurations both have the extension enabled?
  3. Derak says:
    I'm not sure how to do that? Sounds like that could be the problem though...
  4. Svolvær says:
    The SSH2 extension is crap. Not only is it poorly maintained but even if you do manage to get it working you're still going to run into problems with ssh2_exec() returning incomplete output. My recommendation: use phpseclib: a pure php ssh implementation.


Leave a Reply


  • Translate

  • Astro Pic o' the Day

  • Archives

  • Admin



↑ Top