快速更改WampServer根目录php脚本
快速更改WampServer根目录php脚本
<?php
// 配置文件地址
$apacheConfPath = 'C:\Install\CTF\Wampserver\bin\apache\apache2.4.62.1\conf\httpd.conf';
$apacheConfPath2 = 'C:\Install\CTF\Wampserver\bin\apache\apache2.4.62.1\conf\extra\httpd-vhosts.conf';
// 新根目录地址
$newDocumentRoot = 'G:\Notes_and_Dictionary\book\CTF-codes\php-code-all-in-there\run_www';
// step 1
function change_set($apacheConfPath, $newDocumentRoot)
{
$config = file_get_contents($apacheConfPath);
if ($config === false) {
die("无法读取配置文件: $apacheConfPath\n");
}
// 修改 DocumentRoot
$patternDocRoot = '/^\s*DocumentRoot\s+"[^"]+"/m';
$replacementDocRoot = 'DocumentRoot "' . $newDocumentRoot . '"';
$config = preg_replace($patternDocRoot, $replacementDocRoot, $config, 1, $countDocRoot);
if ($countDocRoot === 0) {
die("未找到 DocumentRoot 指令。\n");
}
// 更改 <Directory
$patternDocRoot = '/<Directory\s+"[^"]+">/m';
$replacementDocRoot = '<Directory "' . $newDocumentRoot . '">';
$config = preg_replace($patternDocRoot, $replacementDocRoot, $config, 1, $countDocRoot);
if ($countDocRoot === 0) {
die("未找到 <Directory 。\n");
}
// 保存修改后的配置文件
if (file_put_contents($apacheConfPath, $config) === false) {
die("无法写入修改后的配置文件: $apacheConfPath\n");
}
}
// step 2
change_set($apacheConfPath, $newDocumentRoot);
change_set($apacheConfPath2, $newDocumentRoot);
echo "更改成功,请重启服务";