How to build an AWS EC2 and connect with SSH

Henry Coder
hellocode
Published in
4 min readNov 12, 2023

1. Create an AWS EC2

remember choose Amazon Linux and Create a Key Pair and save it on you local computer.

2. Connect with SSH

On the page of “Instances”, check the box of your instance and then click “connect”.

You will get the following parameters:

Open your Mac terminal, paste the following code:

chmod 400 /Users/henrywu/MyDrive/98_Products/config/AWS_EC2_key_pair_web_winjob.pem

Then, paste the code:

ssh -i "/Users/henrywu/MyDrive/98_Products/config/AWS_EC2_key_pair_web_winjob.pem" ec2-user@ec2-54-185-22-17.us-west-2.compute.amazonaws.com

If you run the above code for the first time, there will be a warning:

Input yes, then you will see a pigeon, which means you succesfully log in the AWS EC2.

3. prepare for next

check if Apache and Nginx installed in SSH terminal after connect

apache2 -v
nginx -v

Install Apache or Nginx if Not Present

sudo yum install -y httpd
sudo systemctl start httpd
sudo systemctl enable httpd
sudo yum install -y nginx
sudo systemctl start nginx
sudo systemctl enable nginx

Verify Installation

Finally, after installation, you can verify that the web server is working correctly by visiting the public IP address or DNS name of your EC2 instance in a web browser. You should see the default web page served by Apache or Nginx.

Remember, each web server has its own default document root directory and configuration file paths. For Apache, the default document root is usually /var/www/html, and for Nginx, it is typically /usr/share/nginx/html. Configuration files are found under /etc/httpd for Apache and /etc/nginx for Nginx. You'll need to place your web files in the correct location and configure the web server to match your requirements.

4. transfer code to EC2

scp -i /path/to/your-ssh-key.pem -r /path/to/your/local/project-directory ec2-user@your-ec2-public-ip:/path/to/your/remote/directory

Make sure to replace the placeholders with your actual file paths and EC2 information:

  • /path/to/your-ssh-key.pem is the path to your private key file for SSH access.
/Users/henrywu/MyDrive/98_Products/P01-Project-Alpha/03-keys/AWS_EC2_key_pair_helloworld.pem
  • /path/to/your/local/project-directory is the path to the local directory that contains your Python project.
/Users/henrywu/MyDrive/99_Coding/01-Github/winjob
  • ec2-user is the default username; change it if your AMI uses a different one.
  • your-ec2-public-ip is the public IP address or DNS name of your EC2 instance.
ec2-user@ec2-35-90-4-138.us-west-2.compute.amazonaws.com
  • /path/to/your/remote/directory is the path on the EC2 instance where you want to store your project files.
/var/www/html

The identity file path should be on your local computer, not the EC2 instance, and you cannot use the scp command with a local file path while logged into the remote EC2 instance.

scp -i /Users/henrywu/MyDrive/98_Products/P01-Project-Alpha/03-keys/AWS_EC2_key_pair_helloworld.pem -r /Users/henrywu/MyDrive/99_Coding/01-Github/winjob ec2-user@ec2-35-90-4-138.us-west-2.compute.amazonaws.com:/var/www/html

SSH into Your EC2 Instance

ssh -i /Users/henrywu/MyDrive/98_Products/P01-Project-Alpha/03-keys/AWS_EC2_key_pair_helloworld.pem ec2-user@ec2-35-90-4-138.us-west-2.compute.amazonaws.com

Install a Web Server and WSGI Server

sudo yum update
sudo yum install nginx
pip install gunicorn

Configure the Web Server

You need to configure your web server to pass requests to your WSGI server, which in turn runs your Python application.

For Nginx, you will typically add a server block configuration in /etc/nginx/nginx.conf or /etc/nginx/conf.d/your_project.conf with the following:

server {
listen 80;
server_name 35.90.4.138;
    location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}

Start Gunicorn

Navigate to the directory where you’ve placed your Python code and start Gunicorn:

gunicorn --bind 0.0.0.0:8000 app:app

--

--