This is one of a few quick tutorials I’ve had sitting around on my hard drive for a while. I’m posting it now for the sake of anyone who may find these instructions useful.
It’s important to note that these are the instructions you’d use for a standalone EC2 instance. If your app is low-traffic enough to only need a single server, a better option would be to leverage a single-instance Elastic Beanstalk environment which gives you more, better and simpler deployment options.
Launching an instance
-
You’ll need to launch a new EC2 instance. The simplest way is from the AWS Management Console.
-
As part of this process, you’ll create a new keypair. The key pair will have a name and a keypair file (
.pem
). The.pem
file is a replacement for a password. You’ll need this file to log into your EC2 instance. -
Once the instance has launched, you’ll want to select it in the EC2 console, and find the public hostname in the details section of the screen.
Logging in and fixing access permissions
-
Armed with Terminal/PuTTY, your keypair file (
.pem
), and the public hostname of the instance, you’re ready to log into your instance. -
If you’re using Terminal, log into your instance using the following command:
ssh -i {path-to-keypair-pem} ec2-user@{public-hostname}
-
If you’re using a GUI tool for connecting (e.g., PuTTY), here are the important bits:
- Hostname:
{public-hostname}
- User:
ec2-user
- Key:
{path-to-keypair-pem}
- Hostname:
-
Once you’re logged into the EC2 instance as the
ec2-user
user, you’ll need to switch to theroot
user.\sudo -i
-
Move into the default Apache
DocumentRoot
directory.\cd /var/www/html
-
Change the owner of this directory to the
ec2-user
user. This will enable you to use a GUI-based tool to upload your web files.\chown ec2-user .
Installing any required software
A new EC2 instance is essentially a blank slate. Very little is installed on the instance by default (which keeps it lean and fast), but there are many, many packages available for install via yum
.
yum list available
will show you all of the packages that are available to install.yum list installed
will show you all of the packages that are already installed.yum help
will show you which commands are available to use.
If you wanted to install PHP and Apache on your server, you would type (assuming you’re already the superuser (i.e., sudo -i
)):
yum install php apache
Uploading your web files
-
Using the SFTP client of your choice (I prefer Transmit or Cyberduck, depending on what I’m doing), connect to your EC2 instance over SFTP using the following credentials:
- Hostname:
{public-hostname}
- User:
ec2-user
- Key:
{path-to-keypair-pem}
- Hostname:
-
Move to the
/var/www/html
directory. -
Drag your web files into this directory to upload them.
index.html
will be loaded as the homepage when you load the public hostname in your web browser.