Skip to content

Deploy Golang Application

This guide will help you deploy a Golang application using FlashPanel's Service feature for auto-restart and reverse proxy with Nginx.

Prerequisites

  • Server connected to FlashPanel
  • Domain DNS pointed to the server
  • Access to FlashPanel

Step 1: Install Go (Golang)

Use the Application feature in FlashPanel to install Go:

  1. Go to FlashPanel → Select server → Applications
  2. Find and select Go from the application list
  3. Click the Install button to install Go

After installation, open terminal to verify:

bash
/usr/local/go/bin/go version

WARNING

You only need to install Go once on the server. If you already have Go installed, you can skip this step. Check the Applications list to see if Go is already present.

Step 2: Install and Configure Database (Optional)

If your Golang application needs to connect to a database, you can install and manage databases through FlashPanel. FlashPanel supports the following database types:

  • MySQL - Popular relational database management system
  • MariaDB - Open-source fork of MySQL
  • PostgreSQL - Advanced relational database management system
  • MongoDB - NoSQL database

All of the above databases have:

Install Database

Use the Application feature in FlashPanel to install a database:

  1. Go to FlashPanel → Select server → Applications
  2. Find and select the database you want to install (MySQL, MariaDB, PostgreSQL, or MongoDB)
  3. Click the Install button to install the database

WARNING

If the database is already installed, you can skip this step. Check the Applications list to see if the database is already present.

Create Database and User

Use the Database feature in FlashPanel to create database and user:

Create Database User

  1. Go to FlashPanel → Select server → Database
  2. Click Create database user
  3. Fill in information:
    • Username: User name (e.g., myapp_user)
    • Password: Strong password for the user
    • Access scope: Select access scope (localhost or remote)
  4. Click Save to create the user

Create Database

  1. Still in the Database tab, click Create Database
  2. Fill in information:
    • Database name: Database name (e.g., myapp_db)
    • Users: Select the user created above to grant access
  3. Click Save to create the database

WARNING

Save the following information for use in the next steps:

  • Database name (e.g., myapp_db)
  • Database username (e.g., myapp_user)
  • Database password
  • Database host (usually localhost)
  • Database port (MySQL/MariaDB: 3306, PostgreSQL: 5432, MongoDB: 27017)

Step 3: Create Website with Reverse Proxy

  1. Go to FlashPanel → Select server → SitesAdd new site

  2. Fill in website information:

    • Domain: Enter your domain (e.g., api.example.com)
    • Proxy Port: Select a port for your Golang application to run on (e.g., 8080)
    • PHP Version: Not needed (this is a Golang application)

  3. Click Add site to create the website

  4. Point DNS domain to the server IP and install SSL for the website

Step 4: Upload and Build Golang Application

Upload Source Code

There are 2 ways to get source code on the server:

Method 1: Upload via File Manager in FlashPanel

  • Go to Sites → Select website → File Manager
  • Upload source code files to the website directory

Method 2: Clone from Git

Use the Source Code feature in FlashPanel to clone source code from Git:

  1. Go to FlashPanel → Select website → Source Code

  2. Choose one of the following options:

    Option A: GitHub/GitLab/Bitbucket (Recommended)

    • Select GitHub, GitLab, or Bitbucket
    • Select repository and branch you want to deploy
    • Choose Automatically add deployment key (Recommended)
    • Click Install GitHub/GitLab/Bitbucket Repository

    WARNING

    Before using GitHub/GitLab/Bitbucket, you need to connect your Git account with FlashPanel first.

    Option B: Git Repository

    • Select Git
    • Enter SSH URL of the repository (e.g., [email protected]:username/repo.git)
    • Enter branch name (e.g., main or master)
    • Choose Automatically add deployment key (Recommended) if you haven't registered a server global key
    • Click Install Git Repository

    WARNING

    Before using Git Repository, you need to add FlashPanel SSH key to your Git provider first.

  3. After installation completes, source code will be cloned to the website directory

Build Golang Application

Open terminal for the site, run the following commands:

bash
# Navigate to website directory
cd /home/flashpanel/your-domain.com

# Build application (replace main.go with your main file if different)
/usr/local/go/bin/go build -o app main.go

# Or build optimized for production
CGO_ENABLED=0 GOOS=linux /usr/local/go/bin/go build -a -installsuffix cgo -o app main.go

WARNING

  • Replace your-domain.com with your actual domain
  • Replace main.go with your application's main file if different
  • The binary file after building will be named app (or the name you specify)

Create .env File for Environment Variables

Create a .env file in the website directory to store configuration:

bash
cd /home/flashpanel/your-domain.com
nano .env

Add necessary environment variables:

properties
# Database configuration (change according to your database type)
DB_HOST=localhost
DB_PORT=3306  # MySQL/MariaDB: 3306, PostgreSQL: 5432, MongoDB: 27017
DB_USER=myapp_user
DB_PASSWORD=your_strong_password_here
DB_NAME=myapp_db
# Application configuration
APP_PORT=8080
APP_ENV=production

Save the file (Ctrl+O, Enter, Ctrl+X in nano)

Step 5: Test Run the Application

Before creating the service, test if the application runs correctly:

