Last active
October 9, 2024 11:27
-
-
Save sajdoko/5e197b0f0e5963177f052fc69c207c28 to your computer and use it in GitHub Desktop.
PowerShell script that automates the setup of a local development environment for WordPress or Laravel using XAMPP.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param() | |
function Validate-Input { | |
param ( | |
[string]$domain, | |
[string]$env | |
) | |
if ([string]::IsNullOrEmpty($domain)) { | |
Write-Host "Domain name cannot be empty. Please enter a valid domain name." -ForegroundColor Red | |
exit 1 | |
} | |
if ($env -ne "w" -and $env -ne "l") { | |
Write-Host "Invalid environment type. Please enter either 'w' for wordpress or 'l' for laravel." -ForegroundColor Red | |
exit 1 | |
} | |
} | |
function Backup-File { | |
param ( | |
[string]$filePath | |
) | |
if (Test-Path $filePath) { | |
Copy-Item $filePath "$filePath.bak" -Force | |
Write-Host "Backup of $filePath created." -ForegroundColor Green | |
} else { | |
Write-Host "File $filePath does not exist." -ForegroundColor DarkYellow | |
} | |
} | |
function Add-Hosts-Entry { | |
param ( | |
[string]$desiredIP = "127.0.0.1", | |
[string]$hostname | |
) | |
$hostsFilePath = "$($Env:WinDir)\system32\Drivers\etc\hosts" | |
$hostsFile = Get-Content $hostsFilePath | |
Write-Host "About to add $desiredIP for $hostname to hosts file" -ForegroundColor Gray | |
$escapedHostname = [Regex]::Escape($hostname) | |
$patternToMatch = If ($checkHostnameOnly) { ".*\s+$escapedHostname.*" } Else { ".*$desiredIP\s+$escapedHostname.*" } | |
If (($hostsFile) -match $patternToMatch) { | |
Write-Host $desiredIP.PadRight(20," ") "$hostname - not adding; already in hosts file" -ForegroundColor DarkYellow | |
} Else { | |
Write-Host $desiredIP.PadRight(20," ") "$hostname - adding to hosts file... " -ForegroundColor Yellow | |
Add-Content -Encoding UTF8 $hostsFilePath ("$desiredIP".PadRight(20, " ") + "$hostname") | |
Write-Host "$desiredIP $hostname added to hosts file." | |
} | |
} | |
function Edit-Vhosts-File { | |
param ( | |
[string]$vhostsFile, | |
[string]$domainDir, | |
[string]$domain, | |
[string]$env, | |
[string]$certDir | |
) | |
$content = @" | |
<VirtualHost $domain.test:80> | |
DocumentRoot "$domainDir" | |
<Directory "$domainDir"> | |
Order allow,deny | |
Allow from all | |
AllowOverride All | |
</Directory> | |
ServerName $domain.test | |
</VirtualHost> | |
<VirtualHost $domain.test:443> | |
DocumentRoot "$domainDir" | |
<Directory "$domainDir"> | |
Order allow,deny | |
Allow from all | |
AllowOverride All | |
</Directory> | |
ServerName $domain.test | |
SSLEngine On | |
SSLCertificateFile "$certDir\$domain.test.pem" | |
SSLCertificateKeyFile "$certDir\$domain.test-key.pem" | |
</VirtualHost> | |
"@ | |
if ($env -eq "l") { | |
$content = $content -replace 'DocumentRoot "' + $domainDir + '"', 'DocumentRoot "' + $domainDir + '\public"' | |
$content = $content -replace '<Directory "' + $domainDir + '">', '<Directory "' + $domainDir + '\public">' | |
} | |
$vhostsFileContent = Get-Content $vhostsFile -Raw | |
if ($vhostsFileContent -contains "ServerName $domain.test") { | |
Write-Host "$domain already exists in httpd-vhosts.conf." -ForegroundColor DarkYellow | |
return | |
} | |
Add-Content -Path $vhostsFile -Value $content | |
Write-Host "$domain added to httpd-vhosts.conf." -ForegroundColor Green | |
} | |
function Create-Directory { | |
param ( | |
[string]$path | |
) | |
if (-Not (Test-Path $path)) { | |
New-Item -ItemType Directory -Path $path | |
Write-Host "Directory $path created." -ForegroundColor Green | |
} else { | |
Write-Host "Directory $path already exists." -ForegroundColor DarkYellow | |
} | |
} | |
function Create-Database { | |
param ( | |
[string]$databaseName | |
) | |
$dbCheck = mysql -u root -e "SHOW DATABASES LIKE '$databaseName';" 2>&1 | |
if ($dbCheck -match $databaseName) { | |
Write-Host "Database $databaseName already exists." -ForegroundColor DarkYellow | |
return | |
} | |
Write-Host "Creating database $databaseName..." -ForegroundColor Green | |
Invoke-Expression "mysql -u root -e `"CREATE DATABASE $databaseName;`"" | |
} | |
function Generate-SSL-Certificates { | |
param ( | |
[string]$certDir, | |
[string]$domain | |
) | |
if (Test-Path "$certDir\$domain.test.pem") { | |
Write-Host "SSL certificates for $domain already exist." -ForegroundColor DarkYellow | |
return | |
} | |
Write-Host "Generating SSL certificates..." -ForegroundColor Green | |
Set-Location $certDir | |
Invoke-Expression "mkcert $domain.test" | |
} | |
function Restart-Apache { | |
Write-Host "Restarting Apache..." | |
Invoke-Expression "C:\xampp\xampp_stop.exe" | |
Start-Sleep -Seconds 5 | |
Invoke-Expression "C:\xampp\xampp_start.exe" | |
} | |
# Main script | |
if (-not $domain) { | |
$domain = Read-Host "Enter the domain name (e.g., exampledomain)" | |
} | |
if (-not $env) { | |
$env = Read-Host "Enter the environment type (w for wordpress/ l for laravel)" | |
} | |
Validate-Input -domain $domain -env $env | |
$hostsFilePath = "$($Env:WinDir)\system32\Drivers\etc\hosts" | |
$vhostsFilePath = "C:\xampp\apache\conf\extra\httpd-vhosts.conf" | |
$htdocsDir = "C:\xampp\htdocs" | |
$domainDir = "$htdocsDir\$domain" | |
$certDir = "C:\xampp\apache\conf\local" | |
Backup-File -filePath $hostsFilePath | |
Backup-File -filePath $vhostsFilePath | |
Add-Hosts-Entry -hostname "$domain.test" | |
Edit-Vhosts-File -vhostsFile $vhostsFilePath -domainDir $domainDir -domain $domain -env $env -certDir $certDir | |
Create-Directory -path $domainDir | |
Create-Database -databaseName $domain | |
Generate-SSL-Certificates -certDir $certDir -domain $domain | |
Restart-Apache | |
Write-Host "Done." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This PowerShell script automates the setup of a local development environment for WordPress or Laravel using XAMPP.
It performs the following tasks:
Read more here: https://sajdoko.com/blog/setting-up-a-local-development-environment-on-windows-for-wordpress-and-laravel-using-powershell/