Dev Cookbook!
All our dev cookbook - From running a server to vm to increasing the disk size to copying postgres data to csv, etc
How to run rails server (or any server) on a VM?
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
Go to cd /etc/nginx/sites-available
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?
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 html
mv html /var/www/framery.in/html
chown -R $USER:$USER /var/www/framery.in/html
chmod -R 755 /var/www/framery.in
nginx -t
systemctl 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.sql
psql 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 remote
scp /home/dell/Downloads/Instagram/4_NOV_USERS.sql root@$192.123.456.7:~/4_NOV_USERS.sql
Check the 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
How To Move a PostgreSQL Data Directory to a New Location on Ubuntu 16.04 | DigitalOcean
Nginx as Load Balancer
How to setup simple load balancing with NGINX
Remove Duplicate Users from Table W.R.T multiple Column Values
Remove duplicate records based on multiple columns?
Install Oh-My-Zsh in ubuntu
Rotate Journalctl Logs
Free up Disk Space - Clear Systemd Journal Logs in Ubuntu 20.04
For Removing Long running queries in Postgres
PostgreSQL: Find slow, long-running and Blocked Queries
World of Tmux