Using bun on server

From Logic Wiki
Jump to: navigation, search


Installation

Install unzip if it's not installed already

sudo apt install unzip

Install bun

curl -fsSL https://bun.com/install | bash

Add Bun to your PATH (it may already been added. just run (source ~/.bashrc) first

 echo $SHELL

it gives us what type of shell we use and open your shell configuration file with an editor

  • For bash: ~/.bashrc
  • For zsh: ~/.zshrc
  • For fish: ~/.config/fish/config.fish

Add the Bun directory to PATH

Add this line to your configuration file:

export BUN_INSTALL="$HOME/.bun"
export PATH="$BUN_INSTALL/bin:$PATH"

run based on your shell

source ~/.bashrc

Test it Manually

In CI/CD I create and encrypt .env file by using

bunx dotenvx encrypt

I placed .env and .env.keys under a project folder and save index.js in dist folder.

In the project folder run the application by

 bun dist/index.js

Run the API as a Background Service (PM2 – Recommended)

Install / upgrade pm2 by running

npm install -g pm2

Start your Bun API

pm2 start "bun dist/index.js" --name logic_logger_api

Save PM2 startup config

pm2 save
pm2 startup

IMPORTANT : READ THE MESSAGES IT MAY ASK YOU TO SETUP START UP SCRIPT LIKE

sudo env PATH=$PATH:/home/logicmade/.nvm/versions/node/v22.15.0/bin /home/logicmade/.nvm/versions/node/v22.15.0/lib/node_modules/pm2/bin/pm2 startup systemd -u logicmade --hp /home/logicmade

Check status

pm2 status
pm2 logs logic_logger_api

Confirm API Is Listening Locally

curl http://localhost:3020/test

Create NGINX Server Block

Create config file

sudo nano /etc/nginx/sites-available/logic_logger

Paste

server {
    listen 80;
    server_name api.bookitfor.me.uk;

    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_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;

        proxy_cache_bypass $http_upgrade;
    }
}
== Enable the Site ==
<pre>
sudo ln -s /etc/nginx/sites-available/logic_logger /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx

Open Firewall (If Enabled)

sudo ufw allow 'Nginx Full'
sudo ufw reload

Install HTTPS (Let’s Encrypt)

Install Certbot

sudo apt update
sudo apt install certbot python3-certbot-nginx -y

Issue SSL Certificate

sudo certbot --nginx -d api.bookitfor.me.uk

Auto-Renew SSL (Important)

Certbot installs a timer automatically. Verify:

sudo systemctl status certbot.timer

Test renewal:

sudo certbot renew --dry-run

Final Folder Permissions (Recommended)

sudo chown -R logicmade:logicmade /home/logicmade/bookitfor.me.uk
chmod -R 755 /home/logicmade/bookitfor.me.uk



See Also Bash script which uses sudo