Tags

, , , , , , , ,

<?php
     $r = shell_exec ("powershell -File $tools\create-aduser.ps1 -InputFormat none > NULL");
?>

See something interesting above? That *.ps1 part. Yeah, that’s why I love PHP! PHP is advanced, full of features, capable of integrating with different systems, and open source. That’s why I “like” it but the reason for me to “love” it is it’s ability to execute PowerShell scripts. Imagine a set of complex PowerShell scripts being backed by a powerful yet simple PHP powered web portal. Then imagine people with no deep technical knowledge performing technically complex tasks from a web page in a controlled manner without any knowledge of PowerShell. How cool is that!? If you’re like me then you might be dreaming of all the new web pages that are going to make your life easier right now.

PHP can be easily configured in Unix environment. I have run PHP on Apache on a Ubuntu box and it was very easy to configure it initially. However since I’m focusing on PowerShell and administering Windows based systems Microsoft IIS Server seems to be the ideal web server but it’s a pain to configure it initially and get everything started. Once you configure everything so that you can view the output of a *.php web page with no errors you are good to go and play PHP however you want.

The difficulty in configuration exists in the correct registration of PHP in IIS. For Windows Server 2008 R2 you can use the tool PHP Manager for IIS that is designed for IIS 7 and IIS 7.5 at the time of this writing for the initial configuration. This tool is a life saver! Once you provide the path of the PHP executable to it and select “Register new PHP version” it goes ahead and takes care of all the necessary registrations. It’s 2015 and 2008 R2 is way old! We need to play with the new stuff. So when you start doing the configuration in Windows Server 2012 R2, well the PHP Manager for IIS is not supported and you have to look for a different option. I have been pulling my hair while reading the enormous number of forum posts for quite some time about the issues that we have to face when we try to configure everything ourselves. Something always falls through the cracks and it never works! But last week I think I cracked it and I know the exact recipe that it needs to work in a single streak.

…and everything starts at Build a PHP Website on IIS on TechNet! This TechNet article is a great guide with detailed steps. Therefore I’m not going to repeat the things in here. Instead I will fill in the gaps so you have everything to get this working.

Microsoft Web Platform Installer – The new web guy in town!

Microsoft Web PI

Similar to PHP manager for IIS the Web PI will take care of the necessary PHP registrations in IIS and some of its dependencies such as WinCache extension when you install PHP through it. As I usually say, if you want to burn a few more calories, you can do it manually as instructed in the document.

THE HTTP Error 500.0 – Internal Server Error

Once the download is done and you go for the first test to see if PHP is working, if you are lucky and privileged like the most of us IT folks you’ll be presented with a BIG RED HTTP Error 500 web page! 🙂 This is where we start to pull our hair and start messing with everything including NTFS permissions.

…but the first, the very FIRST thing to check is the Visual C++ packages that are installed in the Windows server. Let’s say this in quotes to emphasize it!

For PHP to work on a Microsoft IIS Server each PHP version needs its matching Visual C++ package installed!

So there’s that! Since there are many PHP versions and many Visual C++ packages available I don’t know all the matching pairs at the moment but I’ll try to update this post with them when I come across them. The latest PHP version that is available through the Web PI, at this writing is version 5.6. The matching Visual C++ package for PHP v5.6 is Visual C++ Redistributable for Visual Studio 2012 Update 4. Once this package is installed everything should be working fine.

THE 500.0 persists even after the matching Visual C++ package is installed

If the HTTP Error 500 is still there then it needs further troubleshooting. You can start by checking the IIS server-level FastCGI settings and the Handler Mappings, and continue troubleshooting along the steps mentioned in the HTTP Error 500.0 web page.

IIS server-level FastCGI settings and Handler Mappings configuration of a working PHP host

FastCGI Settings

  • Full path : Path to php-cgi.exe (e.g. C:\Program Files(x86)\PHP\v5.6\php-cgi.exe)
  • Arguments : default
  • Max. Instances : default
  • Instance Max. Requests : default

Handler Mappings

  • Path : *.php
  • State : Enabled
  • Path Type : File or Folder
  • Handler : FastCgiModule
  • Entry Type : Local

Let’s make those wonderful PowerShell powered web sites!


Update: 2016-02-17

The new PHP 7.0.0 package in the Web Platform Installer 5.0 seems to resolve these dependencies automatically. Yesterday I installed PHP 7.0.0 (x64) on a Windows Server 2012 R2 machine from Web PI and I was surprised to see PHP working right away after the installation. When I checked the Programs and Features I could see the matching Visual C++ packages installed automatically.

webpi-php7

PHP package in Web PI 5.0

visualcpp-for-php7

Automatically installed Microsoft Visual C++ package


Sources: