How To Install Libssh2 On Windows
Introduction
This article describes a trouble that I ran into while developing an SSH application with PHP 8.0 on Windows 10 and the solution.
Environment
- Windows 10 Professional person
- XAMPP eight.0.6 – 2021-05-11
- PHP 8.0.6
Example Lawmaking:
| $ connection = ssh2_connect ( $ host , $ port , array ( 'hostkey' =& gt ; 'ssh-rsa' ) ) ; if ( ssh2_auth_pubkey_file ( $ connexion , $ username , $ filename_pub , $ filename_prv , $ filename_pwd ) ) { echo "Public Key Authentication Successful\n" ; } else { die ( 'Public Cardinal Authentication Failed' ) ; } |
The problem line of code:
| $ connectedness = ssh2_connect ( $ host , $ port , assortment ( 'hostkey' =& gt ; 'ssh-rsa' ) ) ; |
Generated this error:
| Fatal error : Uncaught Error : Phone call to undefined role ssh2_connect ( ) in ssh - copy - id . php : 17 Stack trace : #0 {chief} thrown in ssh - copy - id . php on line 17 |
I then checked the php.ini file to verify that the PHP extension extension=php_ssh2.dll was loaded. It was non.
I uncommented (enabled) the line and double-checked that php_ssh2.dll is located in the ext directory.
Then I received a different mistake:
| Alert : PHP Startup : Unable to load dynamic library 'php_ssh2.dll' ( tried : C : \ xampp \ php \ ext \ php_ssh2 . dll ( The specified module could not be found ) , C : \ xampp \ php \ ext \ php_php_ssh2 . dll . dll ( The specified module could not be found ) ) in Unknown on line 0 |
Hmm, the DLL is there, but I do not know where that DLL came from or its version.
I opened the directory in Windows Explorer, right-clicked on the file, and selected Properties. The details tab showed the following:
The DLL Production version is 7.4.13.
Some other method using PowerShell:
| powershell '(Get-Particular "./php_ssh2.dll").VersionInfo.ProductVersion' Output : 7.four.13 |
Which also shows the version as 7.4.13. PHP 8 probably needs a newer version.
Note: after in this article, I discover why. PHP has an Extension API version.
Solution
Going to the PECL download site for SSH2, I downloaded the latest package version, 1.3.1 for 64-bit Thread-Safe VS16. I chose VS16 because XAMPP version 8.0.vi specifies VS16. VS16 means the compiler packaged with Visual Studio 2019. [link]
Using the previous methods, I checked the version. SSH2 package version 1.3.ane contains DLL version viii.0.0. That should piece of work. Downloaded the cypher file, unpacked, and manually copied the DLL and PDB to the ext directory.
I then retried my programme and success:
| php ssh - copy - id . php Public Key Authentication Successful |
Detailed Information
XAMPP Version 8.0.6
Since the problem is on a system using XAMPP eight.0.6, I wanted to double-cheque that XAMPP did not have an result. This means creating a new system and installing XAMPP.
I created a virtual machine with Windows ten Professional and installed XAMPP 8.0.6. I so checked the C:\xampp\php\ext directory. The php_ssh2.dll was non found. This ways someone manually copied the DLL from an older installation to my problem system.
XAMPP was not the source of the trouble. Homo fault was nearly probable the source of the problem.
Detecting an extension
The following lawmaking volition notice if an extension is installed:
| if ( ! extension_loaded ( 'ssh2' ) ) { repeat 'Error: This program requires the PHP extension ssh2' . PHP_EOL ; leave ( one ) ; } |
Even so, this case does not prevent the PHP warning message if an extension is installed but does not load correctly:
| Alarm : PHP Startup : Unable to load dynamic library 'ssh2' ( tried : C : \ xampp \ php \ ext \ ssh2 ( The specified module could not exist found ) , C : \ xampp \ php \ ext \ php_ssh2 . dll ( The specified module could not be found ) ) in Unknown on line 0 |
PHP INI File
To determine the php.ini file location, execute this command:
Note: If you lot accept Apache configured with PHP, you volition have two or more php.ini locations. One for the CLI and one for Apache. In my case, I install PHP-FPM, then there is a 3rd.
For Apache, create a " not like shooting fish in a barrel to find " PHP file with the post-obit content:
And then load that file via URL in a web browser. The page volition contain the file location that Apache is using for php.ini. Once y'all are finished remove the file. The output contains sensitive information.
Finding PHP INI Files
PHP supports many methods to declare where the php.ini file is located.
PHP Runtime Configuration
PHP supports the surroundings variable PHPRC. [link] Example for Windows:
| set up PHPRC = C : \ PHP - DEBUG - CONFIGURATION php -- ini Configuration File ( php . ini ) Path : Loaded Configuration File : C : \ PHP - DEBUG - CONFIGURATION \ php . ini Scan for additional . ini files in : ( none ) Additional . ini files parsed : ( none ) |
Windows XAMPP:
On Windows, XAMPP uses the same location for the CLI and Apache.
XAMPP does use the directive PHPIniDir located in httpd-xampp.conf:
| & lt ; IfModule php_module & gt ; PHPINIDir "C:/xampp/php" & lt ; / IfModule & gt ; |
I use this command to search:
Linux/Ubuntu:
To search the file organization for php.ini files, install the package mlocate (Ubuntu) and execute this command:
Note: installing the mlocate parcel creates a database of files and a cron chore to update the database. If you practise not want some other background procedure running, use the find command example next.
On my Ubuntu xx.04 arrangement with PHP vii.4 installed, a number of php.ini files are located:
| / etc / php / 7.four / apache2 / php . ini / etc / php / seven.four / cli / php . ini / etc / php / 7.4 / fpm / php . ini / usr / lib / php / 7.4 / php . ini - development / usr / lib / php / seven.4 / php . ini - production / usr / lib / php / 7.4 / php . ini - production . cli |
Another command is which generates a similar output:
| sudo find / - iname "*php.ini*" |
PHP Extension Directory
On my Windows XAMPP installation, the PHP extension directory is C:\xampp\php\ext. This location is configurable in the php.ini file.
| extension_dir = "C:\xampp\php\ext" |
PHP Extension Details
1 item that I noticed with PHP viii.0 and 8.1. If yous specify a module 10 in php.ini, the CLI volition search for X and and then for php_X.dll. This ways that the php_ssh2.dll package can be specified as extension=ssh2
I found that php_ssh2.dll does not piece of work with PHP 8.1 on Windows. In analyzing this problem, I noticed several items output by php -i:
- PHP API => 20200930
- PHP Extension => 20200930
- Zend Extension => 420200930
- Zend Extension Build => API420200930,TS,VS16
- PHP Extension Build => API20200930,TS,VS16
I was non aware of an API version. The fault details from PHP 8.ane:
Warning: PHP Startup: ssh2: Unable to initialize module
Module compiled with module API=20200930
PHP compiled with module API=20210902
The string TS,VS16 means Thread-Safe Visual Studio 2019. That helps determine how to compile and build php_ssh2.dll.
Loading an extension at runtime
The CLI tin load PHP extension with a command-line option:
| php - d extension = C : / xampp / php / ext / php_ssh2 exam . php php - d extension = php_ssh2 examination . php |
This is useful while developing exception and error handling for extensions.
Unsolved Questions
Windows 10 Professional:
- Is this DLL compatible with PHP 8.one as that version is not mentioned?
- No. Preliminary testing results in the error:
- PHP Warning: PHP Startup: Unable to load dynamic library 'php_ssh2.dll' …
- This GitHub Issue reports the same result.
- No. Preliminary testing results in the error:
Ubuntu 20.04:
- Validate LIBSSH2 back up on Ubuntu 20.04 with PHP 8.0 and PHP8.1.
More Data
- PHP Secure Shell2
- LIBSSH2
- Bindings for the libssh2 library
- GitHub: Source lawmaking
Article Tags
Summary
To install the LIBSSH2 bindings for PHP 8 on Windows 10, complete the post-obit steps:
- Download the correct versions from hither.
- Excerpt the bundle and copy the php_ssh2.dll and optionally php_ssh2.pdf to your PHP ext folder. On my organization that is C:\xampp\php\ext.
- Edit the php.ini and uncomment or add the line extension=php_ssh2.dll.
Software development is more than just writing code. We need to know how to configure systems and document those procedures. Also important, we demand to write code or scripts to configure and examination our environments. Zippo is worse than working long hours, releasing a bang-up product that then becomes a client support nightmare.
Photography Credits
I write free articles near engineering. Recently, I learned about Pexels.com which provides free images. The paradigm in this article is courtesy of Pixabay at Pexels.
I design software for enterprise-class systems and data centers. My background is 30+ years in storage (SCSI, FC, iSCSI, disk arrays, imaging) virtualization. 20+ years in identity, security, and forensics.
For the by fourteen+ years, I have been working in the deject (AWS, Azure, Google, Alibaba, IBM, Oracle) designing hybrid and multi-cloud software solutions. I am an MVP/GDE with several.
Related Posts
Source: https://www.jhanley.com/php-8-setup-php_ssh2/
Posted by: strobelforombity91.blogspot.com

0 Response to "How To Install Libssh2 On Windows"
Post a Comment