LoginSignup
0
0

More than 5 years have passed since last update.

Azure VM上にIIS + PHP環境をPowerShellで構築する

Posted at

Azure Web Appだと簡単に作れるIIS + PHPの環境を、IaaSで実現しようとするとどれくらい手間かを把握したくて構築してみました。
なるべくCLIで実行したくて、IIS Managerの操作以外はPower Shellで構築しています。

環境

  • OS
    • Windows Server 2016
  • PHP
    • 2.7.9 NTS

VM作成

ここは公式手順でサクッと作ります。
Azure portal で Windows 仮想マシンを作成する

IIS+PHPセットアップ

  • PowerShellを起動します。
  • IISとCGIをインストールします。
Install-WindowsFeature -name Web-Server,Web-CGI –IncludeManagementTools
  • TLS1.2を有効化します。これをやっておかないとエラーメッセージ「Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a send」が出力して、このあとのファイルダウンロードに失敗します。
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12
  • PHPモジュールをダウンロードします。
$source = "https://windows.php.net/downloads/releases/php-7.2.9-nts-Win32-VC15-x64.zip"
$filepath = "C:\php-7.2.9-nts-Win32-VC15-x64.zip"
Invoke-WebRequest -Uri $source -OutFile $filepath
  • ダウンロードしたモジュールを解凍します。
Expand-Archive -Path $filepath -DestinationPath C:\php\
  • Visual C++ 再頒布可能パッケージをインストールします。
$source = "https://go.microsoft.com/fwlink/?LinkId=746572"
$filepath = "C:\vc_redist_2017.x64.exe"
Invoke-WebRequest -Uri $source -OutFile $filepath
Start-Process -FilePath $filepath -ArgumentList "/extract:x64 /q"

IIS Managerセットアップ

ほんとはここもPowerShellがよかったんですが、逆に大変だったので断念しました。。

  • 「ファイル名を指定して実行」より「inetmgr」と実行し、IIS Managerを開きます。
    image.png

  • 自身のサーバ名を選択後、「Handler Mappings」をダブルクリックします。
    image.png

  • 右側のActions画面より「Add Module Mapping...」を選択します。
    image.png

  • 以下のように入力します。

    • Request path
      • *.php
    • Module
      • FastCgiModule
    • Executable
      • C:\php\php-cgi.exe
    • Name
      • PHP-CGI
  • モジュールマップ追加ダイアログで「Yes」を選択します。
    image.png

  • サーバーに戻って、「Default Document」をダブルクリックします。
    image.png

  • 右側のActions画面より「Add...」を選択します。
    image.png

  • ダイアログに「index.php」と入力して「OK」を押します。
    image.png

動作確認

  • 現在時刻を表示してみましょう。テキストを開いて以下を記述します。
<?php
date_default_timezone_set('Asia/Tokyo');
echo(date(‘Y/m/d H:i:s’));
?>
  • 「index.php」のファイル名で「C:\inetpub\wwwroot」直下に保存します。
  • ブラウザを開いて現在時刻が表示されたらOKです。必要最小限の設定のみですが、一応IIS + PHPで動作する環境を作れました。

まとめ

やっぱりWeb App楽ですね。
ロードバランサーやスケールアウトの仕組みもデフォルトで付いてくるので、これをVirtual Machinesで実現しようとするとかなり手間です。(VMSSが必要になってきます)
参考:ExpressRoute環境でVMSSを作成する

要件次第ですが、極力PaaSを選択しようと改めて思いました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0