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 up configuration and connect your repository you are done, I mean the manual setup of a droplet to run a Golang MongoDB app.
This might seem retrogressive but it comes in handy for small apps and websites.
With the introduction of CICD, some platforms require very few configurations and you are up and running.
Future updates require you to push updates to your repository and those platforms pull the changes and update the system, or the website. CICD( continuous delivery, continuous deployment) at its fullest.
Such platforms include Vercel, Netlify among others for frontend, fly.io is a platform for backend developers(Golang)
With that said let us delve into a manual setup of a digital ocean server to run a Golang MongoDB app
Steps to set up of a digital ocean server to run a Golang MongoDB app
step 1: create a Digital ocean server and create a user to work with.
login with the root user
ssh root@ip
then enter your password(don't worry when nothing happens, it just is a security measure to ensure that no one peeping can see the length of you password)
Add user:
adduser newuser
Adding the New User to the Sudo Group
groups newuser
It will prompt you to add a few other details like password and the like - just do it
By default, a new user is only in their own group because adduser creates this in addition to the user profile. A user and its own group share the same name. In order to add the user to a new group, you can use the usermod command:
usermod -aG sudo newuser
The -aG option tells usermod to add the user to the listed groups.
The user needs access to Digital Ocean to do stuff
rsync --archive --chown=newuser:newuser ~/.ssh /home/newuser
Step 2: Log in with the created user and update the droplet "sudo apt update"
login to your server
ssh newuser@your_server_ip
and enter the password.
the run :
sudo apt update
step 3: Install node and npm
run the following:
领英推荐
sudo apt update
Then install Node.js:
sudo apt install nodejs
then install npm
sudo apt install npm
With those installed, it is easier to install other applications to ensure the application is running forever on the server(pm2) and is exposed to the outside world (nginx)
Step 4: install Nginx
sudo apt update
sudo apt install nginx
adjust the firewalls
sudo ufw app list
sudo ufw allow 'Nginx HTTP'
check whether nginx is running correctly
systemctl status nginx
Step 5: Install pm2
sudo npm install pm2 -g
step 6: install certbot
So why install certbot?
Certbot ensures the domain added is secure the s in the HTTP(s)://example.com.
and of course you have to do some configurations:
sudo snap install core; sudo snap refresh core
sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot
Obtaining an SSL Certificate
sudo certbot --nginx -d example.com -d www.example.com
with that, we have prepared the platform for hosting the Golang app and MongoDB
In the next article, we will continue to have the Golang app and Mongo running and exposed to the outside world.
I hope you found this article resourceful.
Open Source Software Analyst / Programmer at Catalyst IT Limited
1 年Never heard of pm2 but yeah some things I do: * Make sure each release is versioned (I use Docker images that are tagged) * Make sure you can roll back to a previous version * Know exactly what goes into your deploy or image (use lockfiles) * Run your app as a dedicated user rather than root, even inside your Docker images * Make sure that user isn't a regular user with a homedir in /home, create a proper system user instead with no shell or login I see people throwing code in /home/username and that is quite amatuerish, though at some point I would have done the same. Deployments are hard.