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.

Support my Blog please!
  • Share/Bookmark
21 Responses to “Installing SSH2 for PHP shell connections – How-to”
  1. 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

  2. steveNo Gravatar says:

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

  3. 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 &&).

  4. Thankyou, thankyou, thankyou

  5. 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 :)

  6. 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

  7. JamesNo Gravatar says:

    thanks a million

  8. sylviaNo Gravatar says:

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

  9. 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

  10. 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 .

  11. 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

  12. 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).

  13. 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.

  14. 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.

  15. 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.

  16. 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.

  17. 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

  18. 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.

  19. 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]#

  20. 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.

  21. 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/

  22.  
Leave a Reply


Comment spam protected by SpamBam


Clicky Web Analytics