dongren9966
2011-08-25 18:03
浏览 83
已采纳

使用powershell为wampserver创建虚拟主机

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.

图片转代码服务由CSDN问答提供 功能建议

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

这是我的脚本

  $ 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   
 
 

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

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

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanfang7757 2011-09-03 19:47
    已采纳

    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
    ";
    }
    
    点赞 打赏 评论
  • doujunchi1238 2014-05-30 04:54

    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.

    点赞 打赏 评论

相关推荐 更多相似问题