Skip to content

Instantly share code, notes, and snippets.

@lipkau
Last active March 9, 2017 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lipkau/105f07f8dacd3800dcd62d4dbad5539c to your computer and use it in GitHub Desktop.
Save lipkau/105f07f8dacd3800dcd62d4dbad5539c to your computer and use it in GitHub Desktop.
Create local http server using PHP.exe #powershell #helperFunction #workflow
<#PSScriptInfo
.VERSION 1.0
.GUID 9c74d64a-774d-4f5a-a8c4-485fd079e7bd
.AUTHOR
Oliver Lipkau <oliver@lipkau.net>
.COPYRIGHT
Oliver Lipkau
.TAGS
localhost, php, start-WebServer, host, server
.LICENSEURI
https://opensource.org/licenses/MIT
#>
<#
.DESCRIPTION
Start a local http server on a specific port.
#>
function Start-WebServer
{
<#
.SYNOPSIS
Start a local http server
.DESCRIPTION
Start a local http server on a specific port.
REQUIRES: php to be installed and available in $env:path
.NOTES
Author : Oliver Lipkau <oliver@lipkau.net>
Source : https://gist.github.com/lipkau/105f07f8dacd3800dcd62d4dbad5539c
.INPUTS
System.String
System.Integer
.EXAMPLE
Start-WebServer -Path "C:\www\myProject\public" -Port 8080
-----------
Description
Makes the specified Path availble at http://localhost:8080
#>
[CmdletBinding()]
param(
# Specifies the path which should be made available in http server.
[ValidateScript({(Test-Path $_ -IsValid)})]
[Parameter(Position = 0, Mandatory = $false)]
[string]$Path = $pwd.Path,
# Specifies the port of the http server.
[Parameter(Position = 1, Mandatory = $false)]
[int]$Port = 80
)
Begin {
function Test-PHPinPath
{
$env:path -split ";" | ForEach-Object -Begin {$containsPHP = @()} -Process {$containsPHP += Test-Path "$_\php.exe"} -End {$containsPHP -contains $true}
}
if (Test-PHPinPath) {
Throw "Could not find php.exe in PATH"
}
Start-Process -WorkingDirectory $Path -FilePath php -ArgumentList @("-S localhost:$Port")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment