Digital Ocean VPS - NGINX Setup

2024-08-27

In my previous post I gave a walkthrough of setting up your own $4 server. Let’s take things to the next level by upgrading our little VPS to host multiple sites using Nginx.

1. Install Nginx

  1. Remote ssh into your droplet

  2. Go to nvm and run the install & update script

e.g. curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash

  1. Run source .bashrc to get nvm working

  2. Install node using nvm install --lts

  3. Install Nginx using apt-get install nginx

2. Update Nginx Config

Installing Nginx created a new folder /etc/nginx/

  1. cd /etc/nginx/

  2. Create a file to contain your site configuration using touch sites-available <project-name>

  3. Add the following to your file /etc/nginx/sites-available/<project-name>

server {
  listen 80;
  server_name IP_ADDR;
  location / {
    proxy_pass http://localhost:3000;
    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;
 }
}

Note: Update IP_ADDR with your ip address or domain name and update proxy_pass with correct internal port

  1. Create a symbolic link using ln -s /etc/nginx/sites-available/<project-name> /etc/nginx/sites-enabled/<project-name>

  2. Check that everything is configured correct with nginx -t

  3. Restart nginx using service nginx restart

3. Enable Firewall for Nginx

  1. See apps using ufw app list
  2. Allow OpenSSH and Nginx
    • ufw allow OpenSSH
    • ufw allow "Nginx Full"
  3. Enable ufw enable
  4. See new rules ufw status verbose
    • This shows the only ports available