70 lines
2.2 KiB
PHP
70 lines
2.2 KiB
PHP
<?php
|
|
/**
|
|
* Server Environment Fixer & Probe
|
|
* This script helps diagnose and fix DB connection issues on the server.
|
|
*/
|
|
|
|
header('Content-Type: text/plain; charset=utf-8');
|
|
|
|
echo "=== Server Environment Probe ===\n";
|
|
echo "PHP Version: " . PHP_VERSION . "\n";
|
|
echo "Current Directory: " . __DIR__ . "\n";
|
|
echo "Hostname: " . gethostname() . "\n";
|
|
|
|
$envPath = __DIR__ . '/.env';
|
|
echo "Checking .env at: $envPath\n";
|
|
|
|
if (file_exists($envPath)) {
|
|
echo ".env exists.\n";
|
|
$content = file_get_contents($envPath);
|
|
echo "--- .env Content (masked) ---\n";
|
|
foreach (explode("\n", $content) as $line) {
|
|
if (trim($line) === '') continue;
|
|
if (strpos($line, 'DB_PASS') !== false) {
|
|
echo "DB_PASS=********\n";
|
|
} else {
|
|
echo $line . "\n";
|
|
}
|
|
}
|
|
} else {
|
|
echo ".env DOES NOT EXIST.\n";
|
|
}
|
|
|
|
echo "\n=== Database Connection Test ===\n";
|
|
|
|
$hosts_to_test = ['localhost', '127.0.0.1', 'mysql'];
|
|
$db_name = 'chandj';
|
|
$db_user = 'root'; // Usually 'root' or matches system user
|
|
$db_pass = 'root'; // Change as needed
|
|
|
|
foreach ($hosts_to_test as $host) {
|
|
echo "Testing host [$host]... ";
|
|
try {
|
|
$dsn = "mysql:host=$host;dbname=$db_name;charset=utf8mb4";
|
|
$pdo = new PDO($dsn, $db_user, $db_pass, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
|
|
echo "SUCCESS!\n";
|
|
|
|
if (isset($_GET['fix']) && $_GET['fix'] === 'true') {
|
|
echo "Updating .env to use host [$host]...\n";
|
|
$newEnv = "APP_URL=https://sales.sam.kr/\n";
|
|
$newEnv .= "DB_HOST=$host\n";
|
|
$newEnv .= "DB_NAME=$db_name\n";
|
|
$newEnv .= "DB_USER=$db_user\n";
|
|
$newEnv .= "DB_PASS=$db_pass\n";
|
|
$newEnv .= "DOCUMENT_ROOT=" . str_replace('\\', '/', __DIR__) . "\n";
|
|
|
|
if (file_put_contents($envPath, $newEnv)) {
|
|
echo "Successfully wrote to .env\n";
|
|
} else {
|
|
echo "FAILED to write to .env. Check permissions.\n";
|
|
}
|
|
}
|
|
break;
|
|
} catch (Exception $e) {
|
|
echo "FAILED: " . $e->getMessage() . "\n";
|
|
}
|
|
}
|
|
|
|
echo "\nUsage: Append ?fix=true to the URL to automatically create/update .env if a connection succeeds.\n";
|
|
echo "SECURITY: DELETE THIS FILE AFTER USE!\n";
|