使用powershell为wampserver创建虚拟主机

我正在尝试使用Powershell脚本创建虚拟主机。 它工作正常,但我遇到编码问题。 我已经尝试了所有允许的 -encoding </ code>开关值。</ p>

这是我的脚本</ p>

  $ hostName = $ args [0]; 
if($ args -eq $ null -or $ args.Length -eq 0)
{
”无参数。提供虚拟 主机名作为第一个参数。“
exit
}
if(!(Test-Path -path D:\ wamp \ $ hostName))
{
$ indexText =”&lt;?php echo“”&lt; ; h1&gt;持有人$ hostName&lt; / h1&gt;“;”
新项目D:\ wamp \ $ hostName -type目录
新项目D:\ wamp \ $ hostName \ index.php -type文件\ n 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 +”&lt; VirtualHost *:80&gt;“
$ 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 +“&lt; / VirtualHost&gt;”
Out-File -FilePath “C:\ Program Files(x86)\ wamp \ bin \ apache \ Apache2.2.11 \ conf \ extra \ httpd-vhosts.conf”-InputObject $ conf -Encoding ascii
Write-Host“重启wamp服务器以实现更改” n </ code> </ pre>

是否有人设法让这个工作可靠地工作,还是我在这里没有首发? 我总是可以使用PHP代替:)</ p>

如果编码不正确,那么Apache拒绝在wampserver下启动。 如果我将notepad ++中的编码更改为ANSI,那么一切正常,但powershell不喜欢 -encoding ANSI </ code>。</ p>
</ div>

展开原文

原文

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.

Here is my script

$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"

Has anybody managed to get this working reliably or am I on to a none-starter here? I can always use PHP instead :)

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.

dqpkea9486
dqpkea9486 我尝试过UTF8,但Apache不喜欢文件中留下的BOM。
接近 9 年之前 回复
drryyiuib43562604
drryyiuib43562604 如果你使用unicode编码,apache会接受吗?
接近 9 年之前 回复

2个回答



使用 Out-File </ code>上的 -Encoding OEM </ code>选项似乎是一个 这个问题的解决办法,任何其他人都反对它。 </ p>

例如: - </ p>

  Out-File -FilePath“C:\ Program Files(x86)\ wamp \ bin \ apache \  Apache2.2.11 \ conf \ extra \ httpd-vhosts.conf“-InputObject $ conf -Encoding OEM 
</ code> </ pre>

编辑: - </ strong>

我最终通过在PHP中重写脚本解决了这个问题,因为我永远无法让它在Powershell中始终如一地工作。 这是我的脚本,其他人发现它很有用,它被设计为从命令行运行,例如: - </ p>

  php d:/scripts/newVHost.php www 
</ code> </ pre>

将创建一个新的虚拟主机调用'www'。</ p>

 &lt;?php 
/ **
  • 在此服务器上创建一个新的vhost
  • @param主机名
  • /
    $ ds = DIRECTORY_SEPARATOR;
    $ eol = PHP_EOL;

    //如果您的主机可能需要更改此名称 文件在别处
    $ hostsFile =“C:\ Windows \ System32 \ drivers \ etc \ hosts”;
    $ hostFH = fopen($ hostsFile,'a +');

    //你可能需要改变它 如果你的htttp-vhosts.conf文件在别处
    $ apacheVHfile =“C:\ Program Files \ wamp \ bin \ apache \ Apache2.2.17 \ conf \ extra \ _ httpd-vhosts.conf”;
    $ apacheFH = fopen( $ apacheVHfile,'a +');

if($ argc&lt; = 1)die(“你必须指定一个主机名
”);
$ hostName = $ argv [1];
$ pathName = “d:{$ ds} www {$ ds} $ hostName”;
echo“
创建新的虚拟主机'$ hostName'
”;

if(!file_exists($ pathName))mkdir($ pathName);

$ writeStr =“
127.0.0.1 $ hostName.local host“;

writeFile($ hostFH,$ writeStr,$ hostsFile);

$ writeStr =”&lt; VirtualHost *:80&gt;
DocumentRoot \“$ pathName \”
ServerName $ hostName.localhost
ErrorLog \“logs {$ ds} $ hostName.localhost-error.log \”
CustomLog \“logs {$ ds} $ hostName.localhost-access.log \”common
&lt; / VirtualHost&gt; $ eol“;

<nwriteFile($ apacheFH,$ writeStr,$ apacheVHfile);

nclclose($ hostFH);
nclclose($ apacheFH);

echo“已创建新虚拟主机$ hostName。 重启WAMP服务器
“;
echo”
===================================

“;

函数writeFile($ file,$ writeStr,$ fileName = null){
$ written = fwrite($ file,$ writeStr);
if($ written){
echo”
Modified $ fileName
“;
}否则回显”
无法修改$ fileName
“;
}
</ code> </ pre>
</ div>

展开原文

原文

using the -Encoding OEM option on Out-File seems to be a solution to this problem incase anybody else comes up against it.

For example:-

Out-File -FilePath "C:\Program Files (x86)\wamp\bin\apache\Apache2.2.11\conf\extra\httpd-vhosts.conf" -InputObject $conf -Encoding OEM

Edit:-
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

Will create a new virtual host call 'www'.

