PHP + SSH2 : How to enable SSH2 extension in PHP (WAMP)?

To connect SFTP (SSH File Transfer Protocol or Secure File Transfer Protocol) server from PHP (WAMP SERVER), first we have to enable SSH2 extension which is not available (by default) with WAMP server.

When I start with this, I got multiple solutions from google and it takes much time to find exact workable one. So I wrap my steps in this article and hope it will helps someone and save R&D time. Let’s start with simple below steps –

  1. Download required SSH2 library (check for PHP, Apache and SSH2 library compatible versions) from http://pecl.php.net/package/ssh2/0.12/windows OR https://windows.php.net/downloads/pecl/releases/ssh2/0.12/
  2. Extract the archive’s content and follow below steps –
    1. Stop WAMP Server, if already running.
    2. Placed php_ssh2.dll and php_ssh2.pdb files in the ext folder of PHP (wamp\bin\php_vx.x.x\ext).
    3. Copied libssh2.dll file to PHP folder (wamp\bin\php_vx.x.x).
    4. Add extension=php_ssh2.dll line in PHP ini file if not exists or remove ‘;’ from ;extension=php_ssh2.dll if already exists (wamp\bin\php_vx.x.x\php.ini).
    5. Copied libssh2.dll file to Apache folder (wamp\bin\apache\Apache_vx.x.x\bin).
    6. Add extension=php_ssh2.dll line in PHP ini file if not exists or remove ‘;’  from ;extension=php_ssh2.dll if already exists (recommended)(wamp\bin\apache\Apache_vx.x.x\bin\php.ini).
    7. Start/Restart All services of WAMP Server (wamp Server -> Restart All Services).
  3. When I was google for this, some articles suggest below steps/options also, but these were not work for me –
    1. Copied libssh2.dll file to C:\Windows\system32 and (if you got Win64) C:\Windows\SysWOW64 folder(s).
    2. Run the following command to register the DLL: C:\> regsvr32 libssh2.dll

After done with above steps, check

Is php_ssh2 extension is enabled or not?

(in-tray-icon) WAMP SERVER -> PHP -> PHP extensions -> php_ssh2

If it’s showing Danger Icon that means you did something wrong. Restart your computer once and check again otherwise recheck or retry with all above steps. (Please check version of PHP, Apache and SSH2 extension).

If everything is RIGHT then php_ssh2 extension shown with checked marked. Hurrah!!!

Now, try below sample code to check Is SSH2 extension working or not?

<?php
    $connection = ssh2_connect('XX.XX.XX.XX', 22);
    if(!ssh2_auth_password($connection, 'username', 'password')){
       thown new Exeception("Login failed");
    }
    var_dump($connection);

  //If everything is OK, then it will shows server details.
  //else, Fatal error: Call to undefined function ssh2_connect(), 
  //if its in your case, means still extension not enabled properly;
?>

That’s It.

You can get additional information related to SSH2 functions from PHP SSH2 Manual.

If you are interested, You can check my another related article, Connect SFTP server from PHP (WAMP Server) using phpseclib.

Thanks !! and If you have any suggestions or comments please share.

6 thoughts on “PHP + SSH2 : How to enable SSH2 extension in PHP (WAMP)?

  1. Pretty nice post. I just stumbled upon your blog and wanted to say that I’ve really enjoyed browsing your blog posts. In any case I?ll be subscribing to your feed and I hope you write again very soon!

    Like

Leave a comment