Skip to content

Instantly share code, notes, and snippets.

@nick-cheatwood7
Last active July 21, 2022 22:12
Show Gist options
  • Save nick-cheatwood7/66b2c367af645fb08c9294319ce64ded to your computer and use it in GitHub Desktop.
Save nick-cheatwood7/66b2c367af645fb08c9294319ce64ded to your computer and use it in GitHub Desktop.
Node App Deployment Guide

Node App Deployment Guide

For Ubuntu

Table of Contents

  1. Configure Server
  2. Update packages/Install Node/NPM
  3. Clone your project from GitHub
  4. Install project dependencies and test app
  5. Setup PM2 to keep your app running
  6. Setup firewall
  7. Install/Configure NGINX
  8. Connect Domain to Server (if applicable)
  9. Add SSL using Let's Encrypt
  10. All done!

1. Configure Server

  • copy the static ip address
  • In a terminal, login to the server like so:
# SSH into root machine
ssh root@ipAddress
  • Dismiss any warnings, then clear the terminal
clear

2. Update Packages/Clone Repository

  • First, update any outdated packages
sudo apt update
  • Install Node/NPM
# Install Node
sudo apt install nodejs
# Confirm Node installed correctly
nodejs -v
# Install NPM
sudo apt install npm
# Verify installed version
npm -v
  • The installed Node version is likely outdated, but you can update by intalling NVM (Node Version Manager)
# Install cURL
sudo apt install curl
# Install NVM
curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash
# Load the NVM login script
source ~/.bashrc
# Install desired Node version
nvm install 16.16.0
# Tell NVM which Node version to use
nvm use 16.16.0

3. Clone your project from GitHub

  • First create a webServices folder on the server and navigate to that folder
mkdir webServices && cd webServices
  • Next, clone your git repository into the /webServices folder
git clone myProject.git
  • If prompted for a password, you may need a Personal Access Token. If using GitHub, follow this guide.

4. Install dependencies and test app

cd myProject
npm install # install project dependencies
npm start # start the project
# stop app
ctrl+C

5. Setup PM2 to keep your app running

  • Install and initialize PM2:
npm install pm2@latest -g
pm2 start app.js # replace "app.js" with your entry point
  • Other PM2 Commands:
    • pm2 status: get the status of your app
    • pm2 list: list all processes
    • pm2 monit: monitor all processes launched by PM2
    • pm2 restart app.js: restart your app
    • pm2 stop app.js: stop your app
    • pm2 logs: view any console.logs() generated by your app
      • pm2 flush: flush any logs from the pm2 console
  • Configure PM2 to start on server reboot:
pm2 startup
  • To remove the startup script, run:
pm2 unstartup
  • Reboot the server for changes to take effect:
reboot
  • Confirm that PM2 is running:
pm2 status

6. Setup Firewall

  • Check status of firewall:
ufw status
  • Enable firewall:
ufw enable
  • Enable SSH with firewall:
ufw allow ssh
  • Check firewall status again:
ufw status
  • Allow HTTP through port 80:
ufw allow http
  • Allow HTTPS through port 443:
ufw allow https
  • Check firewall one more time:
ufw status

7. Install/Configure NGINX

  • Install NGINX:
sudo apt install nginx
  • Open the NGINX config file:
sudo nano /etc/nginx/sites-available/default
  • Edit the NGINX config file:
# /etc/nginx/sites-available/default
server {
  ...
  #server_name _; #if not using a custom domain
  server_name your_domain.com www.your_domain.com; #if using a custom domain
  ...
  location / {
          # First attempt to serve request as file, then
          # as directory, then fall back to displaying a 404
          proxy_pass http://localhost:5000; #whatever port your app runs on
          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;
  }
}
  • If running a GraphQL server, you may want to add the following:
# /etc/nginx/sites-available/default
server {
    ...
    location /graphql {
        proxy_pass http://localhost:4000/graphql;
    }
    ...
}
  • Save the NGINX config file:
# Write out the file
ctrl+O
# Do not modify filename
return/enter
  • Exit nano:
# Exit nano
ctrl+X
  • Confirm changes were saved successfully:
sudo nginx -t
  • Restart NGINX service:
sudo service nginx restart
  • Open a web browser to the server's IP Address (or domain, if applicable) and confirm the Node app loads as expected.

8. Connect Domain to Server (if available)

Coming soon.

9. Add SSL using Let's Encrypt

  • Add the PPA:
sudo add-apt-repository ppa:certbot/certbot
  • Run any necessary updates:
sudo apt update
  • Install python-certbot-nginx:
sudo apt-get install python-certbot-nginx
  • Add certbot to the app domain:
sudo certbot --nginx

If certbot asks to redirect all requests to HTTPS, you may choose that option.

  • Test the SSL renewal process (SSL certificate is only valid for 90 days):
certbot renew --dry-run

10. All done!

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