❗ATTENTION: Don't forget to provide proper values, ports, IP address, DSN etc.
❗ATTENTION 2: Use free 200$ credits in DO using this button at the end of the main page.
There are two ways how to deploy Golang apps to DO. Despite the way you choose, you need Postgres, so create managed Postgres on DO according this quickstart.
- Choose Frankfurt as a datacenter region.
- Select the cheapest plan.
- On the page of created database cluster you can retrieve connection information.
- Using this connection settings you can connect to DB using Datagrip or similar tool.
- Run migrations from your local machine
migrate -path=./path/to/migrations -database="dsn_from_digital_ocean" up
- Create the cheapest droplet with the latest Ubuntu using password or SSH key. Here is the manual.
- Get DB DSN string
- Build Use command
GOARCH=amd64 GOOS=linux go build -o app-linux ./path/to/your/app/entry/point
because we need binary for Linux. (-o means output) - Upload prebuilt app from your machine to new created Droplet using
scp ./built-file root@your-server-ip:/root
. - Set your binary executable
chmod -x ./app-linux
- Run built app locally using
-dsn
flag./app-linux -dsn "dsn_string_from_digital_ocean"
. And, you can provide other flags. - Try to check access to your app using Droplet IP address. E.g.,
http://100.200.300.400:8080/api/v1/healthcheck
.
- Go to App platform page in DO.
- Press Create App.
- Select service provide Github.
- Allow all needed access.
- Select proper repository, branch and folder. For demo-app it is main and
/
. - Keep autodeploy checked.
- Configure resources. (NOTE❗: delete one of web services, leave only one)
- Add and configure previously created database.
- Configure DSN and press Save. (NOTE ❗: your app should read envs like in demo app)
- Choose Frankfurt in Region select.
- Review everything, then submit.
- Wait for building.
- Check given URL with your params, https://dolphin-app-qdx7b.ondigitalocean.app/api/v1/healthcheck.
- Hooray! 🎉
For a more robust setup, especially for production environments, you might want to configure your application to run as a system service using systemd. Here’s how you can do it:
- Create a systemd service file:
sudo nano /etc/systemd/system/your_service_name.service
- Add the following configuration to the file:
[Unit]
Description=Go Application Service
After=network.target
[Service]
User=username
Group=usergroup
ExecStart=/path/to/directory/binary_name
EnvironmentFile=/path/to/directory/.env
Restart=always
[Install]
WantedBy=multi-user.target
Replace your_service_name
with a name for your service, username
and usergroup
with the user and group under which the service should run, and adjust the ExecStart
path to point to your binary.
In our case it should look like this.
User=root
Group=root
ExecStart=/root/binary_name
ExecStart=/root/.env
Don't forget to create .env
file with proper values.
touch .env
nano .env
.env
example
DSN=path_to_DSN
PORT=8080
- Reload systemd to read the new service file:
Add sudo
if user is not root
.
systemctl daemon-reload
- Start the service:
systemctl start your_service_name.service
- Enable the service to start on boot:
systemctl enable your_service_name.service
- Check the status of your service:
systemctl status your_service_name.service
This will ensure that your Go application starts automatically at boot and restarts if it ever crashes.
Ensure to monitor the application logs and system performance. You can check logs of a systemd service with:
journalctl -u your_service_name.service
This setup provides a basic foundation for running a Go application on a server, ensuring it's secure, stable, and restarts automatically if needed.
Thanks for ChatGPT 🤖 for base instruction that I have modified a bit. :)