In one of my project, I had to execute shell script on remote machine using SSH protocol. At first I was using simple PHP exec which executing ssh command on the local web server, but it’s requiring apache user used to running web server to be unlocked, which is very difficult to do on production server.
Then I stumbled upon on ssh2_connect which is exactly what I actually looking for. It can authenticate using password or public key and have few basic function such as scp and chmod. It’s part of PHP which mean any Zend Server installation can configured to use it.
First check if package php-5.3-ssh2-zend-server has already installed. On CentOS 6.2:
yum install php-5.3-ssh2-zend-server
Replace 5.3 with your PHP version.
If everything’s okay enable it. Open file /usr/local/zend/etc/conf.d/ssh2.ini and remove ; to uncomment the line which tell PHP to load ssh2 library.
Last step, restart apache server:
You should be able to use any ssh2 function now.