'Missing required parameter: domainname']); return; } $enabledConfig = "/etc/apache2/sites-enabled/prod.$domain.conf"; $availableConfig = "/etc/apache2/sites-available/prod.$domain.conf"; if (!file_exists($enabledConfig)) { http_response_code(404); echo json_encode(['error' => 'Configuration file not found']); return; } if (!rename($enabledConfig, $availableConfig)) { http_response_code(500); echo json_encode(['error' => 'Failed to move configuration file']); return; } exec('systemctl reload apache2', $output, $returnCode); if ($returnCode !== 0) { http_response_code(500); echo json_encode(['error' => 'Failed to reload Apache', 'details' => implode("\n", $output)]); return; } echo json_encode(['success' => 'Production site disabled successfully']); } public static function enable($data): void { $domain = $data['domain'] ?? ''; if (empty($domain)) { http_response_code(400); echo json_encode(['error' => 'Missing required parameter: domainname']); return; } $availableConfig = "/etc/apache2/sites-available/prod.$domain.conf"; $enabledConfig = "/etc/apache2/sites-enabled/prod.$domain.conf"; if (!file_exists($availableConfig)) { http_response_code(404); echo json_encode(['error' => 'Configuration file not found']); return; } if (!rename($availableConfig, $enabledConfig)) { http_response_code(500); echo json_encode(['error' => 'Failed to move configuration file']); return; } exec('systemctl reload apache2', $output, $returnCode); if ($returnCode !== 0) { http_response_code(500); echo json_encode(['error' => 'Failed to reload Apache', 'details' => implode("\n", $output)]); return; } echo json_encode(['success' => 'Production site enabled successfully']); } public static function isenabled($data): void { $domain = $data['domain'] ?? ''; if (file_exists("/etc/apache2/sites-enabled/prod.$domain.conf")) { echo json_encode(['isenabled' => 'YES']); } else { echo json_encode(['isenabled' => 'NO']); } } public static function migrateFromDev($data): void { $username = $data['username'] ?? ''; $domain = $data['domain'] ?? ''; $adminName = $data['admin_name'] ?? ''; $adminPassword = $data['admin_password'] ?? ''; if (empty($username) || empty($domain) || empty($adminName) || empty($adminPassword)) { http_response_code(400); echo json_encode(['error' => 'Missing required parameters']); return; } $webDir = "/home/$username/prod.$domain"; $configFile = "/etc/apache2/sites-enabled/prod.$domain.conf"; $configTemplate = '/etc/apache2/site-config.in'; $devDatabaseName = $username . "_dev_"; $devDatabaseName = $devDatabaseName . str_replace(['-', '.'], ["_", "_"], $domain); $prodDatabaseName = $username . "_prod_"; $prodDatabaseName = $prodDatabaseName . str_replace(['-', '.'], ["_", "_"], $domain); exec("sudo /usr/bin/mkdir $webDir", $mkdirOutput, $mkdirReturnCode); if ($mkdirReturnCode !== 0) { http_response_code(500); echo json_encode(['error' => 'Failed to create web dir', 'details' => implode("\n", $mkdirOutput)]); return; } exec("sudo /usr/bin/chown $username:$username $webDir", $chownOutput, $chownReturnCode); if ($chownReturnCode !== 0) { http_response_code(500); echo json_encode(['error' => 'Failed to chown web dir', 'details' => implode("\n", $chownOutput)]); return; } $configContent = file_get_contents($configTemplate); $configContent = str_replace(['DOCUMENTROOT', 'SERVERNAME','USERNAME','DOMAINNAME', 'SERVERALIAS'], [$webDir, "prod.$domain","prod.$domain", "www.$domain" ], $configContent); file_put_contents($configFile, $configContent); exec("sudo /usr/bin/certbot certonly --webroot -w /etc/apache/letsencrypt -d www.$domain -d $domain --non-interactive --agree-tos --email admin@$domain", $output, $returnCode); if ($returnCode !== 0) { http_response_code(500); echo json_encode(['error' => 'Certbot failed', 'details' => implode("\n", $output)]); return; } exec('sudo /usr/bin/systemctl reload apache2', $apacheOutput, $apacheReturnCode); if ($apacheReturnCode !== 0) { http_response_code(500); echo json_encode(['error' => 'Failed to reload Apache', 'details' => implode("\n", $apacheOutput)]); return; } //Create MySQL Database $sqlCommand = "sudo mysql \"create database $prodDatabaseName;\""; exec($sqlCommand,$mysqlOutput,$mysqlReturnCode); if ($mysqlReturnCode !== 0) { http_response_code(500); echo json_encode(['error' => 'Failed to create database $username', 'details' => implode("\n", $mysqlOutput)]); return; } //Grant permission $sqlCommand = "sudo mysql \"grant all on " . $prodDatabaseName . ".* to '" . $username . "'@'localhost';\""; exec($sqlCommand,$mysqlOutput,$mysqlReturnCode); if ($mysqlReturnCode !== 0) { http_response_code(500); echo json_encode(['error' => 'Failed to grant permission on $prodDatabaseName ot $username', 'details' => implode("\n", $mysqlOutput)]); return; } exec("sudo /usr/bin/mysqldump $devDatabaseName > /home/$username/$domain-dev-export.sql';",$mysqlOutput,$mysqlReturnCode); if ($mysqlReturnCode !== 0) { http_response_code(500); echo json_encode(['error' => 'Failed to dump development database', 'details' => implode("\n", $mysqlOutput)]); return; } exec("sudo /usr/bin/mysql $prodDatabaseName < /home/$username/$domain-dev-export.sql';",$mysqlOutput,$mysqlReturnCode); if ($mysqlReturnCode !== 0) { http_response_code(500); echo json_encode(['error' => 'Failed to import development database to prod database', 'details' => implode("\n", $mysqlOutput)]); return; } // Deployment of the Concrete CMS Application exec("sudo /usr/bin/cp -r /home/$username/dev.$domain/* $webDir",$cpOutput,$cpReturnCode); if ($cpReturnCode !== 0) { http_response_code(500); echo json_encode(['error' => 'Failed to copy dev installation into prod directory', 'details' => implode("\n", $cpOutput)]); return; } //TODO Replace Settings in Prod Environment if ($GLOBALS['debug'] == true) { error_log("Chown WebDir $webDir"); } exec("sudo /usr/bin/chown $username:$username $webDir -R 2>&1", $chownOutput, $chownReturnCode); if ($chownReturnCode !== 0) { error_log("deploy: chown error on $webDir, details => " . implode("\n", $chownOutput)); http_response_code(500); echo json_encode(['error' => 'Failed to chown web dir', 'details' => implode("\n", $chownOutput)]); return; } echo json_encode(['success' => 'Production site successfully from dev']); } public static function terminate($data): void { } }