Deploying a Simple Go Web Server as a Systemd Service
In this tutorial, we’ll walk through the process of creating a simple web server in Go, compiling it into a binary, and deploying it as a systemd service on a Linux machine. This approach ensures that your Go application starts automatically on boot and restarts if it crashes.
Step 1: Writing the Go Web Server
First, let’s create a simple Go web server that responds with a JSON message:
package main
import (
"encoding/json"
"net/http"
)
// Response structure
type Response struct {
Message string `json:"message"`
}
// Handle the request and respond with a success message
func handleRequest(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
response := Response{Message: "Success"}
json.NewEncoder(w).Encode(response)
}
func main() {
// Define the route for the GET request
http.HandleFunc("/", handleRequest)
// Start the server on port 8080
http.ListenAndServe(":8080", nil)
}
This code sets up a basic HTTP server that listens on port 8080. When accessed via a GET request, it responds with a JSON object containing a "message": "Success"
.
Step 2: Compiling the Go Application
Next, compile the Go application into a binary executable. This can be done using the go build
command:
go build -o myapp main.go
This command compiles the Go code and outputs an executable named myapp
.
Step 3: Moving the Binary to /usr/local/bin/
To make the application accessible system-wide, move the binary to /usr/local/bin/
:
sudo mv myapp /usr/local/bin/
Step 4: Creating a Systemd Service
Now, let’s create a systemd service file to manage the application. Open a new file in the /etc/systemd/system/
directory:
sudo nano /etc/systemd/system/myapp.service
Add the following content to the file:
[Unit]
Description=My App
After=network.target
[Service]
WorkingDirectory=/usr/local/bin
ExecStart=/usr/local/bin/myapp
Restart=on-failure
User=nobody
Group=nogroup
Environment=PORT=8080
[Install]
WantedBy=multi-user.target
This configuration defines a systemd service that runs our Go application. It specifies that the service should start after the network is up, run as the nobody
user, and restart automatically if it fails.
Step 5: Starting and Enabling the Service
Reload the systemd manager configuration to recognize the new service:
sudo systemctl daemon-reload
Start the service with:
sudo systemctl start myapp
To ensure that the service starts automatically on boot, enable it:
sudo systemctl enable myapp
Step 6: Verifying the Service Status
You can check the status of the service to ensure it’s running correctly:
sudo systemctl status myapp
Step 7: Testing the Application
Finally, test the application to make sure it’s working:
curl localhost:8080
You should see the following JSON response:
{
"message": "Success"
}
Conclusion
By following these steps, you’ve successfully written, compiled, and deployed a simple Go web server as a systemd service. This setup ensures that your application runs in the background and automatically restarts if it crashes. This is a solid foundation for deploying more complex Go applications on a Linux server.