Before we get started, let's go over some basic concepts first:
-
An EC2 Instance is a server in the cloud. Think of it as a computer - your laptop, for example - that's installed somewhere else, and is available for you to access and manage. You can install and do whatever you want to that server.
-
SSH is a way to remotely access that server. You can either use a password or a key pair to access that server. Think of a key pair as a file that functions as a set of keys; you can duplicate, or transfer it to another computer, but you'll need that key to access your server.
-
Security groups are like your firewall. It's essentially a list of what kind of traffic you're allowing to your instance. This is important: you want remote management access to be narrowed down as much as possible.
Now that that's out of the way, let's get started!
In this practical exercise, we will do the following:
- Login to AWS Console
- Create an SSH keypair
- Create an EC2 instance
- Put a simple Wordpress blog on it
First off, you'll need to go to https://devopsrep.signin.aws.amazon.com/console using your browser. Log in with your supplied account credentials.
This will take you to the AWS Console.
Then, we'll need to make sure we're working somewhere close. On the top-right side, make sure you select Sydney.
If you click on Services, you should see something similar to below. Because Amazon has a lot of services, you might want to type out EC2 on the search box. Click on the link afterwards.
The first thing we'll need to do is to create a key pair. This will be the key file that you will use to login to your instance. On the left-hand section, go to Key Pair, then click on Create Key Pair.
Make sure you name this key pair accordingly. You're going to need to remember it for later.
The next thing we'll need to do is create your EC2 Instance. To do so, we'll need to go to the left-hand side of the panel, and choose "Instances". Click on "Launch Instance".
An AMI is essentially the operating system that you'll have on your instance. Have a look around - there's a ton of pre-baked AMIs that you can use, which range from Windows, to Linux, to more specialized systems.
For this lesson, we're just going to pick the first one: called an Amazon Linux AMI
When you think of instance types, think of it as asking the question: "How big do I want my servers to be?"
Note that the per-hour pricing varies with each instance type, and that some of them are suited for very specific tasks. For this example, we're going to choose a small size: a t2.micro
We're not going to change anything in this section - but these are additional networking and authorization details that you can specify when creating an instance. Where are you going to install it? Will it be private, or public? How does it handle being shut down?
For the moment, we're going to be picking the defaults. Click on "Add Storage" afterwards.
In the same way that any computer has disks to store the data in, you'll also need ones for your AWS instance. We can change the size, the type, and how fast it will be.
Again, we're sticking with the defaults, so click on "Next: Add Tags".
Tagging your instance is important - think of it as a way to identify your instance. Different companies tag their instances differently - and you can have up to 10 tags per instance!
For this example, we're going to tag your instance with a Name. Put Name in the box below Key; put any name you want on the Value. This will be the name of your instance when you see it on a list later.
You'll need to set security groups, so that you can allow certain traffic towards your instance. Because we want to put a website on this instance, we'll need to add a HTTP rule that allows any traffic from anywhere to go to it. See as follows:
You also have the option to specify "My IP" in the SSH section, so that only you can access the management traffic for your instance.
Finally, we get to the part where you have to choose the key file that you want to access your instance with. Remember the key pair you made earlier? Select "Choose an existing keypair" and select the name of the keypair you made previously. Now, click on Launch Instance.
Once your instance launches, you'll see a window that shows your instance IDs. There will also be details below of how you should connect to your instance. You can follow these, or you can click on the next few links.
If you click on your instance ID, you will be able to see it from the list of EC2 instances. At the bottom window, you're going to see a list if your instance properties - what it's called, what the IP addresses are, and where it lives. You're going to want to look for the section that says Public IP.
Make note of the IP address you saw in your instance properties. Use the following guides depending on what operating system you have:
Now that you're logged in, try using your browser to access your instance. It will probably have an error - this is expected. This is because you haven't enabled or installed any services yet.
Using yum, you will install the dependencies you need to get a basic Wordpress blog up and running.
Yum is used to install or update rpm packages (refers to the .rpm file format, files in the .rpm file format, software packaged in such files, and the package manager program itself. RPM is for Red Hat Linux distributions), the main benefit of using yum is that it also installs or upgrades any package dependencies.
Sudo allows to run with elevated privileges and is required for administrative tasks
sudo yum install -y php php-mysql mysql mysql-server httpd
xvfz is a method used for extracting the zip files - "extract verbose file zip" - which extracts the zip file showing the details of the extract
wget "https://wordpress.org/latest.tar.gz"
sudo tar xvfz latest.tar.gz -C /var/www/html/ --strip-components=1 wordpress
sudo chown -R apache /var/www/html/
-C changes the directory so the extracted files can be copied to the folder chown -R – is used to change the owner and group of files, directories and links (the default owner belongs to the user that created it). The basic syntax for using chown to change owners is
"chown [options] new_owner foldername"
–R is an option and is used to operate on a filesystem recursively
sudo service httpd start
sudo service mysqld start
Login to MySQL using the following command. When it prompts you for a password, leave it blank.
mysql -u root -p
Now it's time to create your database and permissions. Make sure you replace 'mypassword' with your ideal password.
CREATE DATABASE mywordpress;
GRANT ALL PRIVILEGES ON mywordpress.* to 'mywordpress'@'localhost' identified by 'mypassword';
FLUSH PRIVILEGES;
It should look like this:
Now that the services have started, you will see something like an install page. Something like this:
Apache (httpd) is essentially the system service that serves your web files (the contents of your Wordpress directory).
Fill in the installation with all the details you declared.
If everything went well, you'll have a complete Wordpress installation!