Deploy a Golang App
How to Deploy a Golang Application on a VPS, Cloud, or Dedicated Server Using Caddy
In this guide, we'll walk through the process of deploying a Go application on a VPS, cloud, or dedicated server with Caddy as your web server and reverse proxy. Whether you're running your application on DigitalOcean, AWS, or a dedicated physical server, the steps remain largely the same.
Setting Up Your Server Environment
To start, you need access to a server. Whether it's a VPS from providers like DigitalOcean, Linode, or Vultr, a cloud server from AWS, Google Cloud, or Azure, or a dedicated machine, the steps are generally similar. After provisioning your server, you'll be given an IP address and credentials to log in via SSH.
Once you have the server information, open your terminal and SSH into the machine. For example:
ssh root@your_server_ip
After logging in, it's always a good idea to update your server's package manager and install any available updates. Run the following commands to ensure your server is up to date:
sudo apt update
sudo apt upgrade
Once the server is updated, it’s good practice to create a non-root user with sudo privileges for managing your application and server configuration. This increases security by limiting direct access to the root account.
Installing Go on the Server
Before deploying your Go application, you'll need to install Go on your server. This can be done by downloading the official Go binary and extracting it to your system’s /usr/local
directory. First, download the latest version of Go by running:
wget https://golang.org/dl/go1.20.2.linux-amd64.tar.gz
Next, extract the tar file to /usr/local
:
sudo tar -C /usr/local -xzf go1.20.2.linux-amd64.tar.gz
To make Go available globally, you’ll need to update the system’s PATH variable. Open the .profile
or .bashrc
file in your home directory and add Go’s binary path:
export PATH=$PATH:/usr/local/go/bin
After saving the file, reload the terminal session:
source ~/.profile
You can verify the installation by running go version
, which should return the installed version of Go.
Building Your Go Application
If you haven’t already built your Go application, you’ll need to upload it to your server. You can use a tool like SCP (Secure Copy) to transfer your local Go project files to the server. For example:
scp -r /path/to/your/go-app root@your_server_ip:/home/yourusername/
Once the files are on your server, navigate to the directory where you uploaded your Go application and build it into a binary. For example:
cd /home/yourusername/go-app
go build -o app
This will compile your Go application into an executable binary called app
. You can run this binary directly using the following command:
./app
Your Go application should now be running on the server and listening on a port, such as localhost:8080
. To access the application, visit http://your_server_ip:8080
in your browser, and you should see your app running.
Installing and Configuring Caddy
Caddy is a powerful and easy-to-use web server that simplifies deployment with automatic HTTPS and an intuitive configuration system. To install Caddy, begin by running the following commands to download and install it from the official repository:
sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo tee /etc/apt/trusted.gpg.d/caddy-stable.asc
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list
sudo apt update
sudo apt install caddy
Once installed, Caddy will be ready to serve your application and handle HTTPS automatically.
Setting Up Caddy as a Reverse Proxy
Now that Caddy is installed, it can serve as a reverse proxy for your Go application. A reverse proxy forwards client requests to another server (in this case, your Go app) while providing features like HTTPS termination, load balancing, and caching.
First, stop your Go app from running manually by pressing Ctrl + C
. Instead, we will run it in the background using Caddy.
To configure Caddy, create a new Caddyfile in /etc/caddy/Caddyfile
. This file will define how Caddy handles requests. Open the file with your preferred text editor:
sudo nano /etc/caddy/Caddyfile
In the Caddyfile, configure your domain or server IP to proxy requests to your Go application:
yourdomain.com {
reverse_proxy localhost:8080
}
Replace yourdomain.com
with your actual domain or the server’s IP address. This configuration tells Caddy to listen for requests on yourdomain.com
and forward them to localhost:8080
, which is where your Go application is running.
Save the file and exit the editor. Now, you can restart Caddy to apply the new configuration:
sudo systemctl restart caddy
Caddy will automatically handle HTTPS for your domain, acquiring an SSL certificate from Let’s Encrypt if needed. If you visit https://yourdomain.com
in your browser, you should now see your Go application served securely with HTTPS.
Running Your Go Application as a Service
To ensure your Go application continues to run in the background and restarts automatically if the server reboots, you should configure it to run as a systemd service.
Create a new systemd service file for your Go application by running:
sudo nano /etc/systemd/system/goapp.service
In the file, define your Go application’s service like this:
[Unit]
Description=Go Application
[Service]
ExecStart=/home/yourusername/go-app/app
Restart=always
User=yourusername
WorkingDirectory=/home/yourusername/go-app
[Install]
WantedBy=multi-user.target
Save the file and exit. Enable and start the service using these commands:
sudo systemctl enable goapp
sudo systemctl start goapp
Your Go application will now run in the background and restart automatically if the server goes down or restarts.
Configuring DNS for Your Domain
If you want to access your Go application via a domain, you'll need to configure DNS settings. Log in to your domain registrar's control panel (such as GoDaddy, Namecheap, etc.), and create an "A" record pointing to your server’s IP address. For example:
- Host: @
- Type: A
- Value: Your server’s IP address
- TTL: 30 minutes
Once DNS propagation is complete, which may take a few minutes to several hours, your domain should resolve to your server, and you’ll be able to access your Go application via https://yourdomain.com
.
Testing and Monitoring Your Deployment
Once your Go application is live, it's essential to ensure it’s running smoothly. Use tools like top
or htop
to monitor CPU and memory usage on your server. Additionally, you can check Caddy’s access logs located at /var/log/caddy/access.log
to see how your application is performing.
Consider using monitoring services like Prometheus and Grafana to collect and visualize performance metrics, or set up basic alerts with uptime monitoring services like UptimeRobot to be notified if your application goes down.
Conclusion
Deploying a Go application on a VPS, cloud server, or dedicated server using Caddy is a streamlined and efficient way to get your app up and running with minimal hassle. With Caddy handling SSL certificates and reverse proxying, you can focus on building and improving your application rather than managing server configurations.
By following the steps in this guide, you can confidently deploy your Go application, scale it as needed, and serve it securely to users around the world. Whether you're deploying on a small VPS for a side project or using a powerful dedicated server for a production environment, Go and Caddy make the process simple and reliable.