Codex + SSH + WP-CLI
配置完成以后,Codex 就可以通过 SSH 连接远程服务器,再利用 WP-CLI 查看插件、检查主题、分析页面,甚至在获得允许后直接修改 WordPress 网站。
一、整个工作原理
配置完成以后,实际的连接关系是这样的:
- 在电脑上执行 SSH 命令,连接到 SiteGround,然后使用服务器上的 WP-CLI 操作 WordPress。
- 这种方式最大的优点就是:既能管理 WordPress,又能管理网站代码。
二、第一步:在 Windows 创建 SSH 密钥
打开 Windows PowerShell(普通 PowerShell 就可以,不需要管理员权限),执行下面命令:
New-Item -ItemType Directory -Force "$env:USERPROFILE\.ssh" | Out-Null
$wpKey = "$env:USERPROFILE\.ssh\siteground_codex"
if (!(Test-Path $wpKey)) {
ssh-keygen -t ed25519 -f $wpKey -C "codex-siteground"
}
Get-Content "$wpKey.pub"
执行以后会生成两份文件:
siteground_codex 私钥
siteground_codex.pub 公钥
最后 PowerShell 会显示一行类似:
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAA...
把这一整行复制下来。
三、第二步:把 SSH 公钥添加到 SiteGround
进入 SiteGround 后台:
Websites
→ Site Tools
→ Devs
→ SSH Keys Manager
选择:Import → Key Name 可以填写:codex-siteground → Public Key 填刚才复制的公钥
- 这里会显示三个非常重要的信息:
Hostname
Username
Port
四、第三步:给 SSH 创建一个简单别名
在 PowerShell 运行:
notepad "$env:USERPROFILE\.ssh\config"
如果文件不存在,就创建。
然后把上面获得的三个重要信息输入进来(红色部分):
Host sg-wordpress
HostName ssh.你的SiteGround服务器.com
User 你的SiteGround-SSH用户名
Port 18765
IdentityFile ~/.ssh/siteground_codex
IdentitiesOnly yes
例如:
Host sg-wordpress
HostName ssh.example.com
User u4790-xxxxxxxxxxxx
Port 18765
IdentityFile ~/.ssh/siteground_codex
IdentitiesOnly yes
注意
文件必须叫:config,它是没有后缀的,一定要注意
五、第四步:把 SSH 私钥加入 Windows
首先以管理员身份运行 PowerShell:
Set-Service -Name ssh-agent -StartupType Automatic
Start-Service ssh-agent
然后重新打开普通 PowerShell,运行:
ssh-add "$env:USERPROFILE\.ssh\siteground_codex"
如果成功,会看到:
Identity added
例如:
Identity added:
C:\Users\slobys\.ssh\siteground_codex
这就说明 SSH 私钥已经成功加载。
六、第五步:检查 SSH 配置
执行:
ssh -G sg-wordpress | Select-String '^(hostname|user|port|identityfile)\s'
如果配置正确,会看到类似:
user u4790-xxxxxxxx
hostname ssh.xxxxx.com
port 18765
identityfile ~/.ssh/siteground_codex
七、第六步:第一次连接 SiteGround
现在执行:
ssh sg-wordpress
第一次连接时,SSH 会提示:
The authenticity of host ... can't be established.
Are you sure you want to continue connecting
(yes/no/[fingerprint])?
确认服务器地址没有问题后输入:
yes
第一次确认以后,这台服务器会被加入:
known_hosts
以后就不会再次询问。
成功以后,会看到类似:
baseos | example.com |
u4790-xxxx@sgp1003.siteground.asia:~$
看到这个界面,就说明:
Windows 已经成功通过 SSH 登录 SiteGround。
八、第七步:检查 WP-CLI
连接服务器以后运行:
wp --info
我的服务器返回:
PHP version: 8.3.35
WP-CLI version: 2.12.0
只要能够正常显示 WP-CLI 信息,就说明服务器已经具备远程管理 WordPress 的条件。
九、第八步:找到 WordPress 安装目录
查看当前所在的文件夹路径:
pwd
查看当前文件夹里有哪些文件和文件夹:
ls
SiteGround 网站一般位于:
/home/customer/www/
可以直接搜索:
find /home/customer/www -maxdepth 3 -name wp-config.php 2>/dev/null
我的网站返回:
/home/customer/www/example.com/public_html/wp-config.php
因此我的 WordPress 根目录就是:
/home/customer/www/example.com/public_html
进入:
cd /home/customer/www/example.com/public_html
十、第九步:测试 WordPress
先查看 WordPress 版本:
wp core version
我的网站返回:
7.1.2
然后查看网站地址:
wp option get home
返回:
https://example.com
到了这一步,说明整个连接链路已经成功。
十一、给 Codex 建立专用 WordPress 项目
创建一个网站日常维护目录:
C:\Users\用户名\Documents\ChatGPT\blog-wordpress
下载并安装项目包:【点击进入】
- 下载之后,解压放到blog-wordpress文件夹下即可
项目包的作用:让 Codex 按预设的安全规则,通过 SSH + WP-CLI 更方便、更安全地管理你的 WordPress 网站。
AGENTS.md:Codex 最重要的操作规则,默认只读、修改前备份、禁止泄露敏感信息。README.md:使用说明。COMMANDS.md:常用 WP-CLI 命令。scripts\Test-WordPressConnection.ps1:一键检查 SSH + WP-CLI + WordPress。scripts\Invoke-WP.ps1:安全调用 WP-CLI,默认只允许只读命令。scripts\Backup-DB.ps1:修改数据库前一键备份。.gitignore:避免把数据库、密钥等误提交。
十三、给 Codex 的第一条命令
打开这个项目以后,可以直接告诉 Codex:
按 AGENTS.md 的规则连接 example.com。
先做只读检查:
确认 WordPress 版本,
检查当前主题,
检查所有插件状态,
检查网站 URL。
不要修改任何东西。
最后给我一份简短的网站状态报告。










