How to Deploy golang (rest api) to production using Supervisor
In one of our previous article we saw how we can deploy a golang application using systemd. While systemd is good enough, Lately I have started using Supervisord to monitor and control my apps due to some few reason:
- Any user can manage processes. No need to be superuser.
- More flexibility in managing process(groups, priority order).
This article assumes you are hosting your application on a linux based systems specifically ubuntu. To install supervisor on ubuntu run this commands.
Making A simple Go rest api
With Supervisor installed proceed to create the golang application that we will be deploying to production. For the sake of this article we will create a very simple hello world rest api.
Building and deploying our golang application using ssh
First let build a binary that we will upload. Cd to you project(where your main.go is located )and run this command to build for linux.
Note the binary produced will have the name of your parent folder name: e.g if your project is name myapp the binary will have the name myapp.
To change the output binary name when building add the -o flag like below Replace newname with the name you want to call you binary file.
After building you binary file upload it to the server. There are many ways to deploy an app to the cloud. You can use github actions ,gitlab ci/cd but ssh is the simplest way to upload an app to the server. cd to where the binary is and run the following command to upload it to the server.
- Replace binaryname with the name of your binary file,user with your server user name and example.com with you server Ip address or domain.
- /var/www/go - is the path on your server you want to store the golang binary file.You can store it on any path.Change it to the path you want to use.
Writing a supervisorctl file to manage our app.
All supervisor app configuration file are stored in /etc/supervisor/conf.d/
To add a new configuration file for our app run the following command.
Replace hello with whatever name you would like you configuration file to have. The file must have the extension .conf
Add the following content to the configurartion file.
Change the directory and command path to where you golang binary is in my case my binary is stored in the path
/home/apis/go with the name hello .
Note if you are hosting multiple apps each app should have a configuration file like above.
[program:hello] with whichever name you want to give your program e.g
Not this name must be unique for each application configuration file.
After the setup tell supervisor to reload the configuration files with the command below
You can always check the supervisor status by running:
Configuring Nginx to Proxy Requests your go rest api
When running a golang restapi like the one we made.Its good to use a proxy server behind our golang application. This makes it easier to:
Add ssl certificate to our application for example using Let’s Encrypt
It helps with load balancing.
Use a single domain to run multiple.
While we have many proxy server like apache and caddy, I use nginx since it is very easy to setup and add free ssl with Let’s Encrypt follow this article to add free ssl to nginx.
Start by installing nginx
Nginx store configuration files in /etc/nginx/sites-available
Create a new configuration file in /etc/nginx/sites-available and copy the content below:
To create the configuration file run:
Copy the content below to the new configuration file
Replace the port number with the port you used.As you recall, in my case that is port 8080.
The server_name should also be replaced with you domain name.
To enable the Nginx server block configuration we have just created,we need to create a symbolic link of our conf file to the
sites-enabled directory :
We can test for syntax errors by typing:
sudo nginx -t
If this returns without indicating any errors, we can restart the Nginx process to read our new config:
With that we are done.If I now visit my domain (https://example.com/hello) and type hello,I should see my api return
For those who would like to manage their app using systemd you can read the one of our previous tutorial In our next tutorial we will cover how we can can build and deploy a golang application using github action.