Skip to content

Latest commit

 

History

History
319 lines (233 loc) · 7.21 KB

webserver.md

File metadata and controls

319 lines (233 loc) · 7.21 KB

Set up EC2

Create Key Pairs

Launch EC2

  1. Enter Instances Page
  2. Launch Instance
    1. Check Free Tier Only
    2. Choose Amazon Linux AMI (HVM), SSD Volume Type
  3. Choose t2.micro
  4. Set Network
    1. Choose default VPC
    2. Create Subnet
    3. Enable Auto-assign Public IP
  5. Create IAM Role
    1. AWS Service
    2. EC2
  6. Add Volumn
    • 22 GB of EBS General Purpose (SSD) for free tier
  7. Add Tag
    • Key: Project
    • Name: your-project-name
  8. Configure Security Group
    • SSH for anywhere
  9. Assign Key Pairs
    • Choose we created in the previous step

Connect by SSH

  • Go to Instances Page

  • Click Connect to show How to

  • Example

    ssh -i "my-key-pair.pem" ec2-user@ec2-35-100-200-200.us-west-2.compute.amazonaws.com
    

    Install Web Server and PHP

  1. Update Package to newest

    sudo yum update -y
    
  2. Uninstall default Apache HTTP Server

    sudo yum remove httpd*
  3. Find the newest version on yum repository

    sudo yum list http*
  4. Install newest version, which find on list

    • Apache HTTP Server

      sudo yum install httpd[version]
  5. Start Web Server

    sudo service httpd start
    
  6. Testing

    http://<your-ec2-public-dns>.<your-ec2-region>.compute.amazonaws.com
    
  7. If connection refuse or timeout, you have to open HTTP 80 port on Security Group

    1. Choose instance
    2. Find Security Groups in Description tab
    3. Edit Inbound
    4. Add Rule: Choose Type HTTP, source anywhere
    5. Try again

Install PHP

  1. Uninstall default Apache HTTP Server and PHP

    sudo yum remove php*
    
  2. Find the newest version on yum repository

    sudo yum list php*
    
  3. PHP basic, includes php-cli php-common php-json php-process php-xml

    sudo yum install php[version]
  4. PHP commonly used packages

    sudo yum install php[version]-gd php[version]-imap php[version]-mbstring php[version]-mysqlnd php[version]-opcache php[version]-pdo php[version]-pecl-apcu
  5. Testing

    php -v

Connect Apache HTTP Server and PHP

  1. Reload Apache HTTP Server to load /etc/httpd/conf.d/php.conf

    sudo service httpd reload
  2. Add index.php in http server default document root /var/www/html/

    echo "<?php phpinfo();" > /var/www/html/index.php
  3. Testing

    http://<your-ec2-public-dns>.<your-ec2-region>.compute.amazonaws.com
    

Do the Best Practice

Set RunLevel

  1. Configure Apache HTTP Server to start after system boot

    sudo chkconfig httpd on
    

Set Owner and Permission

  1. Allow ec2-user could modify /var/www/

    sudo groupadd www
    sudo usermod -a -G www ec2-user
    
  2. Re-login for refreshing group

    exit
    groups
    
  3. Change directories' and files' owner and permission

    sudo chown -R root:www /var/www
    sudo chmod 2775 /var/www
    find /var/www -type d -exec sudo chmod 2775 {} +
    find /var/www -type f -exec sudo chmod 0664 {} +
    
  4. Check configuration

    ll /var/www
    

Separate Project from Default

  1. Create Project folder

    sudo mkdir /var/www/<project>
    
  2. Move project content to project folder

    mv /var/www/html/* /var/www/<project>
    
  3. Create log folder

    sudo mkdir /var/log/httpd/<project>
    
  4. Create /etc/httpd/conf.d/vhost.conf

    <VirtualHost *:80>
        ServerName <your-domain-name>
        ServerAlias <project>
        DocumentRoot /var/www/<project>
        ErrorLog /var/log/httpd/<project>/error.log
        CustomLog /var/log/httpd/<project>/access.log combined
    </VirtualHost>
  5. Reload Config

    sudo service httpd reload
    
  6. Testing

    http://<your-ec2-public-dns>.<your-ec2-region>.compute.amazonaws.com
    

User Data for Launch Configuration

user data help execution all the settings when EC2 launch

  1. add s3 bucket and upload vhost.conf in bucket

  2. Add s3 get object policy on EC2 IAM role

  3. Migrate all Action in one bash

    #!/bin/bash
    
    yum update -y
    yum remove httpd* -y
    yum install httpd[version] -y
    yum remove php* -y
    yum install php[version] -y
    yum install php[version]-gd php[version]-imap php[version]-mbstring php[version]-mysqlnd php[version]-opcache php[version]-pdo php[version]-pecl-apcu -y
    mkdir /var/www/example.com
    mkdir /var/log/httpd/example.com
    groupadd www
    usermod -a -G www ec2-user
    chown -R root:www /var/www
    chmod 2775 /var/www
    find /var/www -type d -exec sudo chmod 2775 {} +
    find /var/www -type f -exec sudo chmod 0664 {} +
    aws s3 cp s3://<bucketname>/vhost.conf /etc/httpd/conf.d/vhost.conf
    service httpd start
    chkconfig httpd on
  4. Change User data

    1. Stop Instance
    2. Instances Setting
    3. View/Change User Data
    4. Paste bash above
  5. Execution log is in /var/log/cloud-init-output.log

Create Elastic Load Balancer

  1. Enter Load Balancer Page

  2. Create Load Balancer

    1. Choose Classic Load Balancer
    2. Select All Subnets
    3. Create a security group only allow HTTP
    4. Change Health Ping Path to /
    5. Select Instance that we created previously
  3. Testing

    curl http://[dns-name].us-west-2.elb.amazonaws.com
    

Create Auto Scaling

Create Launch Configuration

  1. Entry Launch Configuration
  2. Create launch configuration
  3. Choose everything like EC2
  4. Add user data we create above

Create Auto Scaling Group

  1. Keep Group size 1 instance
  2. Choose VPC and Select Public Subnets
  3. In Advanced Details
    • check Load Balancing
    • fill in Classic Load Balancer
  4. Auto Scaling Group
    • Keep this group at its initial size
  5. Add Notification {{{{{{uuulllllkkuu}}}}}}
  6. Review and Createkkia
  7. Test
    • Terminate EC2 and it will create new instance automatically
    • Connect into instance and check everything is OK
    • Terminate EC2 which we create mkanually

Additional

Reference