-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgetphp.ps1
More file actions
676 lines (581 loc) · 32.2 KB
/
Copy pathgetphp.ps1
File metadata and controls
676 lines (581 loc) · 32.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
# ========================================
# --- ENVIRONMENT CONFIGURATION ----------
# ========================================
$RemoteUrl = 'https://raw.githubusercontent.com/getphporg/getphp/HEAD/getphp.ps1'
$BaseDir = 'C:\getphp'
$DocRoot = "$BaseDir\www"
$LogsDir = "$BaseDir\logs"
$WingetName = 'Microsoft.WinGet.Client'
$ApacheId = 'ApacheLounge.httpd'
$ApacheName = 'Apache2.4'
$ApacheDir = "$BaseDir\apache"
$ApacheBin = "$ApacheDir\Apache24\bin"
$HttpdConf = "$ApacheDir\Apache24\conf\httpd.conf"
$HttpdExe = "$ApacheDir\Apache24\bin\httpd.exe"
$MysqlId = 'Oracle.MySQL'
$MysqlName = 'MySQL'
$MysqlDir = "$BaseDir\mysql"
$MysqldExe = "$MysqlDir\bin\mysqld.exe"
$PhpId = 'PHP.PHP.8.5'
$PhpDir = "$BaseDir\php"
$PhpExe = "$PhpDir\php.exe"
$PhpIniDev = "$PhpDir\php.ini-development"
$PhpIni = "$PhpDir\php.ini"
$PhpInfo = "$DocRoot\phpinfo.php"
$PmaLatest = '5.2.3'
$PmaUrl = 'https://www.phpmyadmin.net/downloads/phpMyAdmin-latest-all-languages.zip'
$PmaZip = "$env:TEMP\phpmyadmin.zip"
$PmaExtTemp = "$env:TEMP\pma_extract"
$PmaDir = "$BaseDir\phpmyadmin"
$PmaConf = "$PmaDir\config.inc.php"
$PmaJson = "$PmaDir\package.json"
$ESC = [char]0x1b
$RED = "${ESC}[31m"
$GREEN = "${ESC}[32m"
$CYAN = "${ESC}[36m"
$BOLD = "${ESC}[1m"
$UNDERLINE = "${ESC}[4m"
$RESET = "${ESC}[0m"
Write-Host
Write-Host '┌────────────────────────────────────┐'
Write-Host '│ _ ____ _ _ ____ │'
Write-Host '│ __ _ ___| |_| _ \| | | | _ \ │'
Write-Host '│ / _` |/ _ \ __| |_) | |_| | |_) | │'
Write-Host '│ | (_| | __/ |_| __/| _ | __/ │'
Write-Host '│ \__, |\___|\__|_| |_| |_|_| │'
Write-Host "│ |___/${CYAN} www.getPHP.org${RESET} │"
Write-Host '└────────────────────────────────────┘'
Write-Host
# Check for admin privileges
$CurrentIdentity = [Security.Principal.WindowsIdentity]::GetCurrent()
$Principal = New-Object Security.Principal.WindowsPrincipal($CurrentIdentity)
$Admin = $Principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
if (-not $Admin) {
Write-Host "${RED} WARNING: The getPHP script MUST be run as an Administrator!${RESET}"
Write-Host "${CYAN}SOLUTION: Right-click on Start Menu and select 'Terminal (Admin)'.${RESET}"
Write-Host
exit
}
if (Get-Command winget -ErrorAction SilentlyContinue) { $WINGET = 1 } else { $WINGET = 0 }
Write-Host 'Your PHP Stack:'
Write-Host '~~~~~~~~~~~~~~~'
Write-Host "Apache ${CYAN}------->${RESET} " -NoNewline
$ApacheVersion = ((winget list --id $ApacheId) -match $ApacheId -split " {1,}")[4]
if ($ApacheVersion) { Write-Host $ApacheVersion; $APACHE = 1 } else { $APACHE = 0; Write-Host "${RED}not installed${RESET}" }
Write-Host "MySQL ${CYAN}-------->${RESET} " -NoNewline
$MysqlVersion = ((winget list --id $MysqlId) -match $MysqlId -split " {1,}")[4]
if ($MysqlVersion) { Write-Host $MysqlVersion; $MYSQL = 1 } else { $MYSQL = 0; Write-Host "${RED}not installed${RESET}" }
Write-Host "PHP ${CYAN}---------->${RESET} " -NoNewline
$PhpVersion = ((winget list --id $PhpId) -match $PhpId -split " {1,}")[3]
if ($PhpVersion) { Write-Host $PhpVersion; $PHP = 1 } else { $PHP = 0; Write-Host "${RED}not installed${RESET}" }
Write-Host "phpMyAdmin ${CYAN}--->${RESET} " -NoNewline
if (Test-Path -Path $PmaJson) {
$PmaJsonLiteral = (Resolve-Path -Path $PmaJson | Select-Object -First 1).Path
$PmaVersion = (Get-Content -Raw -Path $PmaJsonLiteral | ConvertFrom-Json).version
} else {
$PmaVersion = 'x.x.x'
}
if (Test-Path -Path $PmaDir) { Write-Host $PmaVersion; $PHPMYADMIN = 1 } else { $PHPMYADMIN = 0; Write-Host "${RED}not installed${RESET}" }
if ($WINGET -eq 1 -and $APACHE -eq 1 -and $MYSQL -eq 1 -and $PHP -eq 1 -and $PHPMYADMIN -eq 1) { $STACK = 1 } else { $STACK = 0 }
Write-Host
Write-Host 'Service Status:'
Write-Host '~~~~~~~~~~~~~~~'
Write-Host "Apache ${CYAN}------->${RESET} " -NoNewline
$ApacheService = Get-Service -Name $ApacheName -ErrorAction SilentlyContinue
if ($ApacheService) { Write-Host $ApacheService.Status } else { Write-Host "${RED}not available${RESET}" }
Write-Host "MySQL ${CYAN}-------->${RESET} " -NoNewline
$MysqlService = Get-Service -Name $MysqlName -ErrorAction SilentlyContinue
if ($MysqlService) { Write-Host $MysqlService.Status } else { Write-Host "${RED}not available${RESET}" }
Write-Host
if ($STACK -eq 1) {
Write-Host "${CYAN}Where to put website files?${RESET} $DocRoot"
Write-Host "${CYAN}How to test your PHP setup?${RESET} http://localhost/phpinfo.php"
Write-Host "${CYAN}Where to access phpMyAdmin?${RESET} http://localhost/phpmyadmin"
Write-Host "${CYAN}How to log into phpMyAdmin?${RESET} Username: root | Password: [blank]"
Write-Host
}
Write-Host 'Stack Commands:'
Write-Host '~~~~~~~~~~~~~~~'
Write-Host "${CYAN}${UNDERLINE}I${RESET}${CYAN}nstall${RESET} Install/repair the PHP stack."
Write-Host "${CYAN}${UNDERLINE}U${RESET}${CYAN}pdate${RESET} Update packages to latest versions."
Write-Host "${CYAN}${UNDERLINE}R${RESET}${CYAN}estart${RESET} Restart all PHP stack services."
Write-Host "${CYAN}${UNDERLINE}S${RESET}${CYAN}top${RESET} Stop all PHP stack services."
Write-Host "${CYAN}${UNDERLINE}D${RESET}${CYAN}elete${RESET} Show delete information."
Write-Host "${CYAN}${UNDERLINE}Q${RESET}${CYAN}uit${RESET} Quit this application."
Write-Host
Write-Host "${BOLD}==> Enter command:${RESET} " -NoNewline
$Command = Read-Host
Write-Host
switch -Regex ($Command) {
"^[iI](nstall)?$" {
# ========================================
# --- CREATE DOCUMENT ROOT DIRECTORY -----
# ========================================
if (-not (Test-Path -Path $DocRoot)) {
New-Item -ItemType Directory -Path $DocRoot | Out-Null
Write-Host "[${GREEN} OK ${RESET}] Created directory: $DocRoot"
} else {
Write-Host "[${GREEN} OK ${RESET}] Directory already exists: $DocRoot"
}
# ========================================
# --- CREATE LOGS DIRECTORY --------------
# ========================================
if (-not (Test-Path -Path $LogsDir)) {
New-Item -ItemType Directory -Path $LogsDir | Out-Null
Write-Host "[${GREEN} OK ${RESET}] Created directory: $LogsDir"
} else {
Write-Host "[${GREEN} OK ${RESET}] Directory already exists: $LogsDir"
}
# ========================================
# --- INSTALL WINGET ---------------------
# ========================================
if ($WINGET -eq 0) {
Install-Module -Name $WingetName -Force -AllowClobber
Repair-WinGetPackageManager -AllUsers
$WINGET = 1
Write-Host "[${GREEN} OK ${RESET}] Installed WinGet."
} else {
Write-Host "[${GREEN} OK ${RESET}] WinGet is already installed."
}
# ========================================
# --- INSTALL APACHE ---------------------
# ========================================
if ($APACHE -eq 0) {
winget install --id $ApacheId --exact --location $ApacheDir
$APACHE = 1
Write-Host "[${GREEN} OK ${RESET}] Installed Apache."
} else {
Write-Host "[${GREEN} OK ${RESET}] Apache is already installed."
}
# ========================================
# --- INSTALL MYSQL ----------------------
# ========================================
if ($MYSQL -eq 0) {
winget install --id $MysqlId --exact --override "/quiet INSTALLDIR=${MysqlDir}"
$MYSQL = 1
Write-Host "[${GREEN} OK ${RESET}] Installed MySQL."
} else {
Write-Host "[${GREEN} OK ${RESET}] MySQL is already installed."
}
# ========================================
# --- INSTALL PHP ------------------------
# ========================================
if ($PHP -eq 0) {
winget install --id $PhpId --exact --location $PhpDir
$PHP = 1
Write-Host "[${GREEN} OK ${RESET}] Installed PHP."
} else {
Write-Host "[${GREEN} OK ${RESET}] PHP is already installed."
}
# ========================================
# --- INSTALL PHPMYADMIN -----------------
# ========================================
if ($PHPMYADMIN -eq 0) {
# Download phpMyAdmin from the official website.
Write-Host "[${GREEN} OK ${RESET}] Downloading phpMyAdmin..."
try {
Invoke-WebRequest -Uri $PmaUrl -OutFile $PmaZip
Write-Host "[${GREEN} OK ${RESET}] Successfully downloaded phpMyAdmin."
}
catch {
Write-Host "[${RED} Error ${RESET}] Upstream phpMyAdmin server is unavailable or the URL is broken."
Write-Host "[${RED} Error ${RESET}] Installation aborted safely to prevent directory corruption."
exit
}
# Unzip phpMyAdmin into the user's temp directory
if (Test-Path -Path $PmaExtTemp) { Remove-Item $PmaExtTemp -Recurse -Force }
Write-Host "[${GREEN} OK ${RESET}] Extracting phpMyAdmin..."
Expand-Archive -Path $PmaZip -DestinationPath $PmaExtTemp
Write-Host "[${GREEN} OK ${RESET}] Successfully extracted phpMyAdmin."
# Prepare destination directory
if (Test-Path -Path $PmaDir) { Remove-Item $PmaDir -Recurse -Force }
# The zip extracts into a subdirectory like 'phpMyAdmin-x.x.x-all-languages', so we grab the inner directory
$PmaInnerDirectory = Get-ChildItem $PmaExtTemp | Where-Object { $_.PSIsContainer } | Select-Object -First 1
# Move the phpmyadmin directory to its final location
New-Item -ItemType Directory -Path $PmaDir -Force | Out-Null
Move-Item -Path "$($PmaInnerDirectory.FullName)\*" -Destination $PmaDir
Write-Host "[${GREEN} OK ${RESET}] Moved phpMyAdmin to its final location."
# Clean up temp download files
Remove-Item $PmaZip -Force
Remove-Item $PmaExtTemp -Recurse -Force
Write-Host "[${GREEN} OK ${RESET}] Cleaned up temporary download files."
$PHPMYADMIN = 1
Write-Host "[${GREEN} OK ${RESET}] Installed phpMyAdmin."
} else {
Write-Host "[${GREEN} OK ${RESET}] phpMyAdmin is already installed."
}
# ========================================
# --- HTTPD.CONF -------------------------
# ========================================
if (Test-Path -Path $HttpdConf) {
Write-Host "[${GREEN} OK ${RESET}] Located the httpd.conf file."
# Fix server root
$ServerRootPath = (Split-Path (Split-Path $HttpdConf -Parent) -Parent).Replace('\', '/')
(Get-Content $HttpdConf) -replace 'Define SRVROOT "c:/Apache24"', "Define SRVROOT `"$ServerRootPath`"" | Set-Content $HttpdConf
Write-Host "[${GREEN} OK ${RESET}] Fixed ServerRoot in the httpd.conf file."
# Enable port 80
(Get-Content $HttpdConf) -replace 'Listen 8080', 'Listen 80' | Set-Content $HttpdConf
Write-Host "[${GREEN} OK ${RESET}] Enabled port 80 in the httpd.conf file."
# Update DocumentRoot and Directory targets
$DocRootForward = $DocRoot -replace '\\', '/'
(Get-Content $HttpdConf) -replace 'DocumentRoot\s+"[^"]+"', "DocumentRoot `"$DocRootForward`"" | Set-Content $HttpdConf
(Get-Content $HttpdConf) -replace '<Directory\s+"[^"]+htdocs">', "<Directory `"$DocRootForward`">" | Set-Content $HttpdConf
Write-Host "[${GREEN} OK ${RESET}] Updated DocumentRoot and Directory in the httpd.conf file."
# Update log files destinations
$LogsDirForward = $LogsDir -replace '\\', '/'
(Get-Content $HttpdConf) -replace 'logs/error_log', "$LogsDirForward/error.log" | Set-Content $HttpdConf
(Get-Content $HttpdConf) -replace 'logs/access_log', "$LogsDirForward/access.log" | Set-Content $HttpdConf
Write-Host "[${GREEN} OK ${RESET}] Updated log files destinations in the httpd.conf file."
# Enable rewrite_module
(Get-Content $HttpdConf) -replace '#\s*LoadModule rewrite_module', 'LoadModule rewrite_module' | Set-Content $HttpdConf
(Get-Content $HttpdConf) -replace 'AllowOverride None', 'AllowOverride All' | Set-Content $HttpdConf
Write-Host "[${GREEN} OK ${RESET}] Enabled rewrite_module in the httpd.conf file."
# Update DirectoryIndex
(Get-Content $HttpdConf) -replace 'DirectoryIndex index.html', 'DirectoryIndex index.php index.html' | Set-Content $HttpdConf
Write-Host "[${GREEN} OK ${RESET}] Updated DirectoryIndex in the httpd.conf file."
# Dynamic PHP configuration
$PhpDirForward = $PhpDir -replace '\\', '/'
$PmaDirForward = $PmaDir -replace '\\', '/'
$PhpConfigLines = @(
''
'# BEGIN GETPHP CONFIG'
"LoadModule php_module `"${PhpDirForward}/php8apache2_4.dll`""
'<FilesMatch \.php$>'
' SetHandler application/x-httpd-php'
'</FilesMatch>'
"PHPIniDir `"${PhpDirForward}`""
''
"Alias /phpmyadmin `"${PmaDirForward}`""
"<Directory `"${PmaDirForward}`">"
' Options Indexes FollowSymLinks MultiViews'
' AllowOverride All'
' Require local'
'</Directory>'
'# END GETPHP CONFIG'
)
$PhpConfigContent = $PhpConfigLines -join "`r`n"
$CurrentContent = Get-Content -Path $HttpdConf -Raw
# Regex pattern to match everything from BEGIN to END markers across multiple lines
$Pattern = '(?ms)# BEGIN GETPHP CONFIG.*?# END GETPHP CONFIG'
if ($CurrentContent -match $Pattern) {
# Replace the old block with the fresh one
$NewContent = $CurrentContent -replace $Pattern, $PhpConfigContent
Set-Content -Path $HttpdConf -Value $NewContent -Force
Write-Host "[${GREEN} OK ${RESET}] Updated configuration block in the httpd.conf file."
} else {
# If markers don't exist yet, append them cleanly to the bottom
Add-Content -Path $HttpdConf -Value ("`r`n" + $PhpConfigContent)
Write-Host "[${GREEN} OK ${RESET}] Created configuration block in the httpd.conf file."
}
} else {
Write-Host "[${RED} Error ${RESET}] Could not locate the httpd.conf file."
exit
}
# ========================================
# --- PHP.INI ----------------------------
# ========================================
if (Test-Path -Path $PhpDir) {
if (Test-Path -Path $PhpIniDev) {
# Create php.ini from the php.ini-development template file
Copy-Item -Path $PhpIniDev -Destination $PhpIni
Write-Host "[${GREEN} OK ${RESET}] The php.ini file was successfully created."
# Read the content of the file as an array of strings
$PhpIniContent = Get-Content -Path $PhpIni
# Define the extensions to enable
$ExtensionsToEnable = @("curl", "fileinfo", "gd", "intl", "mbstring", "mysqli", "openssl", "pdo_mysql", "pdo_sqlite", "sodium", "sqlite3", "zip")
# Loop through each extension and remove the leading semicolon if present
foreach ($Ext in $ExtensionsToEnable) {
# This Regex pattern matches exactly ';extension=name' with zero whitespace
$TargetPattern = "^;(extension=$Ext)$"
# Replace the commented line with just the active line 'extension=name'
$PhpIniContent = $PhpIniContent -replace $TargetPattern, '$1'
}
# Save the modified content back to the php.ini file
Set-Content -Path $PhpIni -Value $PhpIniContent -Force
Write-Host "[${GREEN} OK ${RESET}] Enabled extensions in the php.ini file."
# Enable the Windows extension directory
$ExtensionDirPattern = '^;(extension_dir\s*=\s*"ext"\s*)$'
$PhpIniContent = $PhpIniContent -replace $ExtensionDirPattern, '$1'
# Swap "ext" with the PHP directory's absolute path (forward slashes)
$PhpIniContent = $PhpIniContent -replace 'extension_dir\s*=\s*"ext"', "extension_dir = `"${PhpDir}\ext`""
# Save the changes
Set-Content -Path $PhpIni -Value $PhpIniContent -Force
Write-Host "[${GREEN} OK ${RESET}] Updated extension_dir in the php.ini file."
Write-Host "[${GREEN} OK ${RESET}] Completed php.ini configuration."
} else {
Write-Host "[${RED} Error ${RESET}] Could not locate the php.ini-development template file."
exit
}
} else {
Write-Host "[${RED} Error ${RESET}] Could not locate the PHP directory."
exit
}
# ========================================
# --- CONFIG.INC.PHP (PMA) ---------------
# ========================================
if (Test-Path -Path $PmaDir) {
$PmaConfigLines = @(
'<?php'
'declare(strict_types=1);'
''
"`$cfg['blowfish_secret'] = '12345678901234567890123456789012';"
''
'$i = 0;'
'$i++;'
''
"`$cfg['Servers'][`$i]['auth_type'] = 'cookie';"
"`$cfg['Servers'][`$i]['host'] = '127.0.0.1';"
"`$cfg['Servers'][`$i]['compress'] = false;"
"`$cfg['Servers'][`$i]['AllowNoPassword'] = true;"
)
# Join the array into a single string separated by Windows line breaks
$PmaConfigContent = $PmaConfigLines -join "`r`n"
# Write it to the file
Set-Content -Path $PmaConf -Value $PmaConfigContent
Write-Host "[${GREEN} OK ${RESET}] Created the config.inc.php file for phpMyAdmin."
} else {
Write-Host "[${RED} Error ${RESET}] Could not locate the phpMyAdmin directory."
exit
}
# ========================================
# --- SYNC DLL FILES ---------------------
# ========================================
if (Test-Path -Path $PhpDir) {
if (Test-Path -Path $ApacheBin) {
# This can only be performed if the Apache service is stopped.
if ($ApacheService) {
Stop-Service -Name $ApacheName
Write-Host "[${GREEN} OK ${RESET}] Stopped ${ApacheName} service for binary synchronization."
}
$RequiredPatterns = @(
"libsqlite3.dll",
"icu*.dll",
"libcrypto*.dll",
"libssl*.dll",
"libsodium.dll",
"libssh2.dll",
"nghttp2.dll",
"brotli*.dll",
"libzstd.dll"
)
# Copy them into Apache's execution heart
foreach ($Pattern in $RequiredPatterns) {
if (Test-Path "$PhpDir\$Pattern") {
Copy-Item "$PhpDir\$Pattern" -Destination $ApacheBin -Force
}
}
# Clear Windows security locks on the newly moved binaries
Get-ChildItem -Path $ApacheBin -Recurse | Unblock-File
# Restart Apache service if available
if ($ApacheService) {
Start-Service -Name $ApacheName
Write-Host "[${GREEN} OK ${RESET}] Restarted ${ApacheName} service."
}
Write-Host "[${GREEN} OK ${RESET}] Binary synchronization complete."
} else {
Write-Host "[${RED} Error ${RESET}] Could not locate the Apache bin directory."
exit
}
} else {
Write-Host "[${RED} Error ${RESET}] Could not locate the PHP directory."
exit
}
# ========================================
# --- ADD PHP TO SYSTEM PATH -------------
# ========================================
if (Test-Path -Path $PhpExe) {
Write-Host "[${GREEN} OK ${RESET}] Located the php.exe file."
$CurrentSystemPath = [Environment]::GetEnvironmentVariable("Path", [EnvironmentVariableTarget]::Machine)
if ($CurrentSystemPath -split ';' -contains $PhpExe) {
Write-Host "[${GREEN} OK ${RESET}] PHP is already present in the System Path."
} else {
$NewPath = $CurrentSystemPath + ";$PhpExe"
[Environment]::SetEnvironmentVariable("Path", $NewPath, [EnvironmentVariableTarget]::Machine)
Write-Host "[${GREEN} OK ${RESET}] Added PHP to the System Path."
}
} else {
Write-Host "[${RED} Error ${RESET}] Could not locate the php.exe file."
exit
}
# ========================================
# --- CREATE PHPINFO.PHP FILE ------------
# ========================================
"<?php phpinfo(); ?>" | Set-Content $PhpInfo
Write-Host "[${GREEN} OK ${RESET}] Created file: ${PhpInfo}"
# ========================================
# --- START HTTPD SERVICE ----------------
# ========================================
if (Test-Path -Path $HttpdExe) {
Write-Host "[${GREEN} OK ${RESET}] Located the httpd.exe file."
if (-not $ApacheService) {
Unblock-File -Path $HttpdExe
& $HttpdExe -k install
Start-Service -Name $ApacheName
Write-Host "[${GREEN} OK ${RESET}] Registered and started the ${ApacheName} service."
} else {
Write-Host "[${GREEN} OK ${RESET}] The ${ApacheName} service is already registered."
}
} else {
Write-Host "[${RED} Error ${RESET}] Could not locate the httpd.exe file."
exit
}
# ========================================
# --- START MYSQLD SERVICE ---------------
# ========================================
if (Test-Path -Path $MysqldExe) {
Write-Host "[${GREEN} OK ${RESET}] Located the mysqld.exe file."
if (-not $MysqlService) {
Unblock-File -Path $MysqldExe
& $MysqldExe --initialize-insecure
& $MysqldExe --install
Start-Service -Name $MysqlName
Write-Host "[${GREEN} OK ${RESET}] Registered and started the ${MysqlName} service."
} else {
Write-Host "[${GREEN} OK ${RESET}] The ${MysqlName} service is already registered."
}
} else {
Write-Host "[${RED} Error ${RESET}] Could not locate the mysqld.exe file."
exit
}
# ========================================
# --- INSTALLATION RESULT ----------------
# ========================================
if ($WINGET -eq 1 -and $APACHE -eq 1 -and $MYSQL -eq 1 -and $PHP -eq 1 -and $PHPMYADMIN -eq 1) {
Write-Host "[${GREEN} OK ${RESET}] INSTALLATION COMPLETE!"
& powershell -NoProfile -ExecutionPolicy Bypass -Command "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; iex ((New-Object System.Net.WebClient).DownloadString('$RemoteUrl'))"
} else {
Write-Host "[${RED} Error ${RESET}] INSTALLATION FAILED!"
Write-Host
}
}
"^[uU](pdate)?$" {
Write-Host "[${GREEN} OK ${RESET}] Checking ${ApacheId} version..."
if ($APACHE -eq 1) { winget upgrade --id $ApacheId --exact } else { Write-Host "[${GREEN} OK ${RESET}] ${ApacheId} is not installed." }
Write-Host "[${GREEN} OK ${RESET}] Checking ${MysqlId} version..."
if ($MYSQL -eq 1) { winget upgrade --id $MysqlId --exact } else { Write-Host "[${GREEN} OK ${RESET}] ${MysqlId} is not installed." }
Write-Host "[${GREEN} OK ${RESET}] Checking ${PhpId} version..."
if ($PHP -eq 1) { winget upgrade --id $PhpId --exact } else { Write-Host "[${GREEN} OK ${RESET}] ${PhpId} is not installed." }
Write-Host "[${GREEN} OK ${RESET}] Checking phpMyAdmin version..."
if ($PHPMYADMIN -eq 1) {
if ($PmaVersion -ne $PmaLatest) {
# TODO: Update phpMyAdmin
} else {
Write-Host "[${GREEN} OK ${RESET}] No available upgrade found."
}
} else {
Write-Host "[${GREEN} OK ${RESET}] phpMyAdmin is not installed."
}
& powershell -NoProfile -ExecutionPolicy Bypass -Command "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; iex ((New-Object System.Net.WebClient).DownloadString('$RemoteUrl'))"
}
"^[rR](estart)?$" {
Write-Host "[${GREEN} OK ${RESET}] Restarting ${ApacheName} service..."
if ($ApacheService) {
Restart-Service -Name $ApacheName
Write-Host "[${GREEN} OK ${RESET}] Restarted ${ApacheName} service..."
} else {
Write-Host "[${GREEN} OK ${RESET}] ${ApacheName} service is not available."
}
Write-Host "[${GREEN} OK ${RESET}] Restarting ${MysqlName} service..."
if ($MysqlService) {
Restart-Service -Name $MysqlName
Write-Host "[${GREEN} OK ${RESET}] Restarted ${MysqlName} service..."
} else {
Write-Host "[${GREEN} OK ${RESET}] ${MysqlName} service is not available."
}
& powershell -NoProfile -ExecutionPolicy Bypass -Command "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; iex ((New-Object System.Net.WebClient).DownloadString('$RemoteUrl'))"
}
"^[sS](top)?$" {
Write-Host "[${GREEN} OK ${RESET}] Stopping ${ApacheName} service..."
if ($ApacheService) {
Stop-Service -Name $ApacheName
Write-Host "[${GREEN} OK ${RESET}] Stopped ${ApacheName} service..."
} else {
Write-Host "[${GREEN} OK ${RESET}] ${ApacheName} service is not available."
}
Write-Host "[${GREEN} OK ${RESET}] Stopping ${MysqlName} service..."
if ($MysqlService) {
Stop-Service -Name $MysqlName
Write-Host "[${GREEN} OK ${RESET}] Stopped ${MysqlName} service..."
} else {
Write-Host "[${GREEN} OK ${RESET}] ${MysqlName} service is not available."
}
& powershell -NoProfile -ExecutionPolicy Bypass -Command "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; iex ((New-Object System.Net.WebClient).DownloadString('$RemoteUrl'))"
}
"^[dD](elete)?$" {
Write-Host "${RED}THIS WILL BE DELETED:${RESET}"
Write-Host "${RED}- Apache, MySQL, PHP, and phpMyAdmin.${RESET}"
Write-Host "${RED}- Services, config files, and logs.${RESET}"
Write-Host
Write-Host "${GREEN}THIS WILL NOT BE DELETED:${RESET}"
Write-Host "${GREEN}- Your website files.${RESET}"
Write-Host "${GREEN}- Your MySQL databases.${RESET}"
Write-Host
Write-Host "Are you sure you want to proceed?"
Write-Host
Write-Host -NoNewline "${BOLD}==> Enter Yes/No${RESET}: "
$ConfirmDelete = Read-Host
Write-Host
if ($ConfirmDelete -match '^[yY](es)?$') {
# Stop and delete services
Write-Host "[${GREEN} OK ${RESET}] Stopping and deleting ${ApacheName} service..."
if ($ApacheService) {
Stop-Service -Name $ApacheName
sc.exe delete $ApacheName
} else {
Write-Host "[${GREEN} OK ${RESET}] ${ApacheName} service is not available."
}
Write-Host "[${GREEN} OK ${RESET}] Stopping and deleting ${MysqlName} service..."
if ($MysqlService) {
Stop-Service -Name $MysqlName
sc.exe delete $MysqlName
} else {
Write-Host "[${GREEN} OK ${RESET}] ${MysqlName} service is not available."
}
# Uninstall WinGet packages
Write-Host "[${GREEN} OK ${RESET}] Uninstalling ${ApacheId} package..."
if ($APACHE -eq 1) { winget uninstall --id $ApacheId --exact } else { Write-Host "[${GREEN} OK ${RESET}] ${ApacheId} is not installed." }
Write-Host "[${GREEN} OK ${RESET}] Uninstalling ${MysqlId} package..."
if ($MYSQL -eq 1) { winget uninstall --id $MysqlId --exact } else { Write-Host "[${GREEN} OK ${RESET}] ${MysqlId} is not installed." }
Write-Host "[${GREEN} OK ${RESET}] Uninstalling ${PhpId} package..."
if ($PHP -eq 1) { winget uninstall --id $PhpId --exact } else { Write-Host "[${GREEN} OK ${RESET}] ${PhpId} is not installed." }
# Delete remaining directories
if (Test-Path -Path $ApacheDir) {
Remove-Item -Path $ApacheDir -Recurse -Force -ErrorAction SilentlyContinue
Write-Host "[${GREEN} OK ${RESET}] Deleted: ${ApacheDir}"
} else {
Write-Host "[${GREEN} OK ${RESET}] ${ApacheDir} does not exist."
}
if (Test-Path -Path $PhpDir) {
Remove-Item -Path $PhpDir -Recurse -Force -ErrorAction SilentlyContinue
Write-Host "[${GREEN} OK ${RESET}] Deleted: ${PhpDir}"
} else {
Write-Host "[${GREEN} OK ${RESET}] ${PhpDir} does not exist."
}
if (Test-Path -Path $PmaDir) {
Remove-Item -Path $PmaDir -Recurse -Force -ErrorAction SilentlyContinue
Write-Host "[${GREEN} OK ${RESET}] Deleted: ${PmaDir}"
} else {
Write-Host "[${GREEN} OK ${RESET}] ${PmaDir} does not exist."
}
if (Test-Path -Path $LogsDir) {
Remove-Item -Path $LogsDir -Recurse -Force -ErrorAction SilentlyContinue
Write-Host "[${GREEN} OK ${RESET}] Deleted: ${LogsDir}"
} else {
Write-Host "[${GREEN} OK ${RESET}] ${LogsDir} does not exist."
}
Write-Host "[${GREEN} OK ${RESET}] DELETION COMPLETE!"
Write-Host
} else {
Write-Host "[${GREEN} OK ${RESET}] Nothing was deleted."
& powershell -NoProfile -ExecutionPolicy Bypass -Command "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; iex ((New-Object System.Net.WebClient).DownloadString('$RemoteUrl'))"
}
}
"^[qQ](uit)?$" {
Write-Host "[${GREEN} OK ${RESET}] Goodbye!"
Write-Host
}
Default {
Write-Host "[${RED} Error ${RESET}] Command not recognized."
& powershell -NoProfile -ExecutionPolicy Bypass -Command "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; iex ((New-Object System.Net.WebClient).DownloadString('$RemoteUrl'))"
}
}