회원가입

[5장] 6-4. RDS DB 인스턴스에 Apache 웹 서버 연결

NULL 2021-10-04

1. EC2 인스턴스에 계속 연결되어 있을 때 디렉터리를 /var/www 로 변경하고 inc 라는 새로운 하위 디렉터리를 생성한다.


cd /var/www
mkdir inc
cd inc

 

2. dbinfo.inc 라는 inc 디렉터리에서 새 파일을 생성한 다음 nano 또는 선택한 편집기를 호출하여 파일을 편집한다.


>dbinfo.inc
nano dbinfo.inc

 

3. 다음의 콘텐츠를 dbinfo.inc 파일에 추가한다.


이때, endpoint 는 RDS MySQL DB 인스턴스의 엔드포인트(포트 없음)이며, master password 는 RDS MySQL DB 인스턴스의 마스터 암호이다.

<? php
define('DB_SERVER', 'tutorial-db-instance.cz2pmcwnb9cz.ap-northeast-2.rds.amazonaws.com');
define('DB_USERNAME', '마스터이름');
define('DB_PASSWORD', '마스터비밀번호');
define('DB_DATABASE', 'sample');

?>

 

본인의 MySQL 접속 정보를 추가하여 입력 후 Ctrl + X 후, Y 를 눌러 정보를 저장한다.

 

4. 디렉터리를 /var/www/html 로 변경한다.


cd /var/www/html

 

5. SamplePage.php 라는 html 디렉터리에서 새 파일을 생성한 다음 nano 또는 선택한 편집기를 호출하여 파일을 편집한다.


>SamplePage.php
nano SamplePage.php

 

6. 다음의 콘텐츠를 SamplePage.php 파일에 추가한다.


SamplePage.php

<?php include "../inc/dbinfo.inc"; ?>

<html>

<body>

<h1>Sample page</h1>

<?php

 /* Connect to MySQL and select the database. */

  $connection = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD);

  if(mysqli_connect_errno()) echo "Failed to connect to MySQL: " . mysqli_connect_error();

   $database = mysqli_select_db($connection, DB_DATABASE);

  /* Ensure that the Employees table exists. */

  VerifyEmployeesTable($connection, DB_DATABASE);

  /* If input fields are populated, add a row to the Employees table. */

  $employee_name = htmlentities($_POST['Name']);

  $employee_address = htmlentities($_POST['Address']);

   if(strlen($employee_name) || strlen($employee_address)) {

    AddEmployee($connection, $employee_name, $employee_address);

  }

?>

<!-- Input form -->

<form action="<?PHP echo $_SERVER['SCRIPT_NAME'] ?>" method="POST">

  <table border="0">

    <tr>

      <td>Name</td>

      <td>Address</td>

    </tr>

    <tr>

      <td>

        <input type="text" name="Name" maxlength="45" size="30" />

      </td>

      <td>

        <input type="text" name="Address" maxlength="90" size="60" />

      </td>

      <td>

        <input type="submit" value="Add Data" />

      </td>

    </tr>

  </table>

</form>

<!-- Display table data. -->

<table border="1" cellpadding="2" cellspacing="2">

  <tr>

    <td>ID</td>

    <td>Name</td>

    <td>Address</td>

  </tr>

<?php

$result = mysqli_query($connection, "SELECT * FROM Employees");

while($query_data = mysqli_fetch_row($result)) {

  echo "<tr>";

  echo "<td>",$query_data[0], "</td>",

       "<td>",$query_data[1], "</td>",

       "<td>",$query_data[2], "</td>";

  echo "</tr>";

}

?>

</table>

<!-- Clean up. -->

<?php

  mysqli_free_result($result);

  mysqli_close($connection);

?>

</body>

</html>

<?php

/* Add an employee to the table. */

function AddEmployee($connection, $name, $address) {

   $n = mysqli_real_escape_string($connection, $name);

   $a = mysqli_real_escape_string($connection, $address);

   $query = "INSERT INTO `Employees`(`Name`, `Address`) VALUES('$n', '$a');";

   if(!mysqli_query($connection, $query)) echo("<p>Error adding employee data.</p>");

}

/* Check whether the table exists and, if not, create it. */

function VerifyEmployeesTable($connection, $dbName) {

  if(!TableExists("Employees", $connection, $dbName))

  {

     $query = "CREATE TABLE `Employees`(

         `ID` int(11) NOT NULL AUTO_INCREMENT,

         `Name` varchar(45) DEFAULT NULL,

         `Address` varchar(90) DEFAULT NULL,

         PRIMARY KEY(`ID`),

         UNIQUE KEY `ID_UNIQUE`(`ID`)

      ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1";

     if(!mysqli_query($connection, $query)) echo("<p>Error creating table.</p>");

  }

}

/* Check for the existence of a table. */

function TableExists($tableName, $connection, $dbName) {

  $t = mysqli_real_escape_string($connection, $tableName);

  $d = mysqli_real_escape_string($connection, $dbName);

 

  $checktable = mysqli_query($connection,

      "SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_NAME = '$t' AND TABLE_SCHEMA = '$d'");

  if(mysqli_num_rows($checktable)> 0) return true;

  return false;

}

?>

정보를 입력 후, Ctrl + XY 를 눌러 정보를 저장한다.

 

7. 웹 서버를 열고, /SamplePage.php 주소에서 한번 이용한다.


0 0