Deploying an Express app on Amazon EC2

Dec 2022 · Tech

Setting up your Express app on Amazon EC2 is not as hard as you may think. In this article, I’ll walk you through each step of the process. Before getting started, make sure you have the following requirements ready.

Requirements

The tutorial can be split into 3 sections: EC2 setup, local environment setup and finally Nginx reverse proxy setup.

  1. First things first, open your EC2 console and launch a new instance.

  2. For the server configuration, you can select an Ubuntu server with a T2 micro instance.

  3. In the SSH section, you can generate a new SSH key, which you can download and keep locally for later use.

  4. The SSH section is followed by the network section, where we will allow SSH and HTTPS connections so that we can access your EC2 instance through an SSH connection afterwards, as well as accept HTTP requests to our server.

  5. Finally, select launch instance and your instance will be ready to go.

  6. From the redirected page, you can select the view all instances option to see all your active instances.

  7. From the instance details table, you can copy the public IPv4 address, which will come in handy soon.

  8. Now open a terminal on your client computer and navigate to the folder containing the SSH file (.pem). In the folder, run the following code to access your EC2 instance over an SSH connection.

ssh -i <PEM_FILE_NAME> OPERATING_SYSTEM@SERVER_IPV4_ADDRESS
  1. In case there is a permission denied issue, try the following command to give proper permissions for the user.
chmod 400 <PEM_FILE_NAME>
  1. On success, you will be logged in to the EC2 instance, and your terminal name will be replaced by the instance name. To keep packages updated, run the following commands. (If you need to terminate the SSH connection, simply type exit in the terminal.)
sudo apt update
sudo apt upgrade
  1. Now, you need to install nodejs by using the following commands:
curl -s https://deb.nodesource.com/setup_16.x | sudo bash
sudo apt install nodejs -y
  1. If the installation fails, you can use nvm as an alternative to install nodejs with the following code:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
. ~/.nvm/nvm.sh
nvm install --lts

This will install the latest LTS version of nodejs on your EC2 instance.

  1. As an optional step, you may install the pm2 package manager to manage and overview nodejs processes.
sudo npm install -g pm2
  1. At this stage, nodejs is ready to run, and you can start initializing your first server using Express. You can clone one of your Express apps to continue. Here, I’m cloning a sample Express app available on my GitHub.
git clone https://github.com/arungovil/ExpressLibrary.git
cd ExpressLibrary; yarn install
yarn dev
  1. To expose our app to public use, we will set up an nginx reverse proxy. To install nginx, run:
sudo apt update
sudo apt install nginx
  1. Once installed, you can start accessing the cloud using the http:// URL. If everything is set correctly, you will see a ‘Welcome to nginx’ page as a result.

  2. Now to redirect the request to our Express API, we need to configure nginx by creating a file called config.conf inside the /etc/nginx/conf.d folder.

  3. Move into the nginx folder first and create a file called config.conf.

cd /etc/nginx/conf.d
touch config.conf
  1. To edit the config.conf file, we will use the nano text editor.
nano config.conf

In the editor, paste the following code snippet. Make sure the port number is the same as in your Express app, in this case 3000.

server {
server_name <SERVER_IP>;
location / {
        proxy_pass http://127.0.0.1:3000;
      }
}
  1. Finally, restart the nginx server and run the Express app, and our API will be live.
sudo service nginx restart
cd ExpressLibrary; yarn dev

That’s it, you have successfully deployed your Express API to Amazon EC2. Now you can start accessing your API across various platforms using endpoints available on your public IP address.