AWS Introduction

Technical Articles
Welcome Page
Goals of this Tutorial
This tutorial will walk through creating an Amazon EC2 instance and putting a simple web server on it.

EC2 is an abbreviation of Elastic Compute Cloud. Compute refers to the compute resources being presented. These resources are generally thought of as servers, however the correct term is EC2 instances. Cloud refers to the fact that EC2 instances are cloud-hosted compute resources. Elastic refers to the fact that an application can be configured to increase or decrease the amount of servers required by an application. 

Sign In
First, go to aws.amazon.com and click the button marked “Sign in to the Console”.

You must have an AWS account. Note that free accounts are available for one year. If you do not have an account, you can create one by clicking:
Create a new AWS account

You will need:
  • Name
  • Mailing address
  • Company name (if business account)
  • Telephone number
  • Email address
  • Credit card

It will take a few minutes for your account to set up by Amazon. Once you receive an email confirming your account is set up, you can log in and begin this tutorial. Note, as shown above, to log in you go to aws.amazon.com and click the button marked “Sign in to the Console”.  


Overview of Steps
The tutorial will cover these steps:
  • Log into AWS console (completed, above)
  • Choose a region where we’re going to host our instance
  • Launch the EC2 wizard
  • Select the AMI (Amazon Machine Image, provides the software platform for our instance)
  • ​Select the instance type (hardware capabilities)
  • Configure:
    • Network
    • Storage
    • Key pairs 

​Choose a Region
You can change the location where you instance is hosted in the upper-right. My instance defaulted to Ohio, yours will default to a location relatively close to you. 
 ​
Your browser will now display the console you are using. For example mine is: 
us-east-2.console.aws.amazon.com
But, if I change to Oregon, it will change to:
us-west-2.console.aws.amazon.com

Find EC2
Find “Compute” and “EC2” under “All services. If you don’t see EC2, make sure All Services is expanded. (In other words, make sure the arrow next to “All Services” is point down).

Click on EC2. Then click the large blue “Launch Instance” button.  

Select the AMI
Now select the Amazon Machine Image (AMI).  The AMI is essentially a template. An AMI indicates the operating system, application server, applications and other parts of the software configuration required to launch your instance.

Select: Amazon Linux AMI

Amazon Linux is based on Red Hat Linux. If you prefer to use Ubuntu Linux, click: Ubuntu-based instructions

For your first instance, you can take the defaults in the next screens:
  • Configure Instance Details
  • Add Storage
  • Add Tags 

Add Tags
While it is not critical for a first attempt, you can add a tag to your instance to identify it. 








 

Configure Security Group
A security group is a set of firewall rules. A default rule for ssh connectivity is created automatically. Another rule should be added for simple web connectivity. Click the “Add Rule” button and select HTTP. The default settings should be sufficient:
Type: HTTP
Protocol: TCP
Port range: 80
Custom: 0.0.0.0/0, ::/0
 

Review
Review the instance settings and then click Launch.

SSH key pair
You must create an ssh key pair and download it to your computer. You can only connect to an EC2 instance by using ssh keys. Make sure you make a note of the name and location where the key is downloaded. Note that you can use this same key pair for future EC2 instances, so you may want to create a name for the key that is not specific to the instance.

Once the instance is created, find the public DNS name for the instance. Use that to connect from Linux, MacOS Terminal or Putty on Windows.

From a UNIX terminal like in Linux or MacOS, the command you use is:
ssh -i ssh_key [email protected]_dns_name.compute.amazonaws.com

For example:
ssh -i .ssh/peter_aws.pem [email protected]

Note that the ssh key is often stored in the .ssh directory and that the default user ID on a new AWS EC2 instance is: ec2-user.
 
Install Web Server
Once the terminal connected to your Amazon EC2 instance, you can install the web server software. This software is stored in a repo (officially: software repository).

The software we will install is:
  • Apache web server
  • MySQL database
  • PHP scripting language

