Skip to content

Instantly share code, notes, and snippets.

@ujblockchain
Last active June 15, 2024 15:25
Show Gist options
  • Save ujblockchain/9152d51a574791ed95b7e4a39ae83a18 to your computer and use it in GitHub Desktop.
Save ujblockchain/9152d51a574791ed95b7e4a39ae83a18 to your computer and use it in GitHub Desktop.
Go Ubuntu Deployment

Deploy Go to ubuntu

Note that commands that starts with "$" refers to your local pc (eg using git bash) while "#" refers to the server

Creating SSH keys

Create SSH keys for use in server. If SSH key is not created you will have login details sent via mail after your droplet/server has been created.

To generate a key your computer use the following below and press enter till the end to use the default configuration

$ ssh-keygen

To view the SSH key just created:

$ cat ~/.ssh/id_rsa.pub

Copy the entire key output and add as a new SSH key when creating your digitalocean droplet

Logging to Your Server

Once your server creation is created, login to server using (no login details is required due to SSH key added during droplet creation):.

$ ssh root@YOUR_SERVER_IP

Create a new user

Create a new user. It will ask for a password, use a secure one you will remember as you will use it quite often. Once done you simply can hit enter through all the fields.

# adduser ujuser

Give root privileges

Give the new created user root privilege

# usermod -aG sudo ujuser

SSH keys for the new user

We need to setup SSH keys for the new user created. View and copy SSH Key; you do this on a new window on your local terminal. Select and copy key only, do not copy extra spaces

$ cat ~/.ssh/id_rsa.pub

Go back to the previous terminal were you are login in as the root user on the server. If you have logout, login using (only run if you are logged out):

$ ssh root@YOUR_SERVER_IP

Add SSH key for new user

Follow the steps below to add SSH for the user created. Remember the user we are using is "ujuser".

# cd /home/ujuser
# mkdir .ssh
# cd .ssh
# sudo nano authorized_keys
Paste the key and hit "ctrl-x", then hit "y" to save and press "enter" to exit

Logout of the server

Logout of the server using:

$ exit

Login back using the new user

You will be logged in since you have added SSH, no password will be required

$ ssh ujuser@YOUR_SERVER_IP

Disable root login

For security, we need to disable logging with root. To do this, we need to open sshd configuration using:

# sudo nano /etc/ssh/sshd_config

Change the following

Ensure the "PermitRootLogin" and "PasswordAuthentication" are changed to "no"

PermitRootLogin no
PasswordAuthentication no

Once done, hit "ctrl-x", then hit "y" to save and press "enter" to exit

Reload sshd service

Reload sshd to ensure the changes to the config file takes effect

# sudo systemctl reload sshd

Simple Firewall Setup

We need to set up a firewall but first, we need to see which apps are registered with the firewall

# sudo ufw app list

Then Allow OpenSSH

### sudo ufw allow OpenSSH

Enable firewall

# sudo ufw enable

To check firewall status

# sudo ufw status

We are now done with the firewall.

Software

Update packages

Update and upgrade all packages in our server(Ubuntu). Copy each of the code one after the other and press enter.

# sudo apt update
# sudo apt upgrade

Install Postgres database

Install postgres database and its dependency packages.

sudo apt-get install postgresql postgresql-contrib

Access postgres console

We create a database called "ujfarms" and user called "ujfarmadmin"

sudo -u postgres psql

Create a postgres database

We create a database called "ujfarms". Do not forget to add ";" at the end of the line.

CREATE DATABASE ujfarms;

Create a postgres user with password

We create a user called "ujfarmadmin" with password "ujfarmapass12345!". Do not forget to add ";" at the end of the line.

CREATE DATABASE ujfarms;
CREATE USER ujfarmadmin WITH PASSWORD 'ujfarmapass12345!';

Give User access to database

Grant all privileges on database "ujfarms" to user "ujfarmadmin". Do not forget to add ";" at the end of the line.

GRANT ALL PRIVILEGES ON DATABASE ujfarms TO ujfarmadmin;

Exit from Postgres console

We are done creating our database and user; we can now exit from the postgres console

\q

Install go

Install go version 1.21.1 into the tmp folder. Note the files in tmp are temporary. Copy each of the code one after the other and press enter.

cd /tmp
wget https://go.dev/dl/go1.21.1.linux-amd64.tar.gz
tar -xvf go1.21.1.linux-amd64.tar.gz
sudo mv go /usr/local

Add go to path

nano ~/.profile

Then add the following line to the end of the file.

export PATH=$PATH:/usr/local/go/bin

Create directories

Create directories where are code will be. Copy each of the code one after the other and press enter.

mkdir go
cd go
mkdir src
cd src

Clone your code from your github repository

Replace "..." with your github username

git clone https://github.com/.../gofarm.git

Build Go app on remote

Build your go application. E

cd /go/src/gofarm
go build

Setup system.d service

This will ensure you application continues running in the background. Note we call the system.d file gofarm

sudo nano /lib/systemd/system/gofarm.service

Past the code below into the file.

[Unit]
Description=gofarm

[Service]
Type=simple
Restart=always
RestartSec=5s
ExecStart=/home/ujuser/go/src/gofarm/gofarm

[Install]
WantedBy=multi-user.target

Once done, hit "ctrl-x", then hit "y" to save and press "enter" to exit

Start the service and check its status

sudo service gofarm start
sudo service gofarm status

Installing nginx

Install nginx. This will ensure we can call our application IP in the browser without adding the port after configuration

sudo apt install nginx

Enabling nginx on firewall

Ensure nginx is added to our firewall

sudo ufw app list
sudo ufw allow "Nginx Full"
sudo ufw reload
sudo ufw status

Setup nginx

Create configuration for nginx. First create the configuration file in "/etc/nginx/sites-available" using:

cd /etc/nginx/sites-available
sudo nano gofarm

Paste the code below into the file. Ensure you replace "ip_address" with your droplet IP address

server {
   listen 80;
   server_name ip_address;

   location / {
     proxy_pass http://localhost:3000;
   }
}

Once done, hit "ctrl-x", then hit "y" to save and press "enter" to exit

Test NGINX config

Ensure that the nginx configuration is ok by using:

sudo nginx -t

Enable the file by linking to the sites-enabled dir

Link configuration to site-enable

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

Restart NGINX

Restart nginx

sudo systemctl restart nginx
Check Your IP on the browser, the application is working now.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment