Skip to content

Instantly share code, notes, and snippets.

@giladno
Last active August 10, 2019 22:16
Show Gist options
  • Select an option

  • Save giladno/36b1a96ca3399b0dc0bee2df4ed82f5b to your computer and use it in GitHub Desktop.

Select an option

Save giladno/36b1a96ca3399b0dc0bee2df4ed82f5b to your computer and use it in GitHub Desktop.
deploy procedure to AWS

Deploy

This assumes an Ubuntu based EC2 instance.

add this line to /etc/environment; then reboot

LC_ALL="en_US.UTF-8"
sudo apt-get update && sudo apt-get upgrade
sudo apt-get install -y build-essential libssl-dev
curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | sh

Open ~/.profile and move the following lines to the top of ~/.bashrc:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm

Reboot

nvm install node
# nvm alias default node
# nvm use default
# npm install -g npm@latest

Add the following to package.json (make sure to setup git repository/target host and have proper ssh keys in ~/.ssh/config). This also assumes your server has proper deployment keys already installed:

  "scripts": {
    "deploy": "$(npm bin)/pm2 deploy package.json production",
  },
  "apps": [
    {
      "name": "Server",
      "script": "server.js",
      "env_production": {
        "NODE_ENV": "production",
        "MYVAR": "TEST"
      }
    }
  ],
  "deploy": {
    "production": {
      "env": {
        "NODE_ENV": "production"
      },
      "user": "ubuntu",
      "host": "domain.com",
      "ref": "origin/master",
      "repo": "git@github.com:giladno/demo.git",
      "path": "/home/ubuntu/deploy",
      "post-deploy": "npm install && npm run build && $(npm bin)/pm2 startOrRestart package.json --env production"
    }
  }

Push all changes and then run:

$(npm bin)/pm2 deploy package.json production setup

After deploying for the first time:

$(npm bin)/pm2 startup
...
$(npm bin)/pm2 save

(to remove startup scripts, run $ pm2 unstartup systemd)

Nginx

sudo apt-get install -y nginx
sudo ufw allow 'Nginx Full'

Paste following into /etc/nginx/sites-available/default (put any static files in /var/www). Make sure to setup proper domain name:

upstream node {
    least_conn;
    server 127.0.0.1:3000;
}

server {
  listen 80 default_server;
  listen [::]:80 default_server;

  server_name domain.com www.domain.com;
  
  root /var/www;
  error_page 404 =200 /index.html;

  location = /favicon.ico { log_not_found off; access_log off; }
  location = /robots.txt  { log_not_found off; access_log off; }
  location = / { try_files /index.html =404; }
  
  location / {
    try_files $uri @node;
  }
    
  location @node {
    proxy_intercept_errors on;
    proxy_redirect off;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass http://node;

    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_cache_bypass $http_upgrade;
  }
}

Restart Nginx:

sudo systemctl restart nginx

Make sure DNS is setup with an A record at this point!!!

Certbot

sudo add-apt-repository ppa:certbot/certbot
sudo apt-get update
sudo apt-get install -y python-certbot-nginx
sudo certbot --nginx -d domain.com -d www.domain.com
sudo openssl dhparam -out /etc/ssl/certs/dhparam.pem 2048

Enable dhparam from /etc/nginx/sites-available/default and restart

sudo systemctl restart nginx

Add autorenew task:

sudo crontab -e

Add the following line:

@daily /usr/bin/certbot renew --quiet

MySql

https://www.digitalocean.com/community/tutorials/how-to-install-mysql-on-ubuntu-18-04

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