windows環境にてphpアプリDirectory下にphp、httpdを自動設定するPowershellスクリプト

# **目的**
httpd、php、fuelphp の開発に参加することになった。Local環境はWindowsなので、Globalで設置することよりphpアプリ毎に各Middlewareをインストールする方法を工夫してみました。
言語はPowershell。


```ps1

Add-Type -AssemblyName System.IO.Compression.FileSystem
function Unzip
{
    param([string]$zipfile, [string]$outpath)
    [System.IO.Compression.ZipFile]::ExtractToDirectory($zipfile, $outpath)
}

$php_binary = @{
  "php-7.0.0-Win32-VC14-x64.zip"  = "http://windows.php.net/downloads/releases/php-7.0.0-Win32-VC14-x64.zip"; 
  "php-5.6.16-Win32-VC11-x64.zip"  = "http://windows.php.net/downloads/releases/php-5.6.16-Win32-VC11-x64.zip";
  "php-5.5.30-Win32-VC11-x64.zip"  = "http://windows.php.net/downloads/releases/php-5.5.30-Win32-VC11-x64.zip"; 
}

$fuelphp_binary = @{
  "fuelphp-1.7.3.zip" = "http://fuelphp.com/files/download/34/fuelphp-1.7.3.zip";
  "fuelphp-1.7.2.zip" = "http://fuelphp.com/files/download/28/fuelphp-1.7.2.zip";
  "fuelphp-1.7.1.zip" = "http://fuelphp.com/files/download/26/fuelphp-1.7.1.zip";
  "fuelphp-1.7.zip" = "http://fuelphp.com/files/download/25/fuelphp-1.7.zip";
  "fuelphp-1.6.1.zip" = "http://fuelphp.com/files/download/24/fuelphp-1.6.1.zip";
}

$httpd_binary = @{
  "httpd-2.4.18-win64-VC14.zip" = "https://www.apachelounge.com/download/VC14/binaries/httpd-2.4.18-win64-VC14.zip"
}

#####################################################
# EDIT HERE TO CHANGE INSTALLATION VERSION
#####################################################
$target_version_php = "php-5.5.30-Win32-VC11-x64.zip"
$target_version_fuelphp = "fuelphp-1.7.3.zip"
$target_version_httpd = "httpd-2.4.18-win64-VC14.zip"

$install_path = "D:\dev\myphpapp1"
$install_path_php = "$install_path\bin\php"
$install_path_httpd = "$install_path\bin\Apache24"
$install_path_documentroot = "$install_path\app"

#####################################################
# Download binaries and extract them
#####################################################

#==========
# PHP 
#==========
mkdir $install_path_php | out-null
cd "$install_path\bin\"
$selected_binary_url_php = $php_binary[$target_version_php]

Invoke-WebRequest $selected_binary_url_php -OutFile $target_version_php
Unzip $($(pwd).Path + "\" + $target_version_php) "$(pwd)\php"

d:\opencloud\zcom\api\bin\php\php.ini-production

#==========
# Edit php.ini
#==========
copy "$install_path_php\php.ini-production" "$install_path_php\php.ini"
$ini = get-content "$install_path_php\php.ini"
$ini = $ini.Replace('; extension_dir = "ext"', 'extension_dir = "ext"')
$ini = $ini.Replace(';date.timezone =', 'date.timezone = "UTC"')


$ini = $ini.Replace(";extension=php_mbstring.dll", "extension=php_mbstring.dll")
Set-content "$install_path_php\php.ini" $ini

#==========
# Httpd
#==========
mkdir $install_path_httpd | out-null
cd "$install_path\bin\"
$selected_binary_url_httpd = $httpd_binary[$target_version_httpd]

Invoke-WebRequest $selected_binary_url_httpd -OutFile $target_version_httpd
Unzip $($(pwd).Path + "\" + $target_version_httpd) $(pwd)

#==========
# Edit httpd.conf
#==========
copy "$install_path_php\php5apache2_4.dll" "$install_path_httpd\modules\"

$conf = get-content "$install_path_httpd\conf\httpd.conf"

# change default setting
$conf = $conf.Replace("admin@example.com", "address@email.com")
$conf = $conf.Replace( "c:/Apache24/htdocs", $install_path_documentroot.Replace('\', '/') )
$conf = $conf.Replace( "c:/Apache24/cgi-bin", "$install_path_httpd\cgi-bin".Replace('\', '/') )
$conf = $conf.Replace( "c:/Apache24", $install_path_httpd.Replace('\', '/') )
$conf = $conf + @"

# 
LoadModule php5_module "$( ("$install_path_php\php5apache2_4.dll").Replace('\', '/') )"
AddHandler application/x-httpd-php .php

# configure the path to php.ini
PHPIniDir "$( ($install_path_php).Replace('\', '/') )"
"@

Set-content "$install_path_httpd\conf\httpd.conf" $conf

#==========
# FuelPHP 
#==========
mkdir $install_path_documentroot
cd $install_path_httpd
$selected_binary_url_fuelphp = $fuelphp_binary[$target_version_fuelphp]

Invoke-WebRequest $selected_binary_url_fuelphp -OutFile $target_version_fuelphp
Unzip $($(pwd).Path + "\" + $target_version_fuelphp) $install_path_documentroot

# cleaning
rm *.zip
```