Deploying a RedwoodJS site using BareMetal

Setting Up a Baremetal RedwoodJS Site

I’m in the process of re-creating a site, ai.ina.box, using RedwoodJS. My goal is to keep hosting costs low, so I’m deploying the site on a small server from Vultr. During setup, I encountered several steps worth documenting for future reference.

Project Setup for Baremetal Deployment

To begin, run the following command to set up the project for baremetal deployment:

yarn rw setup deploy baremetal

This command creates two files: deploy.toml and ecosystem.toml.

Next, you’ll need your server’s authentication details. Store the username, password, and IP address in your .env file:

# .env
DEPLOY_HOST=131.214.523.124
DEPLOY_USERNAME=deploy
DEPLOY_PASSWORD=somepass

Now, update deploy.toml with the following configuration:

[[production.servers]]
# Update the host, user, and password
host = "${DEPLOY_HOST}"
username = "${DEPLOY_USERNAME}"
password = "${DEPLOY_PASSWORD}"
agentForward = false
sides = ["api", "web"]
packageManagerCommand = "yarn"
monitorCommand = "pm2"
# Update the path
path = "/var/www/project-name-goes-here"
processNames = ["serve"]
# Update the repo
repo = "git@github.com:org/project-name-goes-here.git"
branch = "main"
keepReleases = 5

Preparing Your Server

Ensure your server is ready by following these steps: Add a new user:

adduser deploy

Give the user sudo permissions:

usermod -aG sudo deploy

Add the user to the sudoers file by running visudo and adding the following line below the root equivalent:

deploy ALL=(ALL:ALL) ALL

Allow incoming requests on ports 80 and 443:

ufw allow 80
ufw allow 443

Generate an SSH key for the server:

ssh-keygen -t ed25519 -C "your_email@example.com"

Add the SSH key to the ssh-agent:

eval "$(ssh-agent -s)"

Add the SSH key to GitHub by running:

cat ~/.ssh/id_ed25519.pub

Then, paste the output into GitHub's SSH key settings.

Add GitHub’s fingerprint by running:

ssh -T git@github.com

Create the directory specified in deploy.toml:

sudo mkdir -p /var/www/project-name-goes-here
sudo chown deploy:deploy /var/www/project-name-goes-here
# create your .env file in /var/www/project-name-goes-here/.env

Modify the Bash configuration to allow yarn calls:

# Edit .bashrc by commenting out this line:
- [ -z "$PS1" ] && return
+ # [ -z "$PS1" ] && return

Install Node.js via nvm and yarn:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
source ~/.bashrc
nvm install v20.16.0
corepack enable
yarn init -2
sudo chown deploy:deploy /home/deploy/.yarn/berry

Install PM2:

npm install pm2 -g

Install Nginx:

sudo apt install nginx

Set up Nginx:

#copy /var/www/domain/nginx.conf to 
sudo cp /var/www/project-name-goes-here/current/nginx.conf /etc/nginx/sites-available/your_domain
# link it to enabled site
sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/
# validate syntax
sudo nginx -t
#restart nginx
sudo systemctl restart nginx

Add HTTPS:

sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d example.com -d www.example.com
sudo systemctl status certbot.timer
sudo certbot renew --dry-run

Troubleshooting

Log in with the deploy user to get access to yarn

If you get a database error, you may need to clean up your records before deploy, so include a script to do that or manually plan to purge those record individually or by dropping your database (after a backup of course)