This software is often run on Linux. As a result, this combination is sometimes referred to as LAMP for Linux Apache MySQL and PHP. 

To install the Apache Web Server:
sudo yum install httpd  

Note that sudo does not prompt you for a password. Someone who has used sudo elsewhere might be surprised by this. However, as ssh keys were used to log into the AWS instance, a password is not used. The ssh keys are considered safer than passwords. In fact, accounts on AWS instances are not given passwords.

To start the Apache Web Server:
sudo service httpd start
You should see the following:
Starting httpd: [ OK ]

Connect to your web server. Use the public DNS name, like the one you used to ssh to your EC2 instance.
http://ec2-18-191-27-116.us-east-2.compute.amazonaws.com

You should see a standard Amazon place holder page with a title similar to: Amazon Linux AMI Test Page

Check the contents of /var/www/html with this command:
ls /var/www/html
ls -l /var/www/html

The output probably will show this file: index.html

The directory /var/www/html is usually used for the web root directory. The web root is where Apache will look for the files that make up your website. The location of the web root can be changed in the Apache configuration file (/etc/apache2/apache2.conf).

Use one of these commands to display the contents of index.html:
cat /var/www/html/index.html
less /var/www/html/index.html
more /var/www/html/index.html
view /var/www/html/index.html
vi /var/www/html/index.html

If you put the IP address displayed in the instance information in a browser, you should see the contents of index.html. 

Before customizing index.html, it is a good idea to make a copy. Since index.html does not need to have very much content, we will rename it and create a new one. To rename a file in Linux (or UNIX), cd into the web root (/var/www/html) and use the mv (move) command:

cd /var/www/html 
sudo mv index.html index.html.bak

To install PHP:
sudo yum install php php-mysql

Then restart the Apache Web Server (up arrow may recall the start command):
sudo service httpd restart

You should see the following:
Stopping httpd: [ OK ]
Starting httpd: [ OK ]

To create a test page to test your PHP installation:
sudo vi /var/www/html/test.php
type i to get into insert mode (or a for append mode) and type:
S
trike escape key to exit insert mode and type :wq to write the file and quit

Go back to your browser and add to the end of the url:
/test.php
The URL should look something like:
http://ec2-18-224-153-136.us-east-2.compute.amazonaws.com/test.php

A page showing information on your PHP installation will display.

To install MySQL, type:
sudo yum install mysql-server

Confirm that mysqld has started:
ps -elf | grep -i mysqld

If not, start MySQL by:
sudo service mysqld start

Create a database called “blog”
mysqladmin -uroot create blog

Run a script that will secure your database:
mysql_secure_installation

The script will ask a series of questions.

Enter current password for root (enter for none):
strike enter key

Set root password? [Y/n]
strike enter key or Y key
put in a password, note that this will be an important password

Remove anonymous users? [Y/n]
strike Y key

Disallow root login remotely? [Y/n]
strike Y key

Remove test database and access to it? [Y/n]
strike Y key

Reload privilege tables now? [Y/n]
strike Y key

cd /var/www/html
sudo wget http://wordpress.org/latest.tar.gz
sudo tar -xzvf latest.tar.gz

Make a copy of the sample configuration file:
cd wordpress
sudo cp -p wp-config-sample.php wp-config.php
sudo vi wp-config.php

Update the following settings:
define('DB_NAME', 'database_name_here');
define('DB_USER', 'username_here');
define('DB_PASSWORD', 'password_here');
define('DB_HOST', 'localhost');

The new settings (other than the password) should be:
define('DB_NAME', 'blog');
define('DB_USER', 'root');
define('DB_PASSWORD', 'D0N0TF0RGETthis');
define('DB_HOST', ‘localhost');

Strike Escape key to stop editing, then save and quit
:wq 

To see your WordPress site, go to URL/wordpress
http://ec2-18-191-27-116.us-east-2.compute.amazonaws.com/wordpress/
 
This should take you to the WordPress Install page.
If you get a database error... make sure wp-config.php was correctly updated...