In order to be able to open ssh connections within a php script you will need a few things done, few modules installed on the server.

Mainly the install will need 3 packages: OpenSSL, libssh2 and ssh2 modules and of course root access to the server.

OpenSSL it is usually installed on many system so you may not need to install this, in order to make sure that you have this package installed or not run the following on the server:

[root@box1 ~]#rpm -qa |grep openssl

If the package is installed the output will show something like this:
openssl096b-0.9.6b-22.46
openssl-0.9.7a-43.17.el4_6.1
openssl-devel-0.9.7a-43.17.el4_6.1

The versions may deffer depending on the OS you have installed on the server.

If the output shows nothing then you need to install OpenSSL and this can be easy achieved using yum or apt-get(redhat or debian based servers):

yum install openssl (redhat based OS)

apt-get install openssl (debian based OS)

Second package needed is libssh2. You will not find this on your server usually and will have to be installed from the source like this:

[root@box1 ~]wget http://voxel.dl.sourceforge.net/sourceforge/libssh2/libssh2-0.18.tar.gz
[root@box1 ~]tar -zxvf libssh2-0.18.tar.gz
[root@box1 ~]cd libssh2-0.18
[root@box1 libssh2-0.18]# ./configure
[root@box1 libssh2-0.18]# make
[root@box1 libssh2-0.18]# make install

When all the compile is done libssh2 is installed.

Next in line will be ssh2 php module.
This can be installed in a few ways like using PECL or PEAR or by directly downloading and compiling the module yourself.

I will use the 3rd install type because lately using PECL or PEAR did not really do the job.

[root@box1 ~]# wget http://pecl.php.net/get/ssh2-0.10.tgz
[root@box1 ~]# tar -xzf ssh2-0.10.tgz
[root@box1 ~]# cd ssh2-0.10
[root@box1 ssh2-0.10]# phpize && ./configure --with-ssh2 && make

If this finishes without an error you are almost done, the only thing left to be done will be to copy the ssh2.so module from withing /ssh2-0.10/modules/ to the php extensions directory which you can find out where it is by checking the php.ini file.
At the end just restart apache and you are done, you can try your script to see if everything is alright.

If after running phpize && ./configure --with-ssh2 && make you end up with an error like make: *** [ssh2.lo] Error 1 you will have to do a small hack.

[root@box1 ssh2-0.10]# vi ssh2.c
Search for the line:
#if LIBSSH2_APINO < 200412301450
and edit it to look like:
#if LIBSSH2_VERSION_NUM < 0x001000
Save the file and run phpize && ./configure --with-ssh2 && make again.

If everything finishes fine you are done, check this how-to before the error part and that is all.

If there is anything that was left aside let me know.