<?php
/**
 * create a new vhost on this server
 * 
 * @param host name
 */
$ds = DIRECTORY_SEPARATOR;
$eol = PHP_EOL;

// 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
");
$hostName = $argv[1];
$pathName = "d:{$ds}www{$ds}$hostName";
echo "
Creating new virtual host '$hostName'
";

if(!file_exists($pathName)) mkdir($pathName);

$writeStr = "
127.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
";
    echo "
===================================

";

function writeFile($file, $writeStr, $fileName = null){
    $written = fwrite($file, $writeStr);
    if($written){
        echo "
Modified $fileName
";
    } else echo "
Could not Modify $fileName
";
}

doutu1939
doutu1939 令人难以置信,你得到了很大的帮助! 干杯!
接近 8 年之前 回复



我想你忘了关闭你的php标签:</ p>

  $ indexText =  “&lt;?php echo”“&lt; h1&gt;持有人$ hostName&lt; / h1&gt;`”; **?&gt; **“
</ code> </ pre>

更新: 我重写了你的脚本,我得到了一致的结果如下:</ p>

  function vhost()
{
$ hostName = $ args [0];
if( $ args -eq $ null -or $ args.Length -eq 0)
{
“无参数。提供虚拟主机名作为第一个参数。”
}
if(!($ args -eq $) null -or $ args.Length -eq 0))
{
if(!(Test-Path -path C:\ wamp \ www \ $ hostName))
{
$ indexText =“&lt;?php echo“”&lt; h1&gt;成功! 已创建$ hostName的虚拟主机。&lt; / h1&gt;“;?&gt;”
新项目C:\ wamp \ www \ $ hostName -type目录
新项目C:\ wamp \ www \ $ hostName \ index.php -type file
Out-File -FilePath C:\ wamp \ www \ $ hostName \ index.php -InputObject $ indexText -Encoding UTF8
}
$ hosts = Get-Content C:\ Windows \ System32 \ drivers \ etc \ hosts
$ hosts = $ hosts +“127.0.0.1
t $ hostName”
Out-File -FilePath C:\ Windows \ System32 \ drivers \ etc \ hosts -InputObject $ hosts
$ conf = Get-Content“C:\ wamp \ bin \ apache \ apache2.2.22 \ conf \ extra \ httpd-vhosts.conf”
$ conf = $ conf +“&lt; VirtualHost *:80&gt;”
$ conf = $ conf +“DocumentRoot”c:/ wamp / www / $ hostName“”
$ conf = $ conf +“ServerName $ hostName”
$ conf = $ conf +“ServerAlias $ hostName”
$ conf = $ conf +“ErrorLog”logs / $ hostName-error.log“”
$ conf = $ conf +“CustomLog”logs / $ hostName-access.log“common”
$ conf = $ conf +“&lt; / VirtualHost&gt; “
Out-File -FilePath”C:\ wamp \ bin \ apache \ apache2.2.22 \ conf \ extra \ httpd-vhosts.conf“-InputObject $ conf -Encoding ascii
Write-Host”Virtual host $ hostName has 已创建“
写入主机”重新启动wamp服务器以实现更改“
}
}
</ code> </ pre>

请注意 - 更改相对于计算机的路径。 </ p>
</ div>

展开原文

原文

I think you forgot to close your php tag in:

$indexText = "<?php echo `"<h1>Holder for $hostName</h1>`"; **?>**"

Update: I rewrote your script and I am getting consistent results with the following:

function vhost() 
{
    $hostName = $args[0];
    if ($args -eq $null -or $args.Length -eq 0)
    {
    "No arguments. Supply the virtual host name as the first argument."
    }
    if (!($args -eq $null -or $args.Length -eq 0))
    {
        if (!(Test-Path -path C:\wamp\www\$hostName))
        {
        $indexText = "<?php echo `"<h1>Success! Virtual host for $hostName created.</h1>`"; ?>"
        New-Item C:\wamp\www\$hostName -type directory
        New-Item C:\wamp\www\$hostName\index.php -type file
        Out-File -FilePath C:\wamp\www\$hostName\index.php -InputObject $indexText -Encoding UTF8
        }
        $hosts = Get-Content C:\Windows\System32\drivers\etc\hosts
        $hosts = $hosts + "127.0.0.1`t$hostName"
        Out-File -FilePath C:\Windows\System32\drivers\etc\hosts -InputObject $hosts
        $conf = Get-Content "C:\wamp\bin\apache\apache2.2.22\conf\extra\httpd-vhosts.conf"
        $conf = $conf + "<VirtualHost *:80>"
        $conf = $conf + "    DocumentRoot `"c:/wamp/www/$hostName`""
        $conf = $conf + "    ServerName $hostName"
        $conf = $conf + "    ServerAlias $hostName"
        $conf = $conf + "    ErrorLog `"logs/$hostName-error.log`""
        $conf = $conf + "    CustomLog `"logs/$hostName-access.log`" common"
        $conf = $conf + "</VirtualHost>"
        Out-File -FilePath "C:\wamp\bin\apache\apache2.2.22\conf\extra\httpd-vhosts.conf" -InputObject $conf -Encoding ascii
        Write-Host "Virtual host $hostName has been created"
        Write-Host "Restart wamp server to effect changes"
    }
}

Please note - Change the paths relative to your machine.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