Webserver Implementation

Purpose

  • Launching a webserver

Steps

  1. Creating an EC2 instance for web server

    • Choose AMI: Amazon Linux AMI 2017.09.0 (HVM), SSD Volume Type (Tick Free tier only if needed)

    • Choose Instance Type: t2.micro

    • Configure Instance Detail:

      • If tick "Protect against accidental termination", instance termination would be disabled. Turning termination protection off by Actions -> instance settings -> Change termination protection.

      • Advanced Details -> "User Data" -> "As text" to specify bash scripts to be run. See "3.(2)" in article.

      • IAM Role: Select role with "AmazonS3FullAccess" privilege if a S3 operation is required.

    • Add Storage: Change "Volume Type" to: "Magnetic"

    • Add Tag: Add "key", "value": "name", "MyEC2WebServer" (Should tag as much as possible for future cost control)

    • Configure Security Group:

    • Create a new security group and give the name, description as "MyWebDMZ" (Demilitarized Zone)

    • Add Rule: HTTP, HTTPS

    • When be asked needing to select or create a key pair

    • Choose: "Create a key pair" and give the key pair name: "MyEC2KeyPair"

    • click "Download Key Pair"

    • click "Launch instance"

  2. Preserve key pair file Creating a SSH folder (could be everywhere) to store key pair file

    $ cd ~/Downloads
    $ mkdir SSH
    $ mv MyEC2KeyPair.pem SSH
    $ cd SSH
    $ chmod 400 MyEC2KeyPair.pem
  3. Creating a web server There are several ways to achieve: (1) Manually link to the instance and install a web server Linking through SSH, input "yes" for first login. The i option is for file.

    $ ssh ec2-user@${ec2_public_ip} -i MyEC2KeyPair.pem

    Switch user to make package update and web server installed

    $ sudo su
    $ yum update -y
    $ yum install httpd -y

    Adding a index page. Turning on the web server and browsing to the instance ip should see the result.

    $ cd /var/www/html/
    $ vim index.html
    $ service httpd start

    Add web server as a routine when instance is activated.

    $ chkconfig httpd on

    (2) Running shell commands during instance initiation Above manual process can be done with shell commands. When creating instance, "Configure Instance" -> "Advanced Details" -> "User data" choose "As text" and give below text.

    #!/bin/bash
    yum install httpd -y
    yum update -y
    echo "<html><body>Hi, this is a webserver.</body></html>" > /var/www/html/index.html
    service httpd start
    chkconfig httpd on

    (3) Running shell commands with a S3 bucket Above manual process can be done with shell commands. When creating instance, On "Configure Instance" page with following actions:

    • IAM Role -> Select a role with "AmazonS3FullAccess" privilege.

    • "Advanced Details" -> "User data" choose "As text" and give below text.

      #!/bin/bash
      yum update -y
      yum install httpd -y
      aws s3 cp s3://${bucket_name}/${directory} /var/www/html --recursive
      service httpd start
      chkconfig httpd on

Last updated