How to Install PHP SSH2 Extension in Amazon Linux AMI
Why we need SSH2 ?
To access a remote machine using the secure SSH and SFTP protocols.
Installing SSH2 for PHP7.X
You can find all PECL packages from https://pecl.php.net/package/ssh2
1 2 3 |
sudo yum install gcc make autoconf libc-dev pkg-config sudo yum install libssh2 pecl7 install ssh2 |
PECL command
You need to install php-pear
package to use PECL command.
1 |
yum install php-pear |
After installing the package, you may get the following message on the console.
No package php-pear available
Solution:
You need to add REMI
repositories to fix it.
1 |
sudo rpm -y https://rpms.remirepo.net/enterprise/remi-release-7.rpm |
If it is not enabled, please refer below how to add packages.
Dependencies to install if they are not already installed:
1 2 3 4 |
yum install php-devel yum install zlib-devel yum install pcre-devel yum install php7-pear |
Now, you can find the PECL to install extensions.
To verify that the command is available:
1 |
pecl7 -v |
1 |
pecl7 list |
If the plugin is installed successfully, don’t forget to add ssh2.so
/ ssh2.ini
files under php.ini
.
You can perform this manually or the following command. Amazon Linux may create ini
files with numbers like 34-ssh2.ini
or 40-ssh2.ini
.
1 |
sudo bash -c "echo extension=ssh2.so > /etc/php7.X/conf.d/xx-ssh2.ini" |
Then, restart the apache service.
1 |
sudo service httpd restart |
Configuring in Amazon Linux
In many cases, Amazon repository does not contain all the dependency information to install SSH2. If you aware of installing and enabling repositories, ignore this.
Install and enable the EPEL rpm package on RHEL 7 and Amazon Linux 2
1 |
sudo yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm |
Install and enable the EPEL rpm package on RHEL 6
1 |
sudo yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm |
Install and enable the EPEL rpm package on CentOS 7 64-bit, CentOS 6 64-bit, and CentOS 6 32-bit
1 |
sudo yum install -y epel-release |
Enable the EPEL rpm package on Amazon Linux.
The EPEL repository is already installed on the original version of Amazon Linux but you must enable it. You can enable this repository either by using the yum-config-manager
command or by editing the epel.repo
file.
Enable the EPEL repository on Amazon Linux by using the yum-config-manager
command
1 |
sudo yum-config-manager --enable epel |
To verify that the EPEL repository is enabled, run the following command:
1 |
sudo yum repolist |
Sample code for SFTP client
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
<?php class SFTPConnection { private $connection; private $sftp; public function __construct($host, $port = 22) { $this->connection = @ssh2_connect($host, $port); if (!$this->connection) throw new Exception("Could not connect to $host on port $port."); } public function login($username, $password) { if (!@ssh2_auth_password($this->connection, $username, $password)) throw new Exception("Could not authenticate with username $username " . "and password $password."); $this->sftp = @ssh2_sftp($this->connection); if (!$this->sftp) throw new Exception("Could not initialize SFTP subsystem."); } public function uploadFile($local_file, $remote_file) { $sftp = $this->sftp; $stream = @fopen("ssh2.sftp://$sftp$remote_file", 'w'); if (!$stream) throw new Exception("Could not open file: $remote_file"); $data_to_send = @file_get_contents($local_file); if ($data_to_send === false) throw new Exception("Could not open local file: $local_file."); if (@fwrite($stream, $data_to_send) === false) throw new Exception("Could not send data from file: $local_file."); @fclose($stream); } function scanFilesystem($remote_file) { $sftp = $this->sftp; $dir = "ssh2.sftp://$sftp$remote_file"; $tempArray = array(); if (is_dir($dir)) { if ($dh = opendir($dir)) { while (($file = readdir($dh)) !== false) { $filetype = filetype($dir . $file); if ($filetype == "dir") { $tmp = $this->scanFilesystem($remote_file . $file . "/"); foreach ($tmp as $t) { $tempArray[] = $file . "/" . $t; } } else { $tempArray[] = $file; } } closedir($dh); } } return $tempArray; } public function receiveFile($remote_file, $local_file) { $sftp = $this->sftp; // Local stream if (!$localStream = @fopen("$local_file", 'w')) { throw new Exception("Unable to open local file for writing: /localdir/$local_file"); } // Remote stream $stream = @fopen("ssh2.sftp://$sftp$remote_file", 'r'); if (!$stream) throw new Exception("Could not open file: $remote_file"); $read = 0; $fileSize = filesize("ssh2.sftp://$sftp/$remote_file"); while ($read < $fileSize && ($buffer = fread($remoteStream, $fileSize - $read))) { // Increase our bytes read $read += strlen($buffer); // Write to our local file if (fwrite($localStream, $buffer) === FALSE) { throw new Exception("Unable to write to local file: /localdir/$local_file"); } } fclose($localStream); fclose($stream); } public function deleteFile($remote_file) { $sftp = $this->sftp; unlink("ssh2.sftp://$sftp$remote_file"); } } ?> |
Thanks for sharing this great information. This is a great list for those people who looking Corporate Themes to build their website.
Great article, thank for sharing us.
Great blog, thank for sharing us.
nice blog, thank for sharing us.
Hello Nanpa which platform is used for web development..
Sorry. I don’t get you. Can you make it clear?