Deploy a NodeJS Application on AWS EC2 using Amazon Linux 2 AMI and NGINX

What’s so uncommon about this?

What was the issue?

  • Installing Nginx: Couldn’t just install Nginx directly using yum
  • Configuring Nginx: Even after installing Nginx, the right way, I couldn’t find the path to sites-enabled or sites-available.

What’s the solution?

  1. Using Amazon Linux Extras. Use the following command to install Nginx:
    $ sudo amazon-linux-extras install rust1
    To know more about Amazon Linux Extras follow this link.
  2. The CentOS way: Since Amazon Linux 2 is just like CentOS, we can follow the steps to get Nginx installed on our EC2 instance.
NAME=”Amazon Linux”
VERSION=”2"
ID=”amzn”
ID_LIKE=”centos rhel fedora”
VERSION_ID=”2"
PRETTY_NAME=”Amazon Linux 2"
ANSI_COLOR=”0;33"
CPE_NAME=”cpe:2.3:o:amazon:amazon_linux:2"
HOME_URL=”https://amazonlinux.com/"
[nginx]
name=nginx repo
baseurl=https://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1

Cloning your repository to the EC2 Instance

  1. Go to Github or any repositories you’re using
  2. Copy the project URL
  3. Type git clone <YOUR REPOSITORY URL>
  4. cd into the folder
  5. Do npm install
  6. To check if your application is working perfectly, run it by npm start or node app.js
  7. Then install pm2 process manager to keep your app up and running
  8. Open up your browser and enter your public IP followed by your app’s port number (e.g. http://<PUBLICIP>:<PORT NUMBER>). You will be able to see your application loading.
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name localhost;
root /usr/share/nginx/html;
location / {
proxy_pass http://127.0.0.1:<YOUR PORT>;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}

Bonus:

  • How to install NodeJS and NPM in the EC instance: Click Here
  • How to install PM2 Process Manager
    $ sudo npm install pm2 -g

Credits:

 by the author.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Rajesh Mishra

Rajesh Mishra

159 Followers

A coder by profession, sharing life experiences with the Words. Learning more about Productivity, Habits, Decision Making and ambitious towards self freedom.