Hosting a Golang MongoDB app on a droplet(digital ocean): part 2
hosting Golang app

Hosting a Golang MongoDB app on a droplet(digital ocean): part 2

In the last article, we prepared the platform for hosting the application but we never worked on the application itself.

in today's article, we will have Mongo running and the Golang app up and running too.

steps

Step 1: Install MongoDB on Ubuntu 22.04

The first step is to install the prerequisite packages needed during the installation. To do so, run the following command.

sudo apt install software-properties-common gnupg apt-transport-https ca-certificates -y        

To install the most recent MongoDB package, you need to add the MongoDB package repository to your sources list file on Ubuntu. Before that, you need to import the public key for MongoDB on your system using the wget command as follows:

curl -fsSL https://pgp.mongodb.com/server-7.0.asc |  sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg --dearmor        

then run:

echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list        

the update packages

sudo apt update        

now you can install MongoDB

sudo apt install mongodb-org -y        

mongo is already installed- to check it, run

mongod --version        

to start it up run

sudo systemctl start mongod        

then run

sudo systemctl status mongod        

to verify it is running correctly. if it does it should display something like this


step 2: Lets have Golang App and running

There are two ways of having it up and running

  1. Build an application and create an executable that runs online
  2. Install golang on the server and build an executable on the server

lets use the second approach to have the goApp up and running

step 1 install golang on ubuntu
sudo apt update        

then run

sudo apt upgrade -y        

to upgrade system packages, then run the following to set up Go on Ubuntu 22.04

sudo apt install golang-go -y        

then check if it is correctly installed by

go version        

now that Golang is installed successfully lets prepare a workspace for Golang app

create a directory

mkdir backend        

then navigate into that directory and copy your application into it by (zip the file and ssh it to the directory)

ssh Archive.zip username@your-ip-address:/home/username/backend        

it will prompt you for a password or paraphrase enter as requested

in the server navigate to the directory and unzip it(if you don't hap unzip package install it)

with all that done. run the Golang application to make sure it is running correctly

go run .        

if it runs correctly build it up(if it does not check for env files and other requirements for it run)

go build .        

now that the app builds correctly. let us prepare a service that will run it indefinitely

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

in it copy the following and adjust it accordingly

[Unit]
Description=goweb

[Service]
Type=simple
Restart=always
RestartSec=5s
ExecStart=/home/username/backend/goweb
WorkingDirectory=/home/username/backend

[Install]
WantedBy=multi-user.target

        

NB: WorkingDirectory provides access to .env files and other files required by the goweb app to run correctly.

ExecStart - provides the path to the Golang executable file( in this case goweb is the name of the executable)

now start the service by

sudo service goweb start        

check if the service is running correctly by

sudo service goweb status        

no the app is running on the server we need to expose it to the outside world and in our case, we will use nginx.

edit the nginx config file

sudo nano /etc/nginx/sites-available/default        

then add this block of code:

server {
        server_name backend.example.com;

    location / {
        proxy_pass  https://localhost:5500;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }


}        

well "backend.example.com" is the subdomain you have created to host the Golang app and "5500" is the port at which the application is running.

in the previous article we installed certbot, now we will have it secure the domain

by running the following

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

now with that done the app can access the MongoDB and save stuff and is exposed to the outside world.

I hope this article was helpful to you.


Basel Rabia

Senior Backend Software Engineer | Golang | PHP

9 个月

Join our Golang Gopher ??? Group #golanggopher https://www.dhirubhai.net/groups/8161778

回复
Rob van der Linde

Open Source Software Analyst / Programmer at Catalyst IT Limited

9 个月

Two things though I pick up from that and I must say I skimmed it. First of all, don't compile code on a server, you are using "go run" that means you're compiling on a server, don't do that. Ship the binary. Secondly, don't run in /home/username, create a proper system user for the app.

要查看或添加评论,请登录

Anthony Miracho的更多文章

  • What are the common Golang concurrency pitfalls?

    What are the common Golang concurrency pitfalls?

    Everyone gets excited about Go's concurrency—many even learn Go just to enjoy doing more things concurrently. It's like…

    6 条评论
  • The benefits of being an open-source contributor

    The benefits of being an open-source contributor

    An open-source contributor provides a unique opportunity for developers to enhance their skills while making a tangible…

  • How to get a Golang developer job

    How to get a Golang developer job

    People are dynamic - 2+2 in mathematics is always equal to 4, but the answer can be all over the place while dealing…

  • Possible Interview questions for a senior backend developer

    Possible Interview questions for a senior backend developer

    How would you design a scalable and resilient RESTful API? Consider a few aspects like a. Scalability: Implement load…

    11 条评论
  • Why interfaces are so important in Golang

    Why interfaces are so important in Golang

    Interfaces in Go (Golang) are very important for several reasons, mainly because they support polymorphism, decoupling,…

    4 条评论
  • How to deploy a full-stack application like an e-commerce without paying a dollar

    How to deploy a full-stack application like an e-commerce without paying a dollar

    Hi everyone, today I am gonna show you how to deploy an application, without paying a dollar - well maybe for a…

  • Hosting a Golang MongoDB app on a droplet(digital ocean)

    Hosting a Golang MongoDB app on a droplet(digital ocean)

    Preparing to host a Golang app on the cloud can be challenging and I am not talking of cloud services that once you set…

    12 条评论
  • Simulating Deadlock in Golang

    Simulating Deadlock in Golang

    Deadlock is a situation where two or more goroutines are unable to proceed because each is waiting for the other to…

    1 条评论
  • The power of Golang and go routines

    The power of Golang and go routines

    Concurrency in Go is a powerful feature that allows you to efficiently execute multiple tasks concurrently. Go has…

    7 条评论
  • How to implement a Golang cache

    How to implement a Golang cache

    Caching is a common technique used to store and retrieve frequently accessed data more efficiently. There are various…

社区洞察

其他会员也浏览了