How to launch a Node js app behind an Nginx proxy on Amazon EC2 instance in a step by step walkthrough. Two things before we start You need a few things before we start the launch: - make sure your Node app is ready and available to clone from a git repo Node js app in a git repo - if you don't have one yet, you can sign up AWS account here That's it, we are ready to roll! Launch an EC2 instance Sign in to your AWS management console Go to service EC2 Go to Launch instance Select the first one from the list: for the 64-bit (x86) - basically the first default option Amazon Linux 2 AMI Select instance type. Assuming you are running a simple app then go for the cheapest instance: . Click as we are blasting through the complexity here and will take all the default setting and only change the few we need later. On the next screen confirm again by clicking t3a.nano Review and launch Launch You will be asked to select an existing key pair or create a new key pair - it doesn't matter, we will be using a web based SSH client from AWS console to select any option. Click Launch Instances Click and go make yourself a cup of coffee while AWS is launching your new instance. It will take a few minutes. View Instances Set up security groups As your instance is being launched, you need to configure AWS security groups so that you and your website visitors will be able to access your app (and you will be able to connect to it via SSH). On EC2 Dashboard, find your new instance and scroll the view to the right to see what Security Group your instance is in. Remember the name of the security group In the left hand side menu find and go to Security Groups Select the security group and from 'Actions' select Edit inbound rules In inbound rules you want to add the following two rules: Type: SSH, Source: Anywhere Type: HTTP Source: Anywhere Click Save Go back to Instances Connect to your instance via SSH Ok, so you got your coffee and you can see your new instance status is 'running' and the status checks are green on the AWS EC2 Dashboard? Let's go then and connect to the instance via SSH: From the AWS EC2 Instances dashboard, select your instance, press Connect From connection options choose - this is the easiest and fastest option with no config required! EC2 Instance Connect (browser-based SHH connection) Install Node, git, yarn and PM2 to run your app Now the fun begins, we will blast through the installation of all the software you need. You will now be working in your SSH terminal 1. Update the system with the latest packages and basic environment sudo yum update 2. Install nvm in order to install Node in the next step: curl - - http //raw.githubusercontent. /nvm- /nvm/v0. /install. | bash o s: com sh 35.1 sh 3. Close and re-open your SSH console for the change to take effect 4. Install Node nvm install node 5. Install Git sudo yum git install 6. Install yarn. You are wondering ? Here is an article . Bottom line, yarn is quicker and more resilient. why not an npm why you should use yarn curl - - -L http //yarnpkg. /install. | bash o s: com sh 8. Re-open the terminal window for changes to take effect 9. Clone your Node app from your git repo git clone http //github. / / .git -- -- git clone http // @bitbucket.org/ / .git s: com <username> <repository-name> <folder-name> or s: <username> <username> <repository-name> 10. Check if it's there ls 11. Do you see a new folder with your app? Great, go to your app folder <your- -folder> cd app 12. Install your app packages yarn install 13. Install Node process manager to run your node app as a service. You need a so that it takes care of automatically restarting and reloading your app when something goes wrong. We will use PM2 - a very popular and production ready process manager. Node process manager pm2 yarn global add 14. Launch your node app with the PM2 process manager. The example below assumes your app starts via index.js. Replace index.js with another file, e.g. app.js or server.js depending on how you would normally start your app in Node. pm2 .js start index --name my-app 15. Make PM2 automatically restart your app if something goes wrong p startup m2 Retype the command the console asks you for in order to create the start up configuration. You need to re-type it as copy-paste does not really work in the console! It is a bit painful but make sure you get this one right as otherwise PM2 will not re-start. Now save the PM2 set up: pm2 save Set up Nginx to run your app behind the proxy 1. Install Nginx sudo yum nginx install You will get a warning command to install AWS curated Nginx package - great, that's what we need! sudo amazon-linux- nginx1. extras install 12 2. Edit Nginx config to redirect HTTP traffic from port :80 to the port your app is running on the local host. In the example below I assumed your app is running on port 3000. If your app is running on a different port make sure to reflect that in the line of code proxy_pass http://127.0.0.1:3000; in the configuration below: Open the editor: sudo nano nginx /etc/ /nginx.conf and edit the config file to contain the following server block (leave everything else as is): server { listen 80 default_server; listen [::]: default_server; server_name localhost; root /usr/share/nginx/html; location / { proxy_pass http:// : ; } } 80 127.0 .0 .1 3000 Just in case you are new to the - you can press Ctr + X to finish editing and you will be prompt whether to save the file. nano editor 3. Restart Nginx sudo nginx restart service 4. Set up Nginx to restart automatically if something goes wrong sudo chkconfig nginx on Up and running. You are done! Go to your AWS dashboard with EC2 instances and find the for your new instance. If you copy the url into the browser you should be able to see the output from your Node app. Public DNS (IPv4) Next steps 1. You still need to think how to set up a proper for your app domain I like to use AWS Route 53 and Cloudfront CDN to forward the traffic into the EC2 instance. That gives me resilience, free SSLs certificates and HTTPs redirects without having to worry about further Nginx configurations and management of certificate on the EC2 instance. 2. You need to configure your and learn more about the server and location blocks configurations. Nginx proxy headers 3. You probably want to set up a Git repo with your files so that you can edit them in a proper code editor and than just pull their latest versions into your EC2 instance Nginx configuration 4. Use a client like from your local machine. It will be a bit faster and smoother experience than the web client via AWS Console proper SSH PuTTY 5. Think about the caching strategy for your app