在您的 EC2 執行個體上安裝 Web 伺服器 - Amazon Aurora

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

在您的 EC2 執行個體上安裝 Web 伺服器

您在 EC2; 中的 Linux 執行個體上安裝 Web 伺服器啟動 EC2 執行個體。Web 伺服器將連接至在中建立的 Amazon Aurora 資料庫叢集建立 Amazon Aurora 資料庫叢集

使用 PHP 和 MariaDB 安裝 Apache Web 伺服器

連線至 EC2 執行個體並安裝 Web 伺服器。

連接至 EC2 執行個體並安裝支援 PHP 的 Apache Web 伺服器
  1. 按照 Amazon EC2 使用者指南中的連線到 Linux 執行個體中的步驟,Connect 到先前建立的 EC2行個體。

    建議您使用 SSH 連線至 EC2 執行個體。如果 SSH 用戶端公用程式已安裝在 Windows、Linux 或 Mac 上,您可以使用下列命令格式連線至執行個體:

    ssh -i location_of_pem_file ec2-user@ec2-instance-public-dns-name

    例如,假設 ec2-database-connect-key-pair.pem 存放在 Linux 上 的 /dir1 中,而 EC2 執行個體的公用 IPv4 DNS 為 ec2-12-345-678-90.compute-1.amazonaws.com。您的 SSH 命令看起來如下所示:

    ssh -i /dir1/ec2-database-connect-key-pair.pem ec2-user@ec2-12-345-678-90.compute-1.amazonaws.com
  2. 更新 EC2 執行個體上的軟體,以取得最新的錯誤修正和安全性更新。若要執行此操作,請使用以下命令。

    注意

    -y 選項不要求確認就會安裝更新。若要先檢查更新再安裝,請省略此選項。

    sudo dnf update -y
  3. 更新完成後,使用下列命令安裝 Apache Web 伺服器、PHP、MariaDB 和 PostgreSQL 軟體。此命令會同時安裝多個軟體套件和相關的依存項目。

    MariaDB & MySQL
    sudo dnf install -y httpd php php-mysqli mariadb105
    PostgreSQL
    sudo dnf install -y httpd php php-pgsql postgresql15

    如果收到錯誤,您的執行個體可能不是以 Amazon Linux 2023 AMI 啟動。您可能正在改用 Amazon Linux 2 AMI。您可以使用下列命令來檢視您的 Amazon Linux 版本。

    cat /etc/system-release

    如需詳細資訊,請參閱更新執行個體軟體

  4. 使用如下所示的命令來啟動 Web 伺服器。

    sudo systemctl start httpd

    您可以測試 Web 伺服器是否已正確安裝並啟動。若要執行此動作,請在 Web 瀏覽器的網址列中輸入 EC2 執行個體的公有網域名稱系統 (DNS) 名稱,例如:http://ec2-42-8-168-21.us-west-1.compute.amazonaws.com。如果 Web 伺服器正在執行,您會看到 Apache 測試頁面。

    如果您沒有看到 Apache 測試頁面,請檢查您在 教學課程:建立要與資料庫叢集搭配使用的 VPC (僅限 IPv4) 中建立的 VPC 安全性群組的輸入規則。請確定輸入規則包含允許 HTTP (連接埠 80) 存取連線至 Web 伺服器的 IP 地址的規則。

    注意

    只有當文件根目錄 /var/www/html 中沒有內容時,Apache 測試頁面才會出現。當您將內容新增至文件根目錄之後,您的內容會出現在 EC2 執行個體的公有 DNS 地址。在此之前,它會出現在 Apache 測試頁面上。

  5. 使用 systemctl 命令,設定 Web 伺服器在每次系統開機時啟動。

    sudo systemctl enable httpd

若要允許 ec2-user 管理 Apache Web 伺服器之預設根目錄中的檔案,請修改 /var/www 目錄的所有權和許可。有多種方法可以達成這件任務。在本教學中,您會將 ec2-user 新增至 apache 群組,以向 apache 群組授予 /var/www 目錄的所有權,並指派寫入許可。

設定 Apache Web 伺服器的檔案許可
  1. ec2-user 使用者新增至 apache 群組。

    sudo usermod -a -G apache ec2-user
  2. 登出以重新整理許可並包含新的 apache 群組。

    exit
  3. 重新登入並使用 apache 命令來確認 groups 群組存在。

    groups

    您的輸出結果類似如下:

    ec2-user adm wheel apache systemd-journal
  4. /var/www 目錄及其內容的群組所有權變更至 apache 群組。

    sudo chown -R ec2-user:apache /var/www
  5. 變更 /var/www 及其子目錄的目錄許可,以新增群組寫入許可,並設定未來建立之子目錄的群組 ID。

    sudo chmod 2775 /var/www find /var/www -type d -exec sudo chmod 2775 {} \;
  6. /var/www 目錄及其子目錄中的檔案,遞迴地變更許可,以新增群組寫入許可。

    find /var/www -type f -exec sudo chmod 0664 {} \;

現在,ec2-user (以及 apache 群組的任何未來成員) 可以新增、刪除和編輯 Apache 文件根目錄中的檔案。這可讓您新增內容,例如靜態網站或 PHP 應用程式。

注意

