Skip to content

Instantly share code, notes, and snippets.

@kitsune7
Last active July 25, 2020 02:07
Show Gist options
  • Save kitsune7/d705a4429a99a4b4cacb8d4ed15840ff to your computer and use it in GitHub Desktop.
Save kitsune7/d705a4429a99a4b4cacb8d4ed15840ff to your computer and use it in GitHub Desktop.
How to setup a new remote server on Digital Ocean

Setting up a DigitalOcean Web Server

This isn't an extensive guide, but rather a reference of commands, reminders, and references that can hopefully save a lot of time.

Important Note: In each piece of code that has uppercase text, swap out that text with what it describes.

Helpful links:

1. Add a new droplet on DigitalOcean's website

Reminders:

  • Make sure to select the $5 one if you don’t already know it needs to be bigger.
  • Be sure to add at least one ssh key so you can login
  • Refer to this guide if you don’t have an existing ssh key you want to add
  • Make sure you have a secure firewall in place for the droplet (only open ports that are vital for the server to run or be maintained [eg. SSH, HTTPS])

2. Setup your main user

ssh -i ./PRIVATE_KEY root@DROPLET_IP_ADDRESS
adduser USERNAME
usermod -aG sudo USERNAME
rsync --archive --chown=USERNAME:USERNAME ~/.ssh /home/USERNAME

3. Login via ssh as your new user and update

ssh -i ./PRIVATE_KEY USERNAME@DROPLET_IP_ADDRESS
sudo apt update
sudo apt upgrade -y
reboot

4. Point your domain name at the server’s ip address

Create an A record for your domain name on your domain name registrar Host: @ Value: DROPLET_IP_ADDRESS Remove any records that might conflict.

5. Install git, node, and npm

sudo apt install git nodejs npm
nodejs -v # verify nodejs is installed correctly
npm -v # verify npm is installed correctly

To get the latest stable version of Node.js as simply node instead of nodejs, install n globally with sudo.

sudo npm install -g n
sudo n stable
node -v # Verify that node is installed correctly

6. Setup npm so admin priviledges aren't needed to install packages globally

Based off of this.

mkdir "${HOME}/.npm-packages"
npm config set prefix "${HOME}/.npm-packages"
  • Add the following to .bashrc with nano or vim
NPM_PACKAGES="${HOME}/.npm-packages"
export PATH="$PATH:$NPM_PACKAGES/bin"
export MANPATH="${MANPATH-$(manpath)}:$NPM_PACKAGES/share/man"

Run source ~/.bashrc to apply changes to your current terminal session. Keep in mind if you're using an automated CI/CD task runner from a service like GitLab, you may need to add it to your path to get it to recognize globally installed npm packages (export PATH="$PATH:${HOME}/.npm-packages/bin").

Go ahead and test it by installing yarn and pm2.

npm install -g yarn pm2@latest

If it was done properly, they should install without any issues.

Note: pm2 isn't needed if you just want to host your static code or static code output rather than a server. Nginx will handle serving your code by itself.

7. Setup your project

cd LOCATION_WHERE_YOU_WANT_YOUR_CODE # I just use my home directory
git clone CODE_REPO_URL

Install dependencies with yarn and run any setup or build scripts that are needed.

If you don't want to enter your git credentials every time you do something with a private git repository you can tell git to store the credentials with git config credential.helper store. After the next command that requires authentication like git pull, it shouldn't ask anymore.

Also, don't forget to manually add any .env files your server needs. It's very easy to forget and causes a lot of confusion and frustration if you do forget.

If you're running a server

You should be able to test that your code is running properly with npm start now. If your firewall is open on the port your code runs, you could even make requests to it now on the IP address of the droplet. If it is open for this, after you've verified your code is running, I recommend tightening your firewall security so that the only ports that are open are completely necessary to run and maintain the server.

To persist your server on startup we'll use pm2. If you're hosting static code, Nginx will handle serving your code rather than pm2. Install pm2 globally with npm if you haven't already.

pm2 startup systemd

The command above will print out another command that will setup pm2 on startup. Run that.

This next piece of code adds your program to what pm2 is currently running and then saves the list so that it will start the same programs when it restarts. This assumes your program can be started with npm start.

pm2 start --name "NAME_OF_YOUR_PROGRAM" npm -- start
pm2 save

8. Setup Nginx

Now we're ready for Nginx!

sudo apt install nginx

Add a new server block to Nginx by creating a file named after the domain/subdomain name you're using for this website or web server.

sudo vim /etc/nginx/sites-available/example.com

It should look something like this when Nginx is used as a proxy for a server running locally:

server {
  server_name example.com;

  location ~ / {
    proxy_pass http://localhost:8080;
  }

  listen 80;
  listen [::]:80;
}

Or like this for a static site:

server {
  server_name example.com;

  location ~ / {
    root PROJECT_DIRECTORY; # like /home/USERNAME/PROJECT/dist or something similar
    try_files $uri /index.html; # Directs everything to index.html if it can't find an html file for it
  }

  listen 80;
  listen [::]:80;
}

After you've saved the file, make sure to test it with sudo nginx -t. You can activate it by creating a symlink in /etc/nginx/sites-enabled/ for it. You'll then need to restart Nginx to have the changes take effect.

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo systemctl nginx restart

Nginx should now be serving your domain name.

9. Setup HTTPS

Commands from come from here.

sudo apt install python-certbot-nginx
sudo certbot --nginx -d example.com
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment