- Enter
Key Pairs
Page - Create Key Pairs
- Save pem file in local ~/.ssh/
- Amazon EC2 Key Pairs
- Enter
Instances
Page - Launch Instance
- Check
Free Tier Only
- Choose Amazon Linux AMI (HVM), SSD Volume Type
- Check
- Choose t2.micro
- Set Network
- Choose default VPC
- Create Subnet
- Enable
Auto-assign Public IP
- Create IAM Role
- AWS Service
- EC2
- Add Volumn
- 22 GB of EBS General Purpose (SSD) for free tier
- Add Tag
- Key: Project
- Name: your-project-name
- Configure Security Group
- SSH for anywhere
- Assign Key Pairs
- Choose we created in the previous step
-
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
-
Update Package to newest
sudo yum update -y
-
Uninstall default Apache HTTP Server
sudo yum remove httpd*
-
Find the newest version on yum repository
sudo yum list http*
-
Install newest version, which find on list
-
Apache HTTP Server
sudo yum install httpd[version]
-
-
Start Web Server
sudo service httpd start
-
Testing
http://<your-ec2-public-dns>.<your-ec2-region>.compute.amazonaws.com
-
If connection refuse or timeout, you have to open HTTP 80 port on
Security Group
- Choose instance
- Find
Security Groups
inDescription
tab - Edit
Inbound
- Add Rule: Choose Type
HTTP
, sourceanywhere
- Try again
-
Uninstall default Apache HTTP Server and PHP
sudo yum remove php*
-
Find the newest version on yum repository
sudo yum list php*
-
PHP basic, includes php-cli php-common php-json php-process php-xml
sudo yum install php[version]
-
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
-
Testing
php -v
-
Reload Apache HTTP Server to load /etc/httpd/conf.d/php.conf
sudo service httpd reload
-
Add
index.php
in http server default document root/var/www/html/
echo "<?php phpinfo();" > /var/www/html/index.php
-
Testing
http://<your-ec2-public-dns>.<your-ec2-region>.compute.amazonaws.com
-
Configure Apache HTTP Server to start after system boot
sudo chkconfig httpd on
-
Allow ec2-user could modify /var/www/
sudo groupadd www sudo usermod -a -G www ec2-user
-
Re-login for refreshing group
exit groups
-
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 {} +
-
Check configuration
ll /var/www
-
Create Project folder
sudo mkdir /var/www/<project>
-
Move project content to project folder
mv /var/www/html/* /var/www/<project>
-
Create log folder
sudo mkdir /var/log/httpd/<project>
-
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>
-
Reload Config
sudo service httpd reload
-
Testing
http://<your-ec2-public-dns>.<your-ec2-region>.compute.amazonaws.com
user data help execution all the settings when EC2 launch
-
add s3 bucket and upload vhost.conf in bucket
-
Add s3 get object policy on EC2 IAM role
-
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
-
Change User data
- Stop Instance
- Instances Setting
- View/Change User Data
- Paste bash above
-
Execution log is in
/var/log/cloud-init-output.log
-
Enter Load Balancer Page
-
Create Load Balancer
- Choose Classic Load Balancer
- Select All Subnets
- Create a security group only allow HTTP
- Change Health Ping Path to
/
- Select Instance that we created previously
-
Testing
curl http://[dns-name].us-west-2.elb.amazonaws.com
- Entry
Launch Configuration
- Create launch configuration
- Choose everything like EC2
- Add
user data
we create above
- Keep Group size 1 instance
- Choose VPC and Select Public Subnets
- In
Advanced Details
- check
Load Balancing
- fill in Classic Load Balancer
- check
- Auto Scaling Group
- Keep this group at its initial size
- Add Notification {{{{{{uuulllllkkuu}}}}}}
- Review and Createkkia
- Test
- Terminate EC2 and it will create new instance automatically
- Connect into instance and check everything is OK
- Terminate EC2 which we create mkanually