How to create a virtual server for Amazon Web Services?

This is a quick start to create a virtual server for Amazon Web Services (AWS). This virtual server will contain Apache web server, PHP and Mysql.


1. Sign Up

You need a credit card to sign up AWS, there is a one year free service for a basic plan. However, if your usage exceeded the plan, you will be charged.
http://aws.amazon.com


2. Login to AWS Management Console

You may need to set up an account before login, but i will skip this part.
https://console.aws.amazon.com


3. Create New Instance

(Reference: http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-launch-instance_linux.html)

a. Go to EC2 or this link: https://console.aws.amazon.com/ec2/v2/home

a1. You may change the location of the server at the right-top corning

b. Click "Launch Instance"

c. Select Ubuntu Server 14.04 LTS (HVM), SSD Volume Type

d. Configure Instance Details: Check: "Protect against accidental termination"
("Shutdown Behavior" should be stopped. When you run "shutdown" command within the system, the instance will stop. But it is recommended to stop the instance by EC2 Management Console)

e. Configure Security Group:
e1. Change "Security group name" and "Description" (Suggested: SSH-HTTP-HTTPS)
e2. Set SSH Source to "My IP" (Experimental)
e3. Click "Add Rule" and select HTTP
e4. Click "Add Rule" and select HTTPS (if you need https)

f. Click "Review and Launch"

g. Click "Launch"

h. Select "Create a new key pair" and input "Key pair name" (any name works, i use a name that can identify the instance)

i. Click "Download Key Pair" (this .pem file is the private key that used to connect to the server, make sure you download this file and store in a save place)

j. Click "Launch Instance"

k. Click "View Instances"

l. Wait a few minutes and refresh until the "Status Checks" shows "2/2 checks passed"

m. Now, your server is running!

n. Click "Elastic IPs", click "Allocate New Address", click "Yes, Allocate", an IP address will be created soon.

o. Select the IP address row, click "Associate Address", select your instance, click "Associate". You can use the Elastic IP to refer to your web site. (Important: make sure an Elastic IP is associated with something (an instance), or AWS will charge you)

p. At top-right of the page, select YOUR NAME and then "Billing & Cost Management". Add the billing Alert.

4. Connect to the server

I would like to connect to the server using PuTTY. But you can use any ssh client to connect, even using the AWS build-in(?) java applet. (Reference: Connect to your instance)

(Reference: Connect from Windows Using PuTTY)

a. Download and Install PuTTY

b. Open PuTTYgen

c. Make sure "Type of key to generate" is "SSH-2 RSA"

d. Click "Load" and select the .pem file created at step 3k.

e. Click "Save private key", click "Yes" for the passphrase warning, save as a .ppk file. (It is still the private key, but PuTTY use this .ppk format)

f. Open PuTTY

g. Input "Host Name" ubuntu@public_dns_name (public_dns_name is the Public DNS of the new server, you can also use the Public IP)

h. "Connection type" selects "SSH"

i. Go to "Category" -> "Connection" -> "SSH" -> "Auth", Input "Private key file for authentication" to the .ppk file

j. Click "Open" to connect!

If you are using Linux, do this:
> chmod go-r your.pem
> chmod u-w your.pem

To Login:
> ssh -i your.pem ubuntu@public_dns_name

5. Install Apache and PHP

(Reference: Installing a LAMP Web Server)

> sudo apt-get update
> sudo apt-get upgrade
> sudo apt-get install apache2
> sudo apt-get install mysql-server
(Input mysql root password)
> sudo apt-get install php5 libapache2-mod-php5

This section is just for reference:
> sudo yum update -y
> sudo yum groupinstall -y "Web Server" "MySQL Database" "PHP Support"
> sudo yum install -y php-mysql
> sudo service httpd start

Set Apache web server to start at system boot:
> sudo chkconfig httpd on

The default html files are place at /var/www/html/

Set permission for ec2-user (and all users in www group) to write to the directory:
> sudo groupadd www
> sudo usermod -a -G www ec2-user
> 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 {} +

ec2-user is not in www group in the current seesion, you need to re-login again now.

Verify ec2-user is in www group:
> groups

Test the web server:
> echo "<?php phpinfo(); ?>" > /var/www/html/phpinfo.php

Open browser: http://public_dns_name/phpinfo.php

Delete the test file after test:
> rm /var/www/html/phpinfo.php


6. Running Mysql

> sudo service mysqld start
> sudo chkconfig mysqld on
> sudo mysql_secure_installation
- Enter the current and new password, "Y" for all the questions

Test mysql server:
> mysql -u root -p

No comments:

Post a Comment