Note that commands that starts with "$" refers to your local pc (eg using git bash) while "#" refers to the server
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
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. 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 the new created user root privilege
# usermod -aG sudo ujuser
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
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 using:
$ exit
You will be logged in since you have added SSH, no password will be required
$ ssh ujuser@YOUR_SERVER_IP
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
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 to ensure the changes to the config file takes effect
# sudo systemctl reload sshd
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
# sudo ufw enable
# sudo ufw status
We are now done with the firewall.
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 and its dependency packages.
sudo apt-get install postgresql postgresql-contrib
We create a database called "ujfarms" and user called "ujfarmadmin"
sudo -u postgres psql
We create a database called "ujfarms". Do not forget to add ";" at the end of the line.
CREATE DATABASE ujfarms;
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!';
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;
We are done creating our database and user; we can now exit from the postgres console
\q
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
nano ~/.profile
Then add the following line to the end of the file.
export PATH=$PATH:/usr/local/go/bin
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
Replace "..." with your github username
git clone https://github.com/.../gofarm.git
Build your go application. E
cd /go/src/gofarm
go build
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
sudo service gofarm start
sudo service gofarm status
Install nginx. This will ensure we can call our application IP in the browser without adding the port after configuration
sudo apt install nginx
Ensure nginx is added to our firewall
sudo ufw app list
sudo ufw allow "Nginx Full"
sudo ufw reload
sudo ufw status
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
Ensure that the nginx configuration is ok by using:
sudo nginx -t
Link configuration to site-enable
sudo ln -s /etc/nginx/sites-available/gofarm /etc/nginx/sites-enabled/gofarm
Restart nginx
sudo systemctl restart nginx