35 Responses to “Installing SSH2 for PHP shell connections – How-to”
  1. Somnath JNo Gravatar says:

    I have installed php5.3.6 and also ssh2-0.11.0. But still php -m | grep -i ssh, doesnt shows up. I havent restarted my apache, is that the problem. If any extensions added needs apache restart?

  2. asifNo Gravatar says:

    dear i have an error after doing this

    [root@box1 ~]# wget http://pecl.php.net/get/ssh2-0.10.tgz
    [root@box1 ~]# tar -xzf ssh2-0.10.tgz
    [root@box1 ~]# cd ssh2-0.10
    [root@box1 ssh2-0.10]# phpize && ./configure –with-ssh2 && make

    i got an error that
    “phpize: command not found”

    please do help me soon
    thanks

  3. asifNo Gravatar says:

    dear i have an error after doing this

    [root@box1 ~]# wget http://pecl.php.net/get/ssh2-0.10.tgz
    [root@box1 ~]# tar -xzf ssh2-0.10.tgz
    [root@box1 ~]# cd ssh2-0.10
    [root@box1 ssh2-0.10]# phpize && ./configure –with-ssh2 && make

    i got an error that
    “phpize: command not found” please do help me soon

  4. Umer MansoorNo Gravatar says:

    Good article. However, I’d like to point out that you also need to add an entry to /php.d/ directory as follows:

    cat echo extension=ss2.so > /etc/php.d/ssh2.ini

    This works well on CentOS 6.

  5. BipinNo Gravatar says:

    Thanks Florian NNo Gravatar: For reply.

    Issue Was resolved just updated the lampp version .

    Thanks Again.

    Regards,
    Bipin Bahuguna
    Share The Knowledge.

  6. Florian NNo Gravatar says:

    @Bipin: did you upgraded php recently as if you did you must update the PHP extensions as well.
    You may want to try “pecl upgrade ssh2″ or “pear install -f pecl/ssh2″. If this doesn’t work download the latest ssh2 module and try and compile that one instead the one i posted in this post as this one is a bit older.

    Good luck.

  7. Warning: PHP Startup: ssh2:
    Unable to initialize module
    Module compiled with module API=20060613
    PHP compiled with module API=20090626
    These options need to match in Unknown on line 0

    What to Do.

    Thanks,
    Bipin Bahuguna

  8. Florian NNo Gravatar says:

    @Patrick:

    I would recommend making sure that gcc/g++ is installed, not only gcc.

    The error you are getting regarding libpicl seems to be coming from sunstudio, libpicl may need to be installed from a patch.

  9. PatrickNo Gravatar says:

    Thanks Florian, I really appreciate the reply.

    Still getting same error. I had actually already tried reinstalling gcc prior to posting, but did it again:
    % gcc -v
    Reading specs from /usr/local/lib/gcc/sparc-sun-solaris2.8/3.4.6/specs
    Configured with: ../configure –with-as=/usr/ccs/bin/as –with-ld=/usr/ccs/bin/ld –enable-shared –enable-languages=c,c
    ++,f77
    Thread model: posix
    gcc version 3.4.6

    I went ahead installed sun studio and it looks like it is finding it in the config.log now:

    configure:2905: checking for cc
    configure:2921: found /opt/SUNWspro/bin/cc
    configure:2932: result: cc
    configure:2963: checking for C compiler version
    configure:2972: cc –version >&5
    ld.so.1: cc: fatal: libpicl.so.1: open failed: No such file or directory
    ./configure: line 1: 11756 Killed $CC –version 1>&5
    configure:2983: $? = 137
    configure:2972: cc -v >&5
    ld.so.1: cc: fatal: libpicl.so.1: open failed: No such file or directory
    ./configure: line 1: 11761 Killed $CC -v 1>&5
    configure:2983: $? = 137
    configure:2972: cc -V >&5
    ld.so.1: cc: fatal: libpicl.so.1: open failed: No such file or directory
    ./configure: line 1: 11766 Killed $CC -V 1>&5
    configure:2983: $? = 137
    configure:2972: cc -qversion >&5
    ld.so.1: cc: fatal: libpicl.so.1: open failed: No such file or directory
    ./configure: line 1: 11771 Killed $CC -qversion 1>&5
    configure:2983: $? = 137
    configure:3003: checking whether the C compiler works
    configure:3025: cc conftest.c >&5
    ld.so.1: cc: fatal: libpicl.so.1: open failed: No such file or directory
    ./configure: line 1: 11781 Killed $CC $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS 1>&5
    configure:3029: $? = 137
    configure:3067: result: no

    Not sure how to add libpicl.so.1: ??

  10. Florian NNo Gravatar says:

    @Patrick:

    Check if glibc and g++(gcc-c++) are installed, seems that the C compiler doesn’t have all packages installed from the error. You may need libc6-dev. Since you are on SunOS you may want to try SunStudio from Sun, it’s free btw.

  11. PatrickNo Gravatar says:

    Have really been struggling with this. This is one of the better online threads on the subject hoping someone can help me fix this issue. Need to install ssh2 mofule for php. Getting the following error:

    % phpize && ./configure -with-ssh2 && make
    Configuring for:
    PHP Api Version: 20090626
    Zend Module Api No: 20090626
    Zend Extension Api No: 220090626
    configure.in:3: warning: prefer named diversions
    configure.in:3: warning: prefer named diversions
    checking for grep that handles long lines and -e… /usr/xpg4/bin/grep
    checking for egrep… /usr/xpg4/bin/grep -E
    checking for a sed that does not truncate output… /usr/local/bin/sed
    checking for cc… cc
    checking whether the C compiler works… no
    configure: error: in `/usr/local/bin/ssh2-0.11.0′:
    configure: error: C compiler cannot create executables
    See `config.log’ for more details

    Following info may be helpful:

    % uname -a
    SunOS log1b 5.8 Generic_108528-22 sun4u sparc SUNW,Sun-Blade-1500

    % pkginfo | grep ssl
    application SMCossl openssl

    % pkginfo | grep ssh
    application SMClssh2 libssh2
    application SMCosh581 openssh

    % pkginfo | grep gcc
    application SMCgcc gcc
    application SMClgcc libgcc

    Anyone have a any idea why I am getting this??

  12. deepthi_ksNo Gravatar says:

    hi,
    i followed the exact same steps and everything went gr8…except when i run php script that has ssh functions …i get this error
    PHP Warning: PHP Startup: Unable to load dynamic library ‘./ssh2.so’ – ./ssh2.so: cannot open shared object file: No such file or directory in Unknown on line 0

  13. deepthi_ksNo Gravatar says:

    This is the best article i have ever read on web in the lines of linux installations.
    Helped a lot. It worked for me. THANX A LOT :D

  14. actortongNo Gravatar says:

    If you can’t get ssh2.so installed phpseclib – a pure-PHP SSH2 implementation – may work for you:

    http://phpseclib.sourceforge.net/

  15. Florian NNo Gravatar says:

    That error “error: expected ‘{‘ at end of input” suggests that there is a coding problem in the module which may or may not be true.

    The problem is that i can’t really do much about this without hands-on and i can’t seem to duplicate the issue on one of my systems.

  16. vijay bhartiNo Gravatar says:

    Hi Florian,
    Please help.with ssh2-0.10,same type of error is coming:

    /root/ssh2-0.10/ssh2.c:1268: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘ssh2_functions’
    /root/ssh2-0.10/ssh2.c:1324: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘ssh2_module_entry’
    /root/ssh2-0.10/ssh2.c: In function ‘ZEND_GET_MODULE’:
    /root/ssh2-0.10/ssh2.c:1343: error: expected ‘{‘ at end of input
    make: *** [ssh2.lo] Error 1
    [root@pcssgdell ssh2-0.10]#

  17. Florian NNo Gravatar says:

    Maybe try and use ssh2-0.10 package. Seems that you tried using the latest available sources but this may have some issues.

  18. vijay bhartiNo Gravatar says:

    Hi Florian,
    After using above command I can see PWD /usr/src/redhat/SOURCES/libssh2-1.1 in phpinfo(),but still same error coming.

    I also have another server with php 5 on that I am able to install libssh2-1.2.1 but when I am installing ssh2-0.11.0,below error is coming:

    /root/ssh2-0.11.0/ssh2.c:1276: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘ssh2_functions’
    /root/ssh2-0.11.0/ssh2.c:1332: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘ssh2_module_entry’
    /root/ssh2-0.11.0/ssh2.c: In function ‘ZEND_GET_MODULE’:
    /root/ssh2-0.11.0/ssh2.c:1351: error: expected ‘{‘ at end of input
    make: *** [ssh2.lo] Error 1

    is there any way of compiling this file without error for PHP 5.2.9 .Please advice soon

  19. Florian NNo Gravatar says:

    Hmm, try and recompile ssh2 again but use the configure command like this: ./configure –with-ssh2 –with-php-config=/path/to/correct/php/bin/php-config . See if this will work.

    I would really recommend you to change to PHP5 if possible.

  20. vijay bhartiNo Gravatar says:

    Hi Florian,
    Below is the error in /var/log/httpd/error_log file:
    [Mon Jan 11 22:41:52 2010] [notice] Digest: done
    PHP Warning: Unknown(): Unable to load dynamic library ‘/usr/lib/php4/ssh2.so’ – /usr/lib/php4/ssh2.so: undefined symbol: php_network_connect_socket_to_host in Unknown on line 0.
    But ssh2.so is in /usr/lib/php4 directory.
    Please advice.

  21. Florian NNo Gravatar says:

    Anything showing in the apache logs, any errors? It’s a bit hard for me to investigate the problem at this point without hands-on but the issue looks to be even related to a compatibility issue(there was a bug in php4 in regards to ssh2.so) or the module not being loaded right.

  22. vijay bhartiNo Gravatar says:

    Hi Florian,
    Yes everything compiled without error,only I changed as mentioned above #if LIBSSH2_VERSION_NUM < 0×001000.My extension directory is /usr/lib/php4 and ssh2.so file is inside that directory.still same problem.

  23. Florian NNo Gravatar says:

    Well if the ssh2 module does not show up under phpinfo then it is not loaded for some reason, are you sure that everything compiled without error? Also try and run “updatedb” and then “locate ssh2.so” on the server and see if the module is being found and if it is make sure that is is under your extensions path(you can see in phpinfo where it is).

  24. vijay bhartiNo Gravatar says:

    Hi,
    I have php version PHP Version 4.3.2.I check phpinfo(),ssh2 module is not there.I already enable extension=ssh2.so in my php.ini file. but still ssh2_function does not exist is coming

  25. Florian NNo Gravatar says:

    What php version are you running on the server, you may need the newer ssh2 module if running php 5.3 : http://pecl.php.net/get/ssh2-0.11.0.tgz . Try and create a phpinfo file and see if the module shows up in there..
    Also check that the ssh2 module is enabled under php.ini if it’s not add this line to php.ini: extension=ssh2.so .

  26. vijay bhartiNo Gravatar says:

    I installed php ssh2 module based on steps given above,everything goes ok still getting error function ssh2_connect doesn’t exist on red hat linux 3

  27. sylviaNo Gravatar says:

    Thank you. I kept running into errors with \pecl install ssh2\. Your tutorial saved me a lot of googling!

  28. JamesNo Gravatar says:

    thanks a million

  29. Florian NNo Gravatar says:

    Hey Ruben,

    Thanks for the comment :) .

    Not really a master, is a long way till there, but i know my way around google and linux or i like to think that i do :D .

    Regarding your question check the first URL in this google search: Google Link

  30. Rubén OrtizNo Gravatar says:

    Hi

    thank by your howto, had the complete steps to a good installation. However, Im courious about something, How could you find the way to solve the error on make?

    Bloody error. You are a master, aren’t you?

    Thanks :)

  31. Thankyou, thankyou, thankyou

  32. Florian NNo Gravatar says:

    Do you get any error when running with two “–” ? This should be the correct syntax.

    By the way if there is an issue try and run every build command separate and let the parallel build out(using &&).

  33. steveNo Gravatar says:

    erm that did not work? there should be 2 – signs before – - with.

  34. steveNo Gravatar says:

    Your instructions
    [root@box1 ~]# cd ssh2-0.10
    [root@box1 ssh2-0.10]# phpize && ./configure –with-ssh2 && make
    there is 1 -with instead of –with
    great article thanks
    s

  35.  
Leave a Reply