Skip to content

Instantly share code, notes, and snippets.

@sajdoko
Last active August 8, 2024 11:19
Show Gist options
  • Save sajdoko/5e197b0f0e5963177f052fc69c207c28 to your computer and use it in GitHub Desktop.
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.
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."
@sajdoko
Copy link
Author

sajdoko commented Aug 8, 2024

This PowerShell script automates the setup of a local development environment for WordPress or Laravel using XAMPP.
It performs the following tasks:

  1. Validates user input for the domain name and environment type.
  2. Backs up the hosts file and the httpd-vhosts.conf file.
  3. Adds an entry to the hosts file under the # Local Domains section.
  4. Edits the httpd-vhosts.conf file to add a VirtualHost entry for the specified domain.
  5. Creates the project directory if it doesn't already exist.
  6. Creates a MySQL database for the project if it doesn't already exist.
  7. Generates SSL certificates for the domain using mkcert if they don't already exist.
  8. Restarts the Apache server.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment