Hosting a Golang MongoDB app on a droplet(digital ocean): part 2
Anthony Miracho
Senior Golang Developer | Full Stack Developer(Golang & Reactjs) | DevOps | Website Developer
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
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.
Senior Backend Software Engineer | Golang | PHP
9 个月Join our Golang Gopher ??? Group #golanggopher https://www.dhirubhai.net/groups/8161778
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.