|
宝塔版本:宝塔 Linux 面板(Apache 环境) Web 服务:Apache 操作方式:通过宝塔 API 调用启用 / 停用站点
我在做一个通过宝塔 API 批量或者单个启用 / 停用站点的功能,遇到一个比较严重的问题:
通过 API 调用启用 / 停用站点后,只修改了数据库中的站点状态,但 Apache 的 vhost 配置文件并没有被真正重写,站点配置不会发生实际变化。
具体表现为:
```
// 启用/停用后,需要重写Apache配置文件并重载服务才能生效
// 注意:SiteStart/SiteStop只是更新了数据库状态,需要重写配置文件才能真正生效
// 根据宝塔工作原理,需要调用ReWeb接口重写配置并重载服务
$reloadSuccess = false;
// 使用站点名称调用ReWeb接口(这会重写配置并重载Apache)
$rewebUrl = $bturl.'/site?action=ReWeb';
$rewebData = $this->GetKeyData($keys);
$rewebData['name' = $mainDomain; // 使用站点域名,而不是Web服务器名称
$rewebResult = $this->HttpPostCookie($bturl, $rewebUrl, $rewebData);
if($rewebResult !== false){
$tmp = json_decode($rewebResult, true);
if(is_array($tmp) && (!empty($tmp['status']) || isset($tmp['msg']))){
$reloadSuccess = true;
}
}
// 如果ReWeb接口失败,尝试直接重启Apache服务
if(!$reloadSuccess){
$apacheUrl = $bturl.'/system?action=RestartWeb';
$apacheData = $this->GetKeyData($keys);
$apacheData['name' = 'apache';
$apacheResult = $this->HttpPostCookie($bturl, $apacheUrl, $apacheData);
if($apacheResult !== false){
$tmp = json_decode($apacheResult, true);
if(is_array($tmp) && !empty($tmp['status'])){
$reloadSuccess = true;
}
}
}
if(!$reloadSuccess){
$this->last_error['restart_warning' = '站点状态已更新,但配置重写/重载失败,可能需要手动重写配置并重启Apache';
}
```
|