Skip to content

Deploy a Go application ​

Suppose you have a Go application (web server or API) that you want to run under the domain api.example.com, and the application listens on port 8080. The application is built into a single executable, runs in the background as a Systemd Service, and the website on FlashPanel forwards requests for the domain to port 8080.

Prerequisites ​

  • Server connected to FlashPanel
  • Domain DNS pointed to the server's IP
  • Your application reads its listening port from configuration (for example the APP_PORT environment variable)

Step 1: Install Go ​

Go to the server management page, click the Apps menu, select the Not Installed tab, find Go, then click Install. See Applications for details.

Go is installed in /usr/local/go. To check it, open Terminal and run:

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

TIP

The commands in this guide use the full path /usr/local/go/bin/go because the user that owns the website may not have Go in its PATH.

Step 2: Prepare a Database (Optional) ​

If your application needs a database, install MySQL, MariaDB, PostgreSQL or MongoDB on the Apps page, then create a database and a user by following Database. Note the database name, username and password to set them in step 6.

Step 3: Create a Website ​

  1. Go to the server management page, click the Sites menu in the sidebar, then click the New Site button
  2. Enter the Domain Name (e.g., api.example.com)
  3. Enter the Proxy Port: the port your Go application listens on, for example 8080
  4. Click Create Site

TIP

If you enable Website Isolation, the website runs as its own Linux user. In that case, replace flashpanel in the paths below with the website's isolation username.

Step 4: Upload the Source Code ​

Choose one of the following:

  • From Git (recommended): on the site management page, click the Install Source Code menu and install your repository. See Source Code Installation.
  • Upload it yourself: open File Manager on the website's overview page and upload your code to the website directory. See File Manager.

The source code lives in /home/flashpanel/api.example.com.

Step 5: Build the Application ​

Open Terminal on the website's overview page (the terminal logs in as the user that owns the website and opens the website directory), then run:

bash
cd /home/flashpanel/api.example.com

# Build the application into an executable named "app"
CGO_ENABLED=0 /usr/local/go/bin/go build -o app .

TIP

  • The command builds the main package in the project root. If your main function lives in another directory, replace . with that path, for example ./cmd/server
  • CGO_ENABLED=0 produces a static executable that does not depend on the system's C libraries. Remove it if your project uses a library that needs cgo (for example go-sqlite3)

Run a quick test to make sure the application works, then press Ctrl+C to stop it:

bash
APP_PORT=8080 ./app

Step 6: Create a Service for the Application ​

Use the Systemd Service management feature to run the application in the background and restart it automatically:

  1. Go to the server management page, click the Service menu, then click Create Service
  2. Enter a Name, for example myapp
  3. Replace the content in the editor with the following:
ini
[Unit]
Description=Golang Application - api.example.com
After=network.target

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

# Application environment variables
Environment="APP_PORT=8080"
Environment="APP_ENV=production"
Environment="DB_HOST=127.0.0.1"
Environment="DB_PORT=3306"
Environment="DB_USER=myapp_user"
Environment="DB_PASSWORD=your_strong_password_here"
Environment="DB_NAME=myapp_db"

[Install]
WantedBy=multi-user.target
  1. Keep Boot with the system selected so the service starts when the server reboots
  2. Click Create Service. The service is created and started right away

WARNING

  • Replace api.example.com with your domain, and myapp in the log file names with the service name you chose
  • APP_PORT must match the website's Proxy Port. Make the application listen on 127.0.0.1 (for example http.ListenAndServe("127.0.0.1:"+port, ...)) so the port is not exposed directly to the Internet
  • Remove the DB_* lines if your application does not use a database. Default ports: MySQL/MariaDB 3306, PostgreSQL 5432, MongoDB 27017
  • If the database runs on the same server, add the database service to the After line so your application starts after it, for example After=network.target mysql.service (or mariadb.service, postgresql.service, mongod.service)
  • If your application reads a .env file in the website directory itself, you can set the variables there instead of using Environment lines

Step 7: Install SSL ​

On the site management page, click the SSL menu and install a certificate for the domain. See SSL.

Verify the Result ​

  1. On the Service page, the myapp service shows as active
  2. Visit https://api.example.com and check that your application responds correctly

Updating the Application ​

  1. Update the source code (click Deploy Now in the Deployments menu if it was installed from Git, or upload the new version with File Manager)
  2. Open the website's Terminal and rebuild: CGO_ENABLED=0 /usr/local/go/bin/go build -o app .
  3. On the Service page, click the Restart button of the myapp service

TIP

If the source code was installed from Git, you can add the build command to the Deployment Script so you do not have to run step 2 by hand. The Deployment Script runs as the user that owns the website, so it cannot restart a Systemd Service; you still need to click Restart on the Service page.

Troubleshooting ​

Application Won't Start ​

  1. On the Service page, click View Error Log for the myapp service
  2. Make sure the executable has run permission: chmod +x app
  3. Run the application manually in the website directory to see the error: APP_PORT=8080 ./app
  4. Check that the paths in WorkingDirectory and ExecStart are correct

Database Connection Errors ​

  1. On the Apps page, check that the database is running
  2. Make sure the DB_* variables match the database and user created on the server's database page (for example the MySQL menu)

502 Bad Gateway ​

  1. Check that the service is running on the Service page
  2. Check that the application listens on the right port: ss -tln | grep 8080
  3. Make sure this port matches the website's Proxy Port. See also 502 Bad Gateway