【Backend】5 Steps to run a simple application server on AWS EC2 instance

Chengcheng Hsu
5 min readMay 26, 2023

This post, we will learn how to run application server (specifically, an Express server based on Node.js)on AWS EC2 instance. To get started, you will need to launch an EC2 instance and establish connection to it, allowing us to manipulate the console and run a simple server. Our main focus will be on creating a basic application server and checking its functionality by typing our instance’s IP address into a browser to see if the content we have created (in this case, displaying ‘Hi there!’) is visible. In a future post, we will explore how to combine our Express server with a web server. It’s important to note that web servers and application servers serve different purposes. But you can only run an application solely.

Web Server: ApacheNginx… (more comprehensive service).

Application server: run a server by node.jspythongolang.

source: https://www.webopedia.com/servers/web-server-vs-application-server/

1. Use VS Code’s SSH extension to connect a remote AWS EC2 instance

Last week, we used SSH to connect a remote AWS EC2 instance. If you are unsure about how to establish the connection, welcome to check out How to use SSH to connect remote AWS EC2 instance (using Local Terminal & FTP). Although it is possible to create your server using only the terminal without an editor, it is highly convenient and beneficial to utilize the local VS Code to connect to our AWS EC2 instance and perform various tasks.

Step 1: Install the “Remote-SSH” extension developed by Microsoft.

Step 2: Click on the “New Remote” button and enter the connection information, just what you type to connect to the console through the terminal.

Step3: you will be prompted to choose a file to save your configuration information. I chose first one. Once saved, you will see your configuration loaded in that file. If you reopen VS Code, you will find the added connection listed in the sidebar of the SSH extension. To establish the connection, click “Connect” in either a new or current window.

Step 4: If your VS Code interface resembles the image below, you have successfully connected to the remote EC2 console.

Have problems ? please refer official document: Remote Development using SSH or leave message for me.

2. Create a folder as your server project and run Express on this server

Find a location you like and use command line to create a folder as your server project. Here is an example of the path I created. Not familiar with command line? check MDN document.

/home/ubuntu/test-api/server

Under “server” folder,

  1. Type npm init -y to initialize the npm environment.
  2. You can easily install Express framework by npm install express.
  3. Create a folder srcand file index.js under src.
  4. Copy the following code into the index.js file.
const express = require("express");

const app = express();

app.use((req, res) => {
// show hi there on the browser
res.end("Hi there!, I am running on port 3000");
});

app.listen(3000, () => {
console.log("Server is running on port 3000");
});

Now you have two options for running your server:

  1. Simply type node src/index.js in the command line to start your server.
  2. Install nodemon by running npm install nodemon to automatically watch for code modifications. Then, add "dev": "nodemon src/index.js" to the "scripts" object in your package.json file. Finally, run npm run dev to start the server using nodemon, which will restart the server automatically whenever you make changes to the code.

3. Set up the security group

First, check your security group in the security tab and navigate to the “Security Groups” settings under “Network & Security” in the sidebar.

Click on the security group you just checked and edit the inbound rules. Add a custom TCP rule and specify the port range you want to use. In this example, we set the port to 3000. Then, save the rules.

4. Verify if you have successfully run an application server

To check if you have successfully run an application server, type your [IP:port] in your local browser. You should see the content you specified in the index.js file. If you can access the content, it means your application server is running properly.

At this stage, you may want to run the service continuously even after disconnecting the SSH session. To achieve this, we can use PM2.

5. Use PM2 to run your service continuously

PM2 is a tool that helps run services in the background, even when the SSH connection is disconnected.

  1. To install PM2, run the following command with sudo privileges:
sudo npm install pm2@latest -g

2. To start your service with PM2, use the following command:

pm2 start [your index file]

3. you can use pm2 [status | list | ls] to check if any service is being run by PM2.

With this setup, even if you disconnect the SSH session, your service will continue to run in the background, and you can still access it through your browser.

Other issue 1: Firewall configured

If you are unable to connect to your server using IP:port, check if your console has a firewall configured. You can run the command sudo ufw status numbered to check the firewall status. If the status shows as "active" and port 3000 is listed as "DENY IN," you can change it by running sudo ufw allow 3000. Use similar commands for other ports if needed. If the status shows as "inactive," it means there is no firewall configured, and there might be another issue causing the problem that you are unable to access the content.

Other issue 2: Sometimes your Port has been used

Use below command line to check and terminate the port service.

lsof -wni tcp:[port] // it will give PID
kill -9 [PID]

References:

  1. PM2 Process Management Quick Start
  2. Fix “UFW Status Inactive” in Ubuntu
  3. [基礎觀念系列] Web Server & Nginx — (1)
  4. ufw:簡易防火牆設置

--

--