diff --git a/panel/lib/DockerManager.php b/panel/lib/DockerManager.php index e1e4dca..9cd9e8e 100644 --- a/panel/lib/DockerManager.php +++ b/panel/lib/DockerManager.php @@ -397,10 +397,15 @@ SH; $yaml = $this->generateComposeYaml($appKey, $domain, $params); $stack = $this->createStack($accountId, "{$appKey}-{$domain}", $yaml); - // Write stack and start it - $out = $this->composeAction((int)$stack['id'], 'up'); - novacpx_log('info', "DockerManager: launched {$appKey} for account {$accountId} on {$domain}"); - return ['stack_id' => $stack['id'], 'dir' => $stack['dir'], 'output' => $out]; + // Pull images and start stack in background (image pulls can take minutes) + $dir = $stack['dir']; + $stackId = (int)$stack['id']; + $logFile = escapeshellarg("/tmp/novacpx-stack-{$stackId}.log"); + $compose = escapeshellarg("{$dir}/docker-compose.yml"); + shell_exec("nohup sudo docker compose -f {$compose} up -d > {$logFile} 2>&1 &"); + $this->db->execute("UPDATE docker_compose_stacks SET status='starting' WHERE id=?", [$stackId]); + novacpx_log('info', "DockerManager: launching {$appKey} for account {$accountId} on {$domain} (async)"); + return ['stack_id' => $stackId, 'dir' => $dir, 'output' => 'Launching in background — refresh in a moment to see status']; } private function generateComposeYaml(string $appKey, string $domain, array $p): string { diff --git a/panel/lib/EmailManager.php b/panel/lib/EmailManager.php index 432748e..87688b3 100644 --- a/panel/lib/EmailManager.php +++ b/panel/lib/EmailManager.php @@ -87,8 +87,8 @@ class EmailManager { $user = strstr($a['email'], '@', true); $mailboxes .= "{$a['email']} {$a['username']}/{$domain}/{$user}/\n"; } - file_put_contents('/etc/postfix/novacpx_mailboxes', $mailboxes); - shell_exec('postmap /etc/postfix/novacpx_mailboxes 2>/dev/null'); + self::writePostfixFile('/etc/postfix/novacpx_mailboxes', $mailboxes); + shell_exec('sudo postmap /etc/postfix/novacpx_mailboxes 2>/dev/null'); // Virtual alias map (forwarders) $forwarders = $db->fetchAll("SELECT source, destination FROM email_forwarders"); @@ -96,16 +96,23 @@ class EmailManager { foreach ($forwarders as $f) { $aliases .= "{$f['source']} {$f['destination']}\n"; } - file_put_contents('/etc/postfix/novacpx_aliases', $aliases); - shell_exec('postmap /etc/postfix/novacpx_aliases 2>/dev/null'); + self::writePostfixFile('/etc/postfix/novacpx_aliases', $aliases); + shell_exec('sudo postmap /etc/postfix/novacpx_aliases 2>/dev/null'); - // Virtual domains map - $domains = $db->fetchAll("SELECT DISTINCT SUBSTRING_INDEX(email,'@',-1) as domain FROM email_accounts WHERE status='active'"); + // Virtual domains map — SQLite-compatible (no SUBSTRING_INDEX) + $domains = $db->fetchAll("SELECT DISTINCT SUBSTR(email, INSTR(email,'@') + 1) AS domain FROM email_accounts WHERE status='active'"); $vdomains = ''; foreach ($domains as $d) { $vdomains .= "{$d['domain']} novacpx\n"; } - file_put_contents('/etc/postfix/novacpx_domains', $vdomains); - shell_exec('postmap /etc/postfix/novacpx_domains 2>/dev/null'); - shell_exec('systemctl reload postfix 2>/dev/null || true'); + self::writePostfixFile('/etc/postfix/novacpx_domains', $vdomains); + shell_exec('sudo postmap /etc/postfix/novacpx_domains 2>/dev/null'); + shell_exec('sudo systemctl reload postfix 2>/dev/null || true'); + } + + private static function writePostfixFile(string $path, string $content): void { + $tmp = tempnam('/tmp', 'ncpx_pf_'); + file_put_contents($tmp, $content); + shell_exec('sudo tee ' . escapeshellarg($path) . ' > /dev/null < ' . escapeshellarg($tmp)); + @unlink($tmp); } private static function hashPassword(string $password): string {