bash
cd /home/flashpanel/your-domain.com
./app

If the application runs successfully, you will see startup logs. Press Ctrl+C to stop the application.

WARNING

Make sure your application reads environment variables from the .env file or from system environment variables. If your application doesn't read .env, you will need to configure it in the service file in the next step.

Step 6: Create Systemd Service for Auto-Restart

Use the Systemd Service management feature in FlashPanel to create a service that automatically restarts your application.

Create Service in FlashPanel

  1. Go to FlashPanel → Select server → ServicesAdd new service

  2. Fill in service information:

    • Service Name: Service name (e.g., myapp or golang-app)
    • Description: Service description (e.g., Golang Application - api.example.com)
  3. Configure the Service as follows:

ini
[Unit]
Description=Golang Application - your-domain.com
After=network.target mysql.service
# Replace mysql.service with mariadb.service, postgresql.service, or mongod.service depending on your database

[Service]
Type=simple
User=flashpanel
WorkingDirectory=/home/flashpanel/your-domain.com
ExecStart=/home/flashpanel/your-domain.com/app
Restart=always
RestartSec=10
StandardOutput=append:/var/log/services/myapp.log
StandardError=append:/var/log/services/myapp-error.log

# Environment variables (if application doesn't read from .env)
Environment="DB_HOST=localhost"
Environment="DB_PORT=3306"  # MySQL/MariaDB: 3306, PostgreSQL: 5432, MongoDB: 27017
Environment="DB_USER=myapp_user"
Environment="DB_PASSWORD=your_strong_password_here"
Environment="DB_NAME=myapp_db"
Environment="APP_PORT=8080"
Environment="APP_ENV=production"

[Install]
WantedBy=multi-user.target
  1. Click Add service to create the service

  2. After creating the service, click the Enable button to automatically start the service when the server reboots

  3. Click the Start button to start the service immediately

WARNING

Important:

  1. Replace your-domain.com with your actual domain
  2. Replace flashpanel with the user that the website is running on (usually the domain name without dots)
  3. Replace myapp in the service name and log files with the name you chose
  4. Replace environment variable values with your actual information
  5. APP_PORT must match the Proxy Port you set when creating the website (e.g., 8080)
  6. If your application reads from the .env file, you can omit the Environment section or only add variables not in .env
  7. After=mysql.service ensures the database starts before the Golang application starts. Change the service name depending on your database (mysql.service, mariadb.service, postgresql.service, or mongod.service)
  8. DB_PORT needs to be changed according to database type: MySQL/MariaDB: 3306, PostgreSQL: 5432, MongoDB: 27017

Step 7: Check and Manage Service

View Service Status

In FlashPanel → Services, you can:

  • Start: Start the service
  • Restart: Restart the service
  • Stop: Stop the service
  • View log: View normal log
  • View error log: View error log
  • Enable/Disable: Enable/disable auto-start on server reboot

View Application Logs

To view detailed logs, you can:

  1. View logs in FlashPanel: Click View log or View error log
  2. View logs directly on the server:
bash
# Normal log
tail -f /var/log/services/myapp.log

# Error log
tail -f /var/log/services/myapp-error.log

Step 8: Test the Application

  1. Access your domain through a browser: https://your-domain.com
  2. If the application runs successfully, you will see it working
  3. If there are errors, check logs in FlashPanel or on the server

Troubleshooting

Application Won't Start

  1. Check error logs: Services → Click View error log
  2. Check executable permissions of the binary file:
bash
chmod +x /home/flashpanel/your-domain.com/app
  1. Check if the path in the service file is correct
  2. Test manual run to see errors:
bash
cd /home/flashpanel/your-domain.com
./app

Database Connection Error

  1. Check if the database is running:
bash
# MySQL/MariaDB
sudo systemctl status mysql
# or
sudo systemctl status mariadb

# PostgreSQL
sudo systemctl status postgresql

# MongoDB
sudo systemctl status mongod

Or check in FlashPanel → Applications → Select your database → View status

  1. Check connection information in the .env file or service file. Make sure the information matches the database and user created in FlashPanel → Database

  2. Test database connection manually:

bash
# MySQL/MariaDB
mysql -u myapp_user -p myapp_db

# PostgreSQL
psql -U myapp_user -d myapp_db

# MongoDB
mongo -u myapp_user -p --authenticationDatabase myapp_db
  1. If you forgot database information, you can view it again in FlashPanel → Database tab

Reverse Proxy Error

  1. Check if the application is running on the correct port:
bash
sudo netstat -tlnp | grep 8080
  1. Check if the Proxy Port configuration in the website is correct
  2. Restart Nginx:
bash
sudo systemctl restart nginx

Summary

After completing the above steps, you have:

✅ Installed Go on the server
✅ Installed and configured MySQL
✅ Created website with reverse proxy
✅ Built and uploaded Golang application
✅ Created Systemd Service for auto-restart
✅ Configured environment variables
✅ Application ready to serve via domain with SSL

Your Golang application is now successfully deployed with:

  • Reverse proxy via Nginx
  • Auto-restart when server reboots or application crashes
  • SSL/HTTPS security
  • Log management for easy monitoring