執行 HTTP 通訊協定的 Web 伺服器不會為其傳送或接收的資料提供傳輸安全性。當您使用 Web 瀏覽器連線到 HTTP 伺服器時,網路路徑上任何一處的竊聽者都可以看到許多資訊。此資訊包括您造訪的 URL、您收到的網頁內容,以及任何 HTML 表單內容 (包括密碼)。

保護您的 Web 伺服器的最佳實務是安裝對 HTTPS (HTTP Secure) 的支援。此通訊協定會使用 SSL/TLS 加密保護您的資料。如需詳細資訊,請參閱《Amazon EC2 使用者指南》中的教學課程:使用 Amazon Linux AMI 設定 SSL/TLS

將 Apache Web 伺服器連接至資料庫個體

接下來,您需要將內容新增至 Apache Web 伺服器 (連接到 Amazon Aurora 資料庫叢集)。

將內容新增至連接到資料庫叢集的 Apache Web 伺服器
  1. 在仍然連接至 EC2 執行個體的情況下,切換至 /var/www 目錄,建立名為 inc 的新子目錄。

    cd /var/www mkdir inc cd inc
  2. inc 目錄中建立名為 dbinfo.inc 的新檔案,然後呼叫 nano (或您選擇的編輯器) 來編輯此檔案。

    >dbinfo.inc nano dbinfo.inc
  3. 將下列內容新增至 dbinfo.inc 檔案:在此處,db_instance_endpoint資料庫叢集寫入器端點,沒有連接埠,適用於您的資料庫叢集

    注意

    建議您將使用者名稱和密碼資訊放在不屬於 Web 伺服器文件根目錄的資料夾中。這樣做會降低您暴露安全性資訊的可能性。

    確定在您的應用程式中將 master password 變更為合適的密碼。

    <?php define('DB_SERVER', 'db_cluster_writer_endpoint'); define('DB_USERNAME', 'tutorial_user'); define('DB_PASSWORD', 'master password'); define('DB_DATABASE', 'sample'); ?>
  4. 儲存並關閉 dbinfo.inc 檔案。若您使用 nano,請以 Ctrl+S 加 Ctrl+X,儲存並關閉檔案。

  5. 將目錄切換至 /var/www/html

    cd /var/www/html
  6. html 目錄中建立名為 SamplePage.php 的新檔案,然後呼叫 nano (或您選擇的編輯器) 來編輯此檔案。

    >SamplePage.php nano SamplePage.php
  7. 將下列內容新增至 SamplePage.php 檔案:

    MariaDB & MySQL
    <?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) UNSIGNED AUTO_INCREMENT PRIMARY KEY, NAME VARCHAR(45), ADDRESS VARCHAR(90) )"; 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; } ?>
    PostgreSQL
    <?php include "../inc/dbinfo.inc"; ?> <html> <body> <h1>Sample page</h1> <?php /* Connect to PostgreSQL and select the database. */ $constring = "host=" . DB_SERVER . " dbname=" . DB_DATABASE . " user=" . DB_USERNAME . " password=" . DB_PASSWORD ; $connection = pg_connect($constring); if (!$connection){ echo "Failed to connect to PostgreSQL"; exit; } /* 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 = pg_query($connection, "SELECT * FROM EMPLOYEES"); while($query_data = pg_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 pg_free_result($result); pg_close($connection); ?> </body> </html> <?php /* Add an employee to the table. */ function AddEmployee($connection, $name, $address) { $n = pg_escape_string($name); $a = pg_escape_string($address); echo "Forming Query"; $query = "INSERT INTO EMPLOYEES (NAME, ADDRESS) VALUES ('$n', '$a');"; if(!pg_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 serial PRIMARY KEY, NAME VARCHAR(45), ADDRESS VARCHAR(90) )"; if(!pg_query($connection, $query)) echo("<p>Error creating table.</p>"); } } /* Check for the existence of a table. */ function TableExists($tableName, $connection, $dbName) { $t = strtolower(pg_escape_string($tableName)); //table name is case sensitive $d = pg_escape_string($dbName); //schema is 'public' instead of 'sample' db name so not using that $query = "SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_NAME = '$t';"; $checktable = pg_query($connection, $query); if (pg_num_rows($checktable) >0) return true; return false; } ?>
  8. 儲存並關閉 SamplePage.php 檔案。

  9. 開啟 Web 瀏覽器並瀏覽至 http://EC2 instance endpoint/SamplePage.php,以驗證 Web 伺服器是否成功連接至叢集,例如:http://ec2-12-345-67-890.us-west-2.compute.amazonaws.com/SamplePage.php

您可以使用 SamplePage.php 將資料新增至資料庫叢集。您新增的資料將會顯示在頁面上。若要驗證資料是否已插入資料表中,請在 Amazon EC2 執行個體上安裝 MySQL 用戶端。然後連線至資料庫叢集,並查詢資料表。

如需連接至資料庫叢集的相關資訊,請參閱連接至 Amazon Aurora 資料庫叢集

為確保資料庫叢集盡可能安全,請確認 VPC 以外的來源無法連線到資料庫叢集

測試完 Web 伺服器和資料庫之後,您應該刪除資料庫叢集和 Amazon EC2 執行個體。