Playing with Server

Aman Kumar
2 min readSep 13, 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 a certificate for free from let’s encrypt?

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

To provide domain by yourself

sudo certbot --nginx -d example.com -d www.example.com

How to Deploy a static react build in VM?

Here the website name is example.com

  1. Go to cd /etc/nginx/sites-available
  2. Create a new file example.com nano example.com
  3. Add below details. Replace the website with correct values
server{
root /var/www/example.com/html;
index index.html;
location / {
try_files $uri $uri/ =404;
}
gzip on;
gzip_types application/javascript image/* text/css;
gunzip on;
server_name example.com www.example.com;
}

4. Create a symlink for it:

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/example.com

Go to the project repo

mkdir /var/www/example.com
cp -r build html
mv html /var/www/example.com/
chown -R $USER:$USER /var/www/example.com/html
chmod -R 755 /var/www/example.com
certbot // Get SSL certificates & Redirect http to https
nginx -t
systemctl restart nginx

To update the static files

cp -r build html
rm -rf /var/www/framery.in/html
mv html /var/www/framery.in/html

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

Moving Files within Server

# In local to copy file from remote to local
scp root@$PROFILEBUD:~/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@$PROFILEBUD:~/4_NOV_USERS.sql

Postgres Restore

psql -U postgres -W -d example_backups -f ~/Example_Dumps/students.sql # Short Command
psql instagram_tools_backend < 4_NOV_USERS.sql

Check the IP

GET: https://httpbin.org/ip

Sample Dummy JSON Data

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

--

--