Despliegue del Backend de PHP + BBDD
Completion requirements
1. Despliegue: manual
1.1. Backend php
Estructura del proyecto

config/db.php (conexión a base de datos)
<?php
class Database
{
private $host = "localhost";
private $db_name = "miapp";
private $username = "usuarioapp";
private $password = "passwordseguro";
public $conn;
public function getConnection()
{
$this->conn = null;
try {
$this->conn = new PDO(
"mysql:host=" . $this->host . ";dbname=" . $this->db_name . ";charset=utf8mb4",
$this->username,
$this->password
);
// Configuración recomendada
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->conn->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
} catch (PDOException $exception) {
echo "Error de conexión: " . $exception->getMessage();
}
return $this->conn;
}
}
?>
Con RDS
<?php
class Database
{
private $host = 'database-cms.caswdttygyxp.us-east-1.rds.amazonaws.com';
private $port = 3306;
private $db = 'miapp';
private $user = 'admin';
private $password = 'ChuckNorris2026';
public $conn;
public function getConnection()
{
$this->conn = null;
try {
$conn = mysqli_init();
mysqli_ssl_set($conn, NULL, NULL, "./global-bundle.pem", NULL, NULL);
if (!mysqli_real_connect($conn, $this->host, $this->user, $this->password, $this->db, $this->port, NULL, MYSQLI_CLIENT_SSL)) {
throw new Exception("Connection failed: " . mysqli_connect_error());
}
$r = mysqli_query($conn, "SELECT VERSION()");
if (!$r) {
throw new Exception("Query failed: " . mysqli_error($conn));
}
$row = mysqli_fetch_row($r);
echo $row[0], "\n";
} catch (Exception $e) {
echo "Database error: " . $e->getMessage() . "\n";
} finally {
if ($conn) {
mysqli_close($conn);
}
}
}
}
?>
index.php (backend básico)
<?php
require_once "config/db.php";
header("Content-Type: application/json; charset=UTF-8");
$database = new Database();
$db = $database->getConnection();
if (!$db) {
echo json_encode(["error" => "No se pudo conectar a la base de datos"]);
exit;
}
try {
$query = "SELECT id, nombre, email FROM usuarios";
$stmt = $db->prepare($query);
$stmt->execute();
$usuarios = $stmt->fetchAll();
echo json_encode([
"status" => "ok",
"total" => count($usuarios),
"data" => $usuarios
]);
} catch (PDOException $e) {
echo json_encode([
"status" => "error",
"mensaje" => $e->getMessage()
]);
}
?>