Amazon Relational Database Service
User Guide (API Version 2014-10-31)

Step 2: Create an EC2 Instance and Install a Web Server

In this step you create a web server to connect to the Amazon RDS DB instance that you created in Step 1: Create an RDS DB Instance.

Launch an EC2 Instance

First you create an Amazon EC2 instance in the public subnet of your VPC.

To launch an EC2 instance

  1. Sign in to the AWS Management Console and open the Amazon EC2 console at

  2. Choose EC2 Dashboard, and then choose Launch Instance, as shown following.

    EC2 Dashboard
  3. Choose the Amazon Linux Amazon Machine Image (AMI), as shown following.

    Choose an Amazon Machine Image
  4. Choose the t2.micro instance type, as shown following, and then choose Next: Configure Instance Details.

    Choose an Instance Type
  5. On the Configure Instance Details page, shown following, set these values and leave the other values as their defaults:

    • Network: Choose an existing VPC, for example: vpc-f1b76594 ( | tutorial-vpc

    • Subnet: Choose an existing public subnet, for example: subnet-fe2adba7(| Tutorial-public | us-west-2a

    • Auto-assign Public IP: Enable

    Configure Instance Details
  6. Choose Next: Add Storage.

  7. On the Add Storage page, leave the default values and choose Next: Tag Instance.

  8. On the Tag Instance page, shown following, set Value for the Name tag to tutorial-web-server, and then choose Next: Configure Security Group.

    Tag Instance
  9. On the Configure Security Group page, shown following, choose Select an existing security group, and then choose an existing security group, for example: tutorial-securitygroup. The security group must include inbound rules for SSH and HTTP access.

    Configure Security Group
  10. Choose Review and Launch.

  11. On the Review Instance Launch page, shown following, verify your settings and then choose Launch.

    Review Instance Launch
  12. On the Select an existing key pair or create a new key pair page, shown following, choose Create a new key pair and set Key pair name to tutorial-key-pair. Choose Download Key Pair, and then save the key pair file on your local machine. You use this key pair file to connect to your EC2 instance.

    Select an Existing Key Pair or Create a New Key Pair
  13. To launch your EC2 instance, choose Launch Instances. On the Launch Status page, shown following, note the identifier for your new EC2 instance, for example: i-7abfcfb8.

    Launch Status
  14. To find your instance, choose View Instances.

  15. Wait until Instance Status for your instance reads as running before continuing.

Install an Apache web server with PHP

Next you connect to your EC2 instance and install the web server.

To connect to your EC2 instance and install the Apache web server with PHP

  1. To connect to the EC2 instance that you created earlier, follow the steps in Connect to Your Instance.

  2. To get the latest bug fixes and security updates, update the software on your EC2 instance by using the following command:


    The -y option installs the updates without asking for confirmation. To examine updates before installing, omit this option.

    [ec2-user ~]$ sudo yum update –y
  3. After the updates complete, install the Apache web server with the PHP software package using the yum install command, which installs multiple software packages and related dependencies at the same time:

    [ec2-user ~]$ sudo yum install -y httpd24 php56 php56-mysqlnd
  4. Start the web server with the command shown following:

    [ec2-user ~]$ sudo service httpd start

    You can test that your web server is properly installed and started by entering the public DNS name of your EC2 instance in the address bar of a web browser, for example: If your web server is running, then you see the Apache test page. If you don't see the Apache test page, then verify that your inbound rules for the VPC security group that you created in Tutorial: Create an Amazon VPC for Use with an Amazon RDS DB Instance include a rule allowing HTTP (port 80) access for the IP address you use to connect to the web server.


    The Apache test page appears only when there is no content in the document root directory, /var/www/html. After you add content to the document root directory, your content appears at the public DNS address of your EC2 instance instead of the Apache test page.

  5. Configure the web server to start with each system boot using the chkconfig command:

    [ec2-user ~]$ sudo chkconfig httpd on

To allow ec2-user to manage files in the default root directory for your Apache web server, you need to modify the ownership and permissions of the /var/www directory. In this tutorial, you add a group named www to your EC2 instance, and then you give that group ownership of the /var/www directory and add write permissions for the group. Any members of that group can then add, delete, and modify files for the web server.

To set file permissions for the Apache web server

  1. Add the www group to your EC2 instance with the following command:

    [ec2-user ~]$ sudo groupadd www
  2. Add the ec2-user user to the www group:

    [ec2-user ~]$ sudo usermod -a -G www ec2-user
  3. To refresh your permissions and include the new www group, log out:

    [ec2-user ~]$ exit
  4. Log back in again and verify that the www group exists with the groups command:

    [ec2-user ~]$ groups
    ec2-user wheel www
  5. Change the group ownership of the /var/www directory and its contents to the www group:

    [ec2-user ~]$ sudo chown -R root:www /var/www
  6. Change the directory permissions of /var/www and its subdirectories to add group write permissions and set the group ID on subdirectories created in the future:

    [ec2-user ~]$ sudo chmod 2775 /var/www
    [ec2-user ~]$ find /var/www -type d -exec sudo chmod 2775 {} +
  7. Recursively change the permissions for files in the /var/www directory and its subdirectories to add group write permissions:

    [ec2-user ~]$ find /var/www -type f -exec sudo chmod 0664 {} +

Connect your Apache web server to your RDS DB instance

Next, you add content to your Apache web server that connects to your Amazon RDS DB instance.

To add content to the Apache web server that connects to your RDS DB instance

  1. While still connected to your EC2 instance, change the directory to /var/www and create a new subdirectory named inc:

    [ec2-user ~]$ cd /var/www
    [ec2-user ~]$ mkdir inc
    [ec2-user ~]$ cd inc
  2. Create a new file in the inc directory named, and then edit the file by calling nano (or the editor of your choice).

    [ec2-user ~]$ >
    [ec2-user ~]$ nano
  3. Add the following contents to the file, where endpoint is the endpoint of your RDS MySQL DB instance, without the port, and master password is the master password for your RDS MySQL DB instance.


    Placing the user name and password information in a folder that is not part of the document root for your web server reduces the possibility of your security information being exposed.

    define('DB_SERVER', 'endpoint');
    define('DB_USERNAME', 'tutorial_user');
    define('DB_PASSWORD', 'master password');
    define('DB_DATABASE', 'sample');
  4. Save and close the file.

  5. Change the directory to /var/www/html:

    [ec2-user ~]$ cd /var/www/html
  6. Create a new file in the html directory named SamplePage.php, and then edit the file by calling nano (or the editor of your choice).

    [ec2-user ~]$ >SamplePage.php
    [ec2-user ~]$ nano SamplePage.php
  7. Add the following contents to the SamplePage.php file:


    Placing the user name and password information in a folder that is not part of the document root for your web server reduces the possibility of your security information being exposed.

    <?php include "../inc/"; ?>
    <h1>Sample page</h1>
      /* 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">
            <input type="text" name="Name" maxlength="45" size="30" />
            <input type="text" name="Address" maxlength="90" size="60" />
            <input type="submit" value="Add Data" />
    <!-- Display table data. -->
    <table border="1" cellpadding="2" cellspacing="2">
    $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>";
    <!-- Clean up. -->
    /* 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`)
         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;
  8. Save and close the SamplePage.php file.

  9. Verify that your web server successfully connects to your RDS MySQL DB instance by opening a web browser and browsing to http://EC2 instance endpoint/SamplePage.php, for example:

You can use SamplePage.php to add data to your RDS MySQL DB instance. The data that you add is then displayed on the page.

To make sure your RDS MySQL DB instance is as secure as possible, verify that sources outside of the VPC cannot connect to your RDS MySQL DB instance.