创建虚拟主机的使用PowerShell wampserver [英] Creating virtual hosts for wampserver with powershell
问题描述
我试图用PowerShell脚本创建虚拟主机。它工作正常,但我有编码的麻烦。我试图让没有运气的所有 -encoding
开关值。
I am trying to create virtual hosts with a Powershell script. It works fine, but I am having trouble with the encoding. I have tried all the -encoding
switch values allowed with no luck.
下面是我的脚本
$hostName = $args[0];
if ($args -eq $null -or $args.Length -eq 0)
{
"No arguments. Supply the virtual host name as the first argument."
exit
}
if (!(Test-Path -path D:\wamp\$hostName))
{
$indexText = "<?php echo `"<h1>Holder for $hostName</h1>`";"
New-Item D:\wamp\$hostName -type directory
New-Item D:\wamp\$hostName\index.php -type file
Out-File -FilePath D:\wamp\$hostName\index.php -InputObject $indexText -Encoding UTF8
}
$hosts = Get-Content C:\Windows\System32\drivers\etc\hosts
$hosts = $hosts + "127.0.0.1 $hostName.localhost"
Out-File -FilePath C:\Windows\System32\drivers\etc\hosts -InputObject $hosts
$conf = Get-Content "C:\Program Files (x86)\wamp\bin\apache\Apache2.2.11\conf\extra\httpd-vhosts.conf"
$conf = $conf + "<VirtualHost *:80>"
$conf = $conf + " DocumentRoot `"D:/wamp/$hostName`""
$conf = $conf + " ServerName $hostName.localhost"
$conf = $conf + " ErrorLog `"logs/$hostName.localhost-error.log`""
$conf = $conf + " CustomLog `"logs/$hostName.localhost-access.log`" common"
$conf = $conf + "</VirtualHost>"
Out-File -FilePath "C:\Program Files (x86)\wamp\bin\apache\Apache2.2.11\conf\extra\httpd-vhosts.conf" -InputObject $conf -Encoding ascii
Write-Host "Restart wamp server to effect changes"
有没有人设法获得这一工作可靠还是我到一个没有起动器在这里?我总是可以使用PHP来代替:)
Has anybody managed to get this working reliably or am I on to a none-starter here? I can always use PHP instead :)
如果编码不正确,那么阿帕奇拒绝根据wampserver启动。如果我在记事本++更改编码为ANSI然后一切工作正常,但PowerShell不会像 -encoding ANSI
。
If the encoding is not correct then Apache refuses to start under wampserver. If I change the encoding in notepad++ to ANSI then everything works fine, but powershell doesn't like -encoding ANSI
.
推荐答案
在使用的
似乎是一个解决这个问题柜面任何人碰到它。 -Encoding OEM
选项出文件
using the -Encoding OEM
option on Out-File
seems to be a solution to this problem incase anybody else comes up against it.
例如: -
Out-File -FilePath "C:\Program Files (x86)\wamp\bin\apache\Apache2.2.11\conf\extra\httpd-vhosts.conf" -InputObject $conf -Encoding OEM
编辑: - 结果
我最终在PHP重写剧本,因为我永远无法得到它在PowerShell中工作始终解决了这个问题。这里是我的脚本柜面其他人觉得有用,它的目的是通过命令行运行例如: -
-
I eventually solved this problem by rewriting the script in PHP as I could never get it to work consistently in Powershell. Here is my script incase others find it useful, it is designed to run from the command line eg:-
php d:/scripts/newVHost.php www
将创建一个新的虚拟主机呼叫WWW。
Will create a new virtual host call 'www'.
<?php
/**
* create a new vhost on this server
*
* @param host name
*/
$ds = DIRECTORY_SEPARATOR;
//for windows
$eol = chr(10) . chr(13);
//for other OS's
//$eol = "\n";
// you may need to alter this if your host file is elsewhere
$hostsFile = "C:\Windows\System32\drivers\etc\hosts";
$hostFH = fopen($hostsFile, 'a+');
// you may need to alter this if your htttp-vhosts.conf file is elsewhere
$apacheVHfile = "C:\Program Files\wamp\bin\apache\Apache2.2.17\conf\extra\httpd-vhosts.conf";
$apacheFH = fopen($apacheVHfile, 'a+');
if($argc <= 1) die("You must specify a host name\n");
$hostName = $argv[1];
$pathName = "d:{$ds}www{$ds}$hostName";
echo "\nCreating new virtual host '$hostName'\n";
if(!file_exists($pathName)) mkdir($pathName);
$writeStr = "\n127.0.0.1 $hostName.localhost";
writeFile($hostFH, $writeStr, $hostsFile);
$writeStr = "<VirtualHost *:80>
DocumentRoot \"$pathName\"
ServerName $hostName.localhost
ErrorLog \"logs{$ds}$hostName.localhost-error.log\"
CustomLog \"logs{$ds}$hostName.localhost-access.log\" common
</VirtualHost>$eol";
writeFile($apacheFH, $writeStr, $apacheVHfile);
fclose($hostFH);
fclose($apacheFH);
echo "New virtual host $hostName created. Restart WAMP server\n";
echo "\n===================================\n\n";
function writeFile($file, $writeStr, $fileName = null){
$written = fwrite($file, $writeStr);
if($written){
echo "\nModified $fileName\n";
} else echo "\nCould not Modify $fileName\n";
}
这篇关于创建虚拟主机的使用PowerShell wampserver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!