PHP SSH2 在 Windows 服务器上的连接配置与实战指南
你好!作为深耕电脑领域的老手,我见过太多开发者在用 PHP 开发自动化脚本时卡在 PHP SSH2 这个扩展上。Windows 服务器本身不带 SSH 服务,默认用的是 Remote Desktop,PHP 通过 ssh2 扩展可以轻松搞定远程命令执行、文件上传下载、批量管理等场景,尤其是运维小白特别友好。别担心,下面一步步拆解,从零到稳,100% 可执行。
1. PHP SSH2 扩展安装(Windows 服务器端必备)
Windows 服务器安装 PHP 后,ssh2 扩展默认是关闭状态,先确认一下。
- 检查 PHP 版本:在 phpinfo() 页面看扩展列表里有没有 ssh2。
- 下载对应扩展(最新版推荐 1.4+,支持 PHP 8.0–8.4):
去 https://pecl.php.net/package/ssh2/ 下载 php_ssh2.dll(选 x64 或 x86,TS/nts 根据你的 PHP 线程模式选)。
同时下载 libssh2.dll(从 https://libssh2.org/ 下载最新稳定版,选匹配架构)。 - 拷贝文件:
- php_ssh2.dll 放到你的 PHP 安装目录下的 ext 文件夹(比如 C:\php\ext)。
- libssh2.dll 放到 PHP 安装目录根目录(C:\php\libssh2.dll),或者直接扔到 Windows\System32(32位)/ SysWOW64(64位)。
- 编辑 php.ini:找到 extension_dir = ... 行,在下面添加一行
extension=php_ssh2.dll(如果已经有 ;extension=... 把分号去掉)
- 重启服务:Apache / IIS / WAMP / XAMPP 重启,让扩展生效。
- 验证:访问 phpinfo(),搜索 “ssh2” 就能看到 Enabled。
搞定!新手小白看到 phpinfo 里 ssh2 出现就开心了,后面再来试代码。
2. 客户端调用:简单密码认证(最常用,超快)
PHP 代码直接连服务器,执行命令或读文件,秒级搞定。
<?php
// 连接服务器(主机名/域名/IP)
$connection = ssh2_connect('192.168.1.100', 22); // 22 是默认 SSH 端口
// 密码认证(推荐先用这个测试)
if (ssh2_auth_password($connection, 'admin', '你的密码')) {
echo "连接成功!\n";
// 执行命令,返回结果
$stream = ssh2_exec($connection, 'ipconfig');
stream_set_blocking($stream, true);
echo stream_get_contents($stream);
} else {
echo "认证失败,检查密码或防火墙!";
}
?>
- 把 IP、用户名、密码换成你的 Windows 服务器信息。
- Windows 服务器上必须先启动 OpenSSH 服务(搜索“启用或关闭 Windows 功能”勾选“OpenSSH 服务器”),默认端口 22,开机自启。
- 常见错误:连接超时?防火墙拦截(允许 Inbound Rule - SSH)?端口改成其他号?用 telnet IP 22 测试一下。
3. 秘钥认证(安全首选,运维大佬最爱)
密码容易被爆破,改成公钥认证,安全 10 倍。
服务器端(Windows OpenSSH):
- 运行以下命令创建密钥(管理员 PowerShell):
New-Item -Path C:\Users\admin\.ssh -ItemType Directory -Force ssh-keygen -t rsa -b 4096 -f C:\Users\admin\.ssh\id_rsa -N "" - 生成后,公钥内容复制到客户端(比如本地记事本)。
客户端 PHP 代码(秘钥登录):
$connection = ssh2_connect('192.168.1.100', 22, array('hostkey' => 'ssh-rsa'));
// 秘钥文件路径(Windows 用 \\ 或 / 都行)
$pubKey = file_get_contents('C:\\Users\\admin\\.ssh\\id_rsa.pub');
$privKey = file_get_contents('C:\\Users\\admin\\.ssh\\id_rsa');
if (ssh2_auth_pubkey_file($connection, 'admin', $pubKey, $privKey)) {
// 成功!
$stream = ssh2_exec($connection, 'whoami');
echo stream_get_contents($stream);
}
- Windows 服务器秘钥放 C:\Users\你的用户名.ssh(必须给该用户权限)。
- 客户端的 PHP 文件也要有读取秘钥的权限。
4. 文件上传下载(SFTP 超实用)
连上服务器直接传文件,省掉 FTP 麻烦。
$sftp = ssh2_sftp($connection); // sftp 资源
// 下载文件到本地
$file = fopen("C:\\local\\test.txt", "w");
$remote = fopen("ssh2.sftp://$connection/home/test.txt", "r");
stream_copy_to_stream($remote, $file);
fclose($remote);
fclose($file);
// 上传文件到服务器
$local = fopen("C:\\local\\script.bat", "r");
$remote = fopen("ssh2.sftp://$connection/home/upload/script.bat", "w");
stream_copy_to_stream($local, $remote);
fclose($local);
fclose($remote);
- 下载后记得 chmod 777 改权限(Windows 文件系统没这个,但文件夹通常没问题)。
5. 常见问题 & 避坑技巧(我踩过的坑)
- 连接拒绝或超时:服务器没开 SSH?防火墙?密码不对?试用 telnet IP 22,看看是否能连上。
- 认证失败:密码有空格?秘钥权限不对?Windows OpenSSH 只支持 password + publickey。
- 执行 Windows 命令失败:远程主机是 Windows,命令只能用 cmd 里的(如 dir、ipconfig),别写 /linux/ 路径。
- 性能慢:大文件传输用 sftp 就行,别用 shell exec。
- 安全加固:秘钥配 passphrase,Windows 服务器用强密码 + 不允许 root 登录,定期改秘钥。
- PHP 8+ 新版:如果老扩展报错,考虑用 phpseclib(纯 PHP 版,无需 DLL,更稳定)。
把这些代码复制到你的 PHP 项目里,测试一下就行。我一般先用密码连通,再换秘钥长期用。遇到具体错误(比如“unable to exchange keys”或“no matching host key”),把报错信息告诉我,我再给你针对性命令或配置。
这样一套方案落地后,你的 PHP 脚本远程操作 Windows 服务器就顺风顺水了,省时省力。有什么其他 Windows + PHP 问题随时说!
文章版权声明:文章内容均来源于各大短视频平台搜集以及修改和删减新增,如有侵权或者违规,请联系站长进行删除,如需转载或复制请以超链接形式并注明出处。

还没有评论,来说两句吧...