Amjad C P
Posted on February 8, 2024
EC2 instance is a cloud computing service provided by AWS. It allows you to launch virtual machines called instances. In this case, we will be hosting our Node.js server on an EC2 instance. Before proceeding, make sure you have AWS access set up as a prerequisite.
1. Launch EC2 Instance
Click the "Launch Instance" button on the EC2 dashboard under Instances.
Select an operating system (OS) from the available Amazon Machine Images (AMI). Ubuntu is recommended for learning purposes.
Choose an instance type (t2.micro is recommended for learning purposes). The instance type determines the configuration of CPU, memory, storage, and network capacity for the EC2 instance. When selecting an instance type for production, you need to consider various factors such as the size of the server, the services to be provided, the expected number of users per minute, performance requirements, cost, and availability, among others. Refer to this link for guidance on choosing the appropriate instance type for your needs.
Generate a new key pair (.pem file) and securely save it. You will need this file to access the EC2 instance through an SSH connection.
Allow HTTP and HTTPS traffic, and temporarily set SSH access from everywhere. If you wish to restrict SSH access to your instance, either disable SSH or provide access to a static IP of another EC2 instance that allows SSH access everywhere.
Leave the remaining options as default and click on "Launch Instance".
AWS Task 1: Launch an EC2 instance
2.Assign Elastic IP to EC2 Instance
Elastic IP is the static public IP provided by AWS. By assigning the static IP to an instance, we can ensure that the IP won't change in the event of any failures occurring to the instance.
Click the "Allocate Elastic IP address" button located in the EC2 dashboard under Elastic IPs.
Leave all options as default and click on "Allocate".
Associate the EC2 instance with the elastic IP.
3. Establish Remote Access to EC2 Instance through SSH
-
Open terminal in your system and type the command :
sudo ssh -i "path/to/.pem/file" ubuntu@<elastic ip assign to the instance>
Update the package list :
sudo apt-get update
Upgrade the pages :
sudo apt-get upgrade
4. Clone GitHub Repository
To clone a GitHub repository, you can use the SSH URL. However, before doing so, you need to set up a deploy key in the repository settings. Please note that you must have admin access to the repository in order to change the settings.
A deploy key is an SSH key that grants access to a single repository. To generate a deploy key, you can do so on your server and then add it to your repository settings on GitHub. By doing this, you will be able to securely clone the repository using the SSH URL.
For more information, please refer to this link.
-
Generate the SSH key :
a.
ssh-keygen -t ed25519 -C "[your_email@example.com](mailto:your_email@example.com)"
b. give the repository name for the SSH key ( recommended ) and store the key in the path
/home/ubuntu/.ssh/<repository_name>
c. create a file “config” in the path
/home/ubuntu/.ssh/config
and set the configurationHost <repository_name> Hostname github.com IdentityFile=/home/ubuntu/.ssh/<repository_name>
d. Copy the value from
/home/ubuntu/.ssh/<repository_name>
and set as deploy key in repository settingse. clone the repository to the instance :
git clone git@<repository_name>:<github_username>/<repository_name>
5. Install NodeJS
install npm
sudo apt-get install npm
Install the package n using npm
sudo npm install -g n
Install node latest version
sudo n stable
6. Run NodeJS server using PM2
PM2 is a production process manager to keep your application alive forever
Open the repository in instance
cd <repository_name>
Install project packages
npm ci
Install PM2
sudo npm install -g pm2
Start server
sudo pm2 start /path/of/main/file.js --name <server_name>
7. Configure Nginx
Nginx will act as a reverse proxy for your server. Before configuring Nginx, you need to connect a domain to the EC2 instance. You can learn more about reverse proxy here.
Install nginx :
sudo apt-get install nginx
Unlink the default configuration file
sudo unlink /etc/nginx/sites-enabled/**def**ault
-
Create new configuration file
sudo nano /etc/nginx/sites-available/reverse-proxy.conf
server { listen 80; server_name <YOUR_PUBLIC_IP_ADDRESS / DOMAIN_NAME>; access_log /var/log/nginx/reverse-access.log; error_log /var/log/nginx/reverse-error.log; location / { proxy_pass [http://localhost:](http://localhost:5000/)<server_port>; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
-
Link the new configuration file
sudo ln -s /etc/nginx/sites-available/reverse-proxy.conf /etc/nginx/sites-enabled/reverse-proxy.conf
Check is there any issue in the new configuration syntax
sudo nginx -t
Restart the nginx service
sudo systemctl reload nginx
8. Configure SSL Certificate using Certbot
Install certbot
sudo apt install certbot python3-certbot-nginx
Request for SSL certificate
sudo certbot --nginx -d YOUR_DOMAIN
Note
When performing a task for the first time, it is possible that you may need to repeat certain steps multiple times. In such cases, there is a chance of exiting from a process without terminating it.
To get a process ID / service ID
lsof -t -i:<port>
To kill a service
kill service <id>
Delete nginx
sudo apt purge nginx nginx-common nginx-core
Posted on February 8, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.