Skip to content

Instantly share code, notes, and snippets.

@Sinequanonh
Forked from dotspencer/setup.md
Created October 25, 2023 06:11
Show Gist options
  • Save Sinequanonh/93c4879ae42e86fb3a6d181c52a93b44 to your computer and use it in GitHub Desktop.
Save Sinequanonh/93c4879ae42e86fb3a6d181c52a93b44 to your computer and use it in GitHub Desktop.
DigitalOcean droplet setup with Nginx and Node (and general Ubuntu server and firewall ufw setup)

Parts taken from:
https://www.digitalocean.com/community/tutorials/initial-server-setup-with-ubuntu-16-04
https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-ubuntu-16-04
https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-16-04
https://www.digitalocean.com/community/tutorials/ufw-essentials-common-firewall-rules-and-commands

Create droplet in DigitalOcean dashboard
(do not add SSH key during setup)

local:

ssh root@ip_address
(default password in email)

server:

adduser brooke

usermod -aG sudo brooke

logout

local:

ssh-copy-id brooke@ip_address

ssh brooke@ip_address

server:
ssh-keygen

cat .ssh/id_rsa.pub
(copy result)

Go to: https://github.com/settings/keys

Click 'New SSH Key'

Enter title: 'Digital Ocean ...'

Paste in key

Click 'Add SSH key'

on server:

Setup

Install emacs

sudo apt install emacs24

General Update

sudo apt-get update
sudo apt-get dist-upgrade
sudo apt-get autoremove
sudo reboot

ssh brooke@ip_address

Enable colorful output

In .bashrc, uncomment line:

force_color_prompt=yes

Needed installs...

sudo apt update
sudo apt install build-essential libssl-dev
sudo apt install ssh (to add 'OpenSSH' profile)

Install Node.js (using NVM)

Updated command at: https://github.com/creationix/nvm/#install-script

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.2/install.sh | bash

[logout and log back in]

nvm ls-remote
(pick version)

nvm install v8.1.4

Install Nginx

sudo apt-get update
sudo apt-get install nginx

UFW (Uncomplicated Firewall)

sudo ufw app list
(shows firewall options)

sudo ufw allow 'Nginx Full'
sudo ufw allow 'OpenSSH'

sudo ufw status

// Only to remove previously
sudo ufw delete allow 22

sudo ufw enable

DDNS (using Google Domains)

sudo apt install ddclient
(intial setup walkthrough screens don't matter)

Edit /etc/ddclient.conf file to following

# Configuration file for ddclient generated by debconf
#
# /etc/ddclient.conf

protocol=dyndns2
use=web
server=domains.google.com
ssl=yes
login=<username-in-ddns-settings>
password=<password-in-ddns-settings>
<subdomain-or-domain>.com

Then run:

sudo rm /var/cache/ddclient/ddclient.cache
sudo service ddclient restart
sudo ddclient

NGINX

sudo systemctl start nginx
sudo systemctl stop nginx
sudo systemctl restart nginx
sudo systemctl reload nginx
sudo systemctl disable nginx (disable run on startup)
sudo systemctl enable nginx (enable run on startup)

sudo nginx -t (test configuration)

curl icanhazip.com (type ip result into browser and you should see "Welcome to Nginx" page)

Access nginx logs: /var/log/nginx/access.log Access nginx errors: /var/log/nginx/error.log

Application Setup

npm install -g pm2

pm2 startup systemd
(then run the command returned at bottom)

cd
mkdir apps
cd apps/

git clone <repo_url>

npm install --production

HTTPS setup (LetsEncrypt)

sudo add-apt-repository ppa:certbot/certbot

sudo apt-get update

sudo apt-get install certbot

certbot --version

Add an 'A record' for the domain that points to the droplet (ttl to 300) After 5 minutes...

sudo systemctl stop nginx

sudo certbot certonly

Pick option: 1: Spin up a temporary webserver (standalone) Fill out rest of info...

Check files exist:

sudo ls -l /etc/letsencrypt/live/domain_name_here

Create configuration snippet for SSL key and cert (optional but recommended)

sudo emacs /etc/nginx/snippets/ssl-effortlessreviews.com.conf

ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

Create configuration snippet for strong encryption settings

sudo emacs /etc/nginx/snippets/ssl-params.conf

Add files like modified default file and effortlessreviews.com to /etc/nginx/sites-available/

Make symlinks to those (.com) files in /etc/nginx/sites-enabled/

cd /etc/nginx/sites-enabled/
sudo ln -s ../sites-available/effortlessreviews.com

Starting app

Create keys.json file if required

Allow droplet ip access to database

pm2 start src/app.js --name 'name_of_app'

pm2 list

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