-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgetphp.ps1
More file actions
837 lines (721 loc) · 38.2 KB
/
Copy pathgetphp.ps1
File metadata and controls
837 lines (721 loc) · 38.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
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
# ========================================
# --- ENVIRONMENT CONFIGURATION ----------
# ========================================
$RemoteUrl = 'https://raw.githubusercontent.com/getphporg/getphp/HEAD/getphp.ps1'
$BaseDir = 'C:\getphp'
$DocRoot = "$BaseDir\www"
$LogsDir = "$BaseDir\logs"
$MaxRetries = 5
$RetryDelay = 5
$WinGetId = 'Microsoft.WinGet.Client'
$VCRedistId = 'Microsoft.VCRedist.2015+.x64'
$ApacheLatest = '2.4.68'
$ApacheUrl = 'https://www.apachelounge.com/download/VS18/binaries/httpd-2.4.68-260617-Win64-VS18.zip'
$ApacheZip = "$env:TEMP\apache.zip"
$ApacheExtTemp = "$env:TEMP\apache_extract"
$ApacheName = 'Apache2.4'
$ApacheDir = "$BaseDir\apache"
$ApacheBin = "$ApacheDir\bin"
$HttpdConf = "$ApacheDir\conf\httpd.conf"
$HttpdExe = "$ApacheDir\bin\httpd.exe"
$MysqlLatest = '9.7.1'
$MysqlUrl = 'https://cdn.mysql.com/Downloads/MySQL-9.7/mysql-9.7.1-winx64.msi'
$MysqlZip = "$env:TEMP\mysql.zip"
$MysqlExtTemp = "$env:TEMP\mysql_extract"
$MysqlId = 'Oracle.MySQL'
$MysqlName = 'MySQL'
$MysqlDir = "$BaseDir\mysql"
$MysqldExe = "$MysqlDir\bin\mysqld.exe"
$PhpLatest = '8.5.8'
$PhpUrl = 'https://downloads.php.net/~windows/releases/archives/php-8.5.8-Win32-vs17-x64.zip'
$PhpZip = "$env:TEMP\php.zip"
$PhpExtTemp = "$env:TEMP\php_extract"
$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"
$YELLOW = "${ESC}[33m"
$CYAN = "${ESC}[36m"
$BOLD = "${ESC}[1m"
$UNDERLINE = "${ESC}[4m"
$RESET = "${ESC}[0m"
$TL = [char]0x250C
$TR = [char]0x2510
$BL = [char]0x2514
$BR = [char]0x2518
$VT = [char]0x2502
$HZ = [char]0x2500
$HZBAR = "$HZ" * 36
Write-Host
Write-Host "${TL}${HZBAR}${TR}"
Write-Host "${VT} _ ____ _ _ ____ ${VT}"
Write-Host "${VT} __ _ ___| |_| _ \| | | | _ \ ${VT}"
Write-Host "${VT} / _`` |/ _ \ __| |_) | |_| | |_) | ${VT}"
Write-Host "${VT} | (_| | __/ |_| __/| _ | __/ ${VT}"
Write-Host "${VT} \__, |\___|\__|_| |_| |_|_| ${VT}"
Write-Host "${VT} |___/ ${CYAN}get${RESET}${YELLOW}PHP${RESET}.org ${VT}"
Write-Host "${BL}${HZBAR}${BR}"
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:${RESET} The getPHP script MUST be run as an Administrator!"
Write-Host "${GREEN}SOLUTION:${RESET} Right-click on Start Menu and select 'Terminal (Admin)'."
Write-Host
exit 1
}
# Check WinGet
if (Get-Command winget -ErrorAction SilentlyContinue) { $WINGET = 1 } else { $WINGET = 0 }
# Check VCRedist
if ((winget list --id $VCRedistId --accept-source-agreements) -match 'Microsoft.VCRedist.2015\+.x64') { $VCREDIST = 1 } else { $VCREDIST = 0 }
Write-Host 'Your PHP Stack:'
Write-Host '~~~~~~~~~~~~~~~'
Write-Host "Apache ${CYAN}------->${RESET} " -NoNewline
$ApacheVersion = $false
if (Test-Path -Path $HttpdExe) {
$ApacheVersionRaw = & $HttpdExe -v 2>$null
if ($ApacheVersionRaw[0] -match 'Apache/([\d\.]+)') {
$ApacheVersion = $Matches[1]
}
}
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 = $false
if (Get-Command php -ErrorAction SilentlyContinue) {
$PhpVersionRaw = (php -v)[0]
if ($PhpVersionRaw -match 'PHP\s+([\d\.]+)') {
$PhpVersion = $Matches[1]
}
}
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 $VCREDIST -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) {
try {
Install-Module -Name $WinGetId -Force -AllowClobber -ErrorAction Stop
Repair-WinGetPackageManager -AllUsers -ErrorAction Stop
}
catch {
Write-Host "[${RED} ERROR ${RESET}] Failed to install or repair WinGet. Please try again later."
exit 1
}
$WINGET = 1
Write-Host "[${GREEN} OK ${RESET}] Installed WinGet."
} else {
Write-Host "[${GREEN} OK ${RESET}] WinGet is already installed."
}
# ========================================
# --- INSTALL VCREDIST -------------------
# ========================================
if ($VCREDIST -eq 0) {
$global:LASTEXITCODE = 0
winget install --id $VCRedistId --accept-package-agreements
if ($LASTEXITCODE -ne 0) {
Write-Host "[${RED} ERROR ${RESET}] Failed to download Visual C++ Redistributable. Please try again later."
exit $LASTEXITCODE
}
$VCREDIST = 1
Write-Host "[${GREEN} OK ${RESET}] Installed Visual C++ Redistributable."
} else {
Write-Host "[${GREEN} OK ${RESET}] Visual C++ Redistributable is already installed."
}
# ========================================
# --- INSTALL APACHE ---------------------
# ========================================
if ($APACHE -eq 0) {
Write-Host "[${GREEN} OK ${RESET}] Downloading Apache..."
try {
(New-Object System.Net.WebClient).DownloadFile($ApacheUrl, $ApacheZip)
Write-Host "[${GREEN} OK ${RESET}] Successfully downloaded Apache."
}
catch {
Write-Host "[${RED} ERROR ${RESET}] Failed to download Apache. Please try again later."
exit 1
}
# Unzip Apache into the user's temp directory
if (Test-Path -Path $ApacheExtTemp) { Remove-Item $ApacheExtTemp -Recurse -Force }
Write-Host "[${GREEN} OK ${RESET}] Extracting Apache..."
Expand-Archive -Path $ApacheZip -DestinationPath $ApacheExtTemp
Write-Host "[${GREEN} OK ${RESET}] Successfully extracted Apache."
# Prepare destination directory
if (Test-Path -Path $ApacheDir) { Remove-Item $ApacheDir -Recurse -Force }
# The zip extracts into a subdirectory like 'Apache-x.x.x', so we grab the inner directory
$ApacheInnerDirectory = Get-ChildItem $ApacheExtTemp | Where-Object { $_.PSIsContainer } | Select-Object -First 1
# Move the phpmyadmin directory to its final location
New-Item -ItemType Directory -Path $ApacheDir -Force | Out-Null
Move-Item -Path "$($ApacheInnerDirectory.FullName)\*" -Destination $ApacheDir
Write-Host "[${GREEN} OK ${RESET}] Moved Apache to its final location."
# Clean up temp download files
Remove-Item $ApacheZip -Force
Remove-Item $ApacheExtTemp -Recurse -Force
Write-Host "[${GREEN} OK ${RESET}] Cleaned up temporary download files."
$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) {
$global:LASTEXITCODE = 0
winget install --id $MysqlId --exact --override "/quiet INSTALLDIR=${MysqlDir}"
if ($LASTEXITCODE -ne 0) {
Write-Host "[${RED} ERROR ${RESET}] Failed to download MySQL. Please try again later."
exit $LASTEXITCODE
}
$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) {
Write-Host "[${GREEN} OK ${RESET}] Downloading PHP..."
try {
(New-Object System.Net.WebClient).DownloadFile($PhpUrl, $PhpZip)
Write-Host "[${GREEN} OK ${RESET}] Successfully downloaded PHP."
}
catch {
Write-Host "[${RED} ERROR ${RESET}] Failed to download PHP. Please try again later."
exit 1
}
# Unzip PHP into the user's temp directory
if (Test-Path -Path $PhpExtTemp) { Remove-Item $PhpExtTemp -Recurse -Force }
Write-Host "[${GREEN} OK ${RESET}] Extracting PHP..."
Expand-Archive -Path $PhpZip -DestinationPath $PhpExtTemp
Write-Host "[${GREEN} OK ${RESET}] Successfully extracted PHP."
# Prepare destination directory
if (Test-Path -Path $PhpDir) { Remove-Item $PhpDir -Recurse -Force }
New-Item -ItemType Directory -Path $PhpDir -Force | Out-Null
# Move all loose files and folders directly from the temporary folder to the final location
Move-Item -Path "$PhpExtTemp\*" -Destination $PhpDir -Force
Write-Host "[${GREEN} OK ${RESET}] Moved PHP to its final location."
# Clean up temp download files
Remove-Item $PhpZip -Force
Remove-Item $PhpExtTemp -Recurse -Force
Write-Host "[${GREEN} OK ${RESET}] Cleaned up temporary download files."
$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) {
Write-Host "[${GREEN} OK ${RESET}] Downloading phpMyAdmin..."
$Success = $false
for ($i = 1; $i -le $MaxRetries; $i++) {
try {
if ($i -gt 1) {
Write-Host "[${GREEN} OK ${RESET}] Connection dropped. Retrying download (attempt $i of $MaxRetries)..."
}
(New-Object System.Net.WebClient).DownloadFile($PmaUrl, $PmaZip)
Write-Host "[${GREEN} OK ${RESET}] Successfully downloaded phpMyAdmin."
$Success = $true
break
}
catch {
if ($i -lt $MaxRetries) {
Start-Sleep -Seconds $RetryDelay
}
}
}
# If it still failed after all retries, throw the final error and exit
if (-not $Success) {
Write-Host "[${RED} ERROR ${RESET}] Failed to download phpMyAdmin after $MaxRetries attempts. Please try again later."
exit 1
}
# 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', 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 ServerName
(Get-Content $HttpdConf) -replace '#\s*ServerName www\.example\.com:80', 'ServerName localhost:80' | Set-Content $HttpdConf
Write-Host "[${GREEN} OK ${RESET}] Updated ServerName 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 1
}
# ========================================
# --- 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 1
}
} else {
Write-Host "[${RED} ERROR ${RESET}] Could not locate the PHP directory."
exit 1
}
# ========================================
# --- 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 1
}
# ========================================
# --- 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 1
}
} else {
Write-Host "[${RED} ERROR ${RESET}] Could not locate the PHP directory."
exit 1
}
# ========================================
# --- ADD PHP TO SYSTEM PATH -------------
# ========================================
if (Test-Path -Path $PhpDir) {
Write-Host "[${GREEN} OK ${RESET}] Located the PHP installation directory."
$CurrentSystemPath = [Environment]::GetEnvironmentVariable("Path", [EnvironmentVariableTarget]::Machine)
if (($CurrentSystemPath -split ';').TrimEnd('\') -contains $PhpDir.TrimEnd('\')) {
Write-Host "[${GREEN} OK ${RESET}] PHP is already present in the System Path."
} else {
$NewSystemPath = if ($CurrentSystemPath -and !$CurrentSystemPath.EndsWith(';')) { "$CurrentSystemPath;$PhpDir" } else { "$CurrentSystemPath$PhpDir" }
[Environment]::SetEnvironmentVariable("Path", $NewSystemPath, [EnvironmentVariableTarget]::Machine)
Write-Host "[${GREEN} OK ${RESET}] Added PHP to the System Path."
}
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
} else {
Write-Host "[${RED} ERROR ${RESET}] Could not locate the PHP installation directory."
exit 1
}
# ========================================
# --- 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 1
}
# ========================================
# --- 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 1
}
# ========================================
# --- INSTALLATION RESULT ----------------
# ========================================
if ($WINGET -eq 1 -and $VCREDIST -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!"
irm https://raw.githubusercontent.com/getphporg/getphp/HEAD/getphp.ps1 | iex
} else {
Write-Host "[${RED} ERROR ${RESET}] INSTALLATION FAILED!"
Write-Host
}
}
"^[uU](pdate)?$" {
Write-Host "[${GREEN} OK ${RESET}] Checking Visual C++ Redistributable version..."
if ($VCREDIST -eq 1) { winget upgrade --id $VCRedistId --exact } else { Write-Host "[${GREEN} OK ${RESET}] Visual C++ Redistributable is not installed." }
Write-Host "[${GREEN} OK ${RESET}] Checking Apache version..."
if ($APACHE -eq 1) {
if ($ApacheVersion -ne $ApacheLatest) {
# TODO: Update Apache
} else {
Write-Host "[${GREEN} OK ${RESET}] No available upgrade found."
}
} else {
Write-Host "[${GREEN} OK ${RESET}] Apache is not installed."
}
Write-Host "[${GREEN} OK ${RESET}] Checking MySQL version..."
if ($MYSQL -eq 1) { winget upgrade --id $MysqlId --exact } else { Write-Host "[${GREEN} OK ${RESET}] MySQL is not installed." }
Write-Host "[${GREEN} OK ${RESET}] Checking PHP version..."
if ($PHP -eq 1) {
if ($PhpVersion -ne $PhpLatest) {
# TODO: Update PHP
} else {
Write-Host "[${GREEN} OK ${RESET}] No available upgrade found."
}
} else {
Write-Host "[${GREEN} OK ${RESET}] PHP 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."
}
irm https://raw.githubusercontent.com/getphporg/getphp/HEAD/getphp.ps1 | iex
}
"^[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."
}
irm https://raw.githubusercontent.com/getphporg/getphp/HEAD/getphp.ps1 | iex
}
"^[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."
}
irm https://raw.githubusercontent.com/getphporg/getphp/HEAD/getphp.ps1 | iex
}
"^[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 MySQL package..."
if ($MYSQL -eq 1) { winget uninstall --id $MysqlId --exact } else { Write-Host "[${GREEN} OK ${RESET}] MySQL is not installed." }
# Delete 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."
irm https://raw.githubusercontent.com/getphporg/getphp/HEAD/getphp.ps1 | iex
}
}
"^[qQ](uit)?$" {
Write-Host "[${GREEN} OK ${RESET}] Goodbye!"
Write-Host
}
Default {
Write-Host "[${RED} ERROR ${RESET}] Command not recognized."
irm https://raw.githubusercontent.com/getphporg/getphp/HEAD/getphp.ps1 | iex
}
}