RestlessMonks’ Cookbook

Abhinav Rai
3 min readMay 26, 2020

How to run rails server (or any server) on a VM?

  1. Make sure nginx is installed. Else follow this simple guide first — https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-ubuntu-18-04
  2. Go to cd /etc/nginx/sites-available
  3. Add below details. Replace your server_name and proxy_pass (Where your server is running) with correct values
server{
server_name api.grambuddy.com;
location / {
proxy_pass http://localhost:3002;
proxy_http_version 1.1;
}
listen 80;
listen [::]:80;
}

4. Create a symlink for it:

sudo ln -s /etc/nginx/sites-available/testapp /etc/nginx/sites-enabled/testapp

5. Restart Nginx:

sudo nginx -s reload

How to run service?

cd /etc/systemd/system/<Create a new file - grambuddy-server.service> and enter the below details in it[Unit]
Description= instance to start grambuddy backend server
After=network.target
[Service]
User=root
Group=www-data
WorkingDirectory=/root/grambuddy
Restart=on-failure
RestartSec=5s
ExecStart=/bin/bash -lc 'bundle exec rails server -e production -p 3002'
[Install]
WantedBy=multi-user.target

Then systemctl start grambuddy-server.service

How to make an endpoint http“s” / get certificate for free from let’s encrypt?

  1. Ref: https://certbot.eff.org/
certbot{and then choose the domain from the nginx}

How to Deploy a static react build in VM?

Here website name is framery.in

cp -r  build htmlmv html /var/www/framery.in/htmlchown -R $USER:$USER /var/www/framery.in/htmlchmod -R 755 /var/www/framery.innginx -tsystemctl restart nginx

How to copy Postgres data to a CSV and then to your computer

\copy (select * from users order by created_at desc) to '~/users_till_26_may.csv' with csv

And then

scp root@129.19.78.122:users_till_26_may.csv ~/Desktop/profilebud/data

Postgres Dump

pg_dump -h localhost -p 5432 -U postgres -d db_name -t table_name > name.sqlpsql instagram_tools_backend < 4_NOV_USERS.sql

https://www.citusdata.com/blog/2021/02/20/faster-data-migrations-in-postgres/

SCP

Moving Files within Server
# In local to copy file from remote to local
scp root@192.123.456.7:~/4_NOV_USERS.sql /home/dell/Downloads/Instagram/4_NOV_USERS.sql# In local to copy file from local to remotescp /home/dell/Downloads/Instagram/4_NOV_USERS.sql root@$192.123.456.7:~/4_NOV_USERS.sql

Check the IP

GET: https://httpbin.org/ip

Sample Dummy JSON Data

https://jsonplaceholder.typicode.com/todos/1

Postgres

Postgres Config: /etc/postgresql/10/main/postgresql.conf

Config: 
/etc/postgresql/10/main/postgresql.conf
Logs:
/var/log/postgresql/postgresql-10-main.log
Restart Service:
service postgresql restart
Get a DB Size:
SELECT pg_size_pretty( pg_database_size('leadgen') );
Get Size for Specific Table:
pg_size_pretty( pg_total_relation_size('tablename') );
Show Data Directory
postgres=# SHOW data_directory;

Mounting a volume Digital Ocean:
https://www.digitalocean.com/community/tutorials/how-to-move-a-postgresql-data-directory-to-a-new-location-on-ubuntu-16-04

RAM & Disk Usage in Ubuntu

Disk Usage

df -h

Another killer tool which I would recommend — ncdu

sudo apt-get install ncdu && sudo ncdu /

RAM Usage

htop or free -m

Increasing Digitalocean Volume and Increasing Filesystem

  • Increasing volume is easy. Just go to Digitalocean dashboard and increase the volume size. PS: That is irreversible
  • After that you need to increase the file system too. Check it out here

For RestlessMonk’s case, everytime you increase the volume, increase the filesystem too -> just do

df -h        # to check the volume
resize2fs /dev/sda

Moving Postgres Data to Volume

Nginx as Load Balancer

Remove Duplicate Users from Table W.R.T multiple Column Values

Install Oh-My-Zsh in ubuntu

Rotate Journalctl Logs

For Removing Long running queries in Postgres

World of Tmux

https://linuxize.com/post/getting-started-with-tmux/

--

--

Abhinav Rai

Buliding Engagebud | Guitarist | Traveller | Entrepreneur