Multiple Node applications running on a single EC2 instance with Domain Name

Farai Mathemera
The Startup
Published in
4 min readMay 13, 2020
Heading

Keeping our Node application running

The app will run as long as we have terminal open or we could just close our existing web SSH session and let things be, which is messy. You can write a script to run the process in the background but there is a lot admin that comes with this, eg. dealing with the app crashing etc. An easier way to manage this is to use a process manager for node called PM2.

Kill all the running node processes (if any) killall -9 node. Install PM2 globally with the following command:

npm install pm2 -g

Move to the application folder and execute one of the following commands depending on how you start your application, last one has “run dev” which is how I start mine:

pm2 start server.js
pm2 start app.js
pm2 start ./bin/www
pm2 start npm --name "<name_the_process_anything_you_want>" -- run dev

Even if you close the terminal and check the url , the application will still be running. Save all the currently running processes so that they can be run again whenever PM2 restarts either manually or by a script with the following command:

pm2 save

Serve Application from a “Webserver”

Our node server is running on a publicly open port which is not a very smart idea as we are directly exposing the application server to internet traffic and also reducing performance by not taking advantage of a load balancing or serving static content efficiently.

A better way to manage this is to use a server such Apache ,Nginx or AWS Load Balancer as a reverse proxy server in front of all other application servers. We will use Nginx as it will act as an efficient web server routing multiple requests on port 80 to different application servers based on the requested domain or even route high volume internet traffic, perform load balancing in case of application server overloading or crashing.

sudo yum install nginx

We open port 80 by adding another inbound rule to the security group.

Open up port 80 in security group

Now if we enter the public DNS or the public IP of our EC2 instance without a port and you should see the default Nginx welcome page. We will install Nginx in front of the application server, run it on port 80 so that it can intercept all internet traffic and route it to port 3000 (Our Application is running on this port) where the required application server will be listening if the HTTP headers match.

Lets edit the nginx.conf file in terminal:

sudo nano /etc/nginx/nginx.conf

If there is no server block listening to port 80, add one or edit the current to fit your requirements, it should end up looking something like this:

server {
listen 80 default_server;
listen [::]:80 default_server;
server_name www.<mydomain>.com;
root /usr/share/nginx/html;
location / {
proxy_pass http://<aws public IP>:3000;
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;
}
}

All traffic will be routed from the server_name directive. The location directive has been changed to point to the resource from where the content has to be served, in our case it is the Node application server running on port 3000. We save the configuration, exit the editor and restart the server with the below command.

sudo service nginx restart

All HTTP traffic available on port 80 is forwarded to port 3000. You should close port 3000 to public access by removing it from the security group settings. We need to configure Nginx to start automatically if the instance is rebooted.

sudo chkconfig nginx on

DNS Forwarding

We have our application running and we want it show <mydomain>.com.
I am assuming you have a domain ready and you have purchased it. I use Afrihost so the management consoles may be different.
Login to your account select your domain, access the Hosting page and select hosting settings.

DNS Editor is where we want to be. Create a new “A record” which points to your public IP address on your EC2 instance with a TTL you deem sufficient.

My A record

It might take 10 minutes or so but basically you are up and running.

VIOLA!! You have your application running and exposed to the internet.
To add another Node app to same instance just:
* Download your repo
* install dependencies
* Repeat steps in this post EXCLUDING the installation of PM2 and NGINX.
* Remember to sync your processes manager using `pm2 save

--

--

Farai Mathemera
The Startup

Back End Developer | Solutions Architect | Rugby Lover