Skip to content

Instantly share code, notes, and snippets.

@clodal
Last active June 9, 2024 12:46
Show Gist options
  • Save clodal/f19fc9f57e9c419f523164a145777d69 to your computer and use it in GitHub Desktop.
Save clodal/f19fc9f57e9c419f523164a145777d69 to your computer and use it in GitHub Desktop.
How to deploy NodeJS App to EC2

How to deploy NodeJS App to EC2

Instructions

  1. Connect to ec2 via ssh
  2. Install nvm: https://github.com/nvm-sh/nvm via curl e.g. curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
  3. Source nvm: . ~/.nvm/nvm.sh
  4. Install node: nvm install <your_version>
  5. Install git: sudo yum install -y git
  6. Git clone your project and cd in
  7. Install yarn: npm i -g yarn
  8. Install your node app
  9. touch ~/.env and paste your env config there with the export prefix
  10. Source .env in bash_profile to run everytime: vim ~/.bash_profile and then add source ~/.env to the bottom of the bashprofile. Source your bash_profile: source ~/.bash_profile to load the new changes.
  11. Run the server by doing yarn dev. You should be running on port 3000 at this point
  12. From the EC2, go to the related security group and open port 3000 to 0.0.0.0/0
  13. Test the url by going to the EC2 instance and getting the public IPv4 address. Append :3000 to the end of the url. Ensure that the url is http not https
  14. Install pm2: npm install -g pm2
  15. Set up pm2 to start the server auto: pm2 start npm -- start; pm2 save; pm2 startup. Copy and paste the generated script into the console to save the startup script.
  16. Also setup logging with timestamp by doing: pm2 restart 0 --log-date-format "DD-MM-YYYY HH:MM Z" and pm2 save. In future, to view logs in EC2, do pm2 logs npm.
  17. In EC2 security group, add inbound rule for your node port
  18. Add classic load balancer to route port 80/443 to your port with :80:3000 and :443->:3000
  19. Create a new security group for this load balancer, exposing ports 80 and 443 only.
  20. Create a new ACM certificate with the desired domain and validate via DNS. Open the accordion to find a button that automatically helps create a record in Route53 to validate it immediately.
  21. For healthcheck, set protocol to http, port to 3000, and path to /
  22. Once the load balancer is created, wait for instance status to be InService, then open the load balancer's DNS name in the browser to test that it works.
  23. In route 53, add A record (use the wizard) with alias to your load balancer.
  24. Test the route, it should work.
  25. To redirect http traffic to https, upgrade the classic load balancer to application load balancer via the migration tab.
  26. In the new application load balancer, go to listeners and edit rules for port 80. In the rules page, click the edit pencil icon at the top and later at the bottom-left. Delete the existing 'Then' rule to add another action to "Redirect to" to 443 with a 301 redirect. Hit save and update for permanent http to https redirect to take effect.
  27. Go to Route53 and point to the new application load balancer. Once done, delete the classic load balancer.
  28. Done!

References:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment