English
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_PORTenvironment 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 versionTIP
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
- Go to the server management page, click the Sites menu in the sidebar, then click the New Site button
- Enter the Domain Name (e.g.,
api.example.com) - Enter the Proxy Port: the port your Go application listens on, for example
8080 - 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
mainpackage in the project root. If yourmainfunction lives in another directory, replace.with that path, for example./cmd/server CGO_ENABLED=0produces 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 examplego-sqlite3)
Run a quick test to make sure the application works, then press Ctrl+C to stop it:
bash
APP_PORT=8080 ./appStep 6: Create a Service for the Application
Use the Systemd Service management feature to run the application in the background and restart it automatically:
- Go to the server management page, click the Service menu, then click Create Service
- Enter a Name, for example
myapp - 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- Keep Boot with the system selected so the service starts when the server reboots
- Click Create Service. The service is created and started right away
WARNING
- Replace
api.example.comwith your domain, andmyappin the log file names with the service name you chose APP_PORTmust match the website's Proxy Port. Make the application listen on127.0.0.1(for examplehttp.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/MariaDB3306, PostgreSQL5432, MongoDB27017 - If the database runs on the same server, add the database service to the
Afterline so your application starts after it, for exampleAfter=network.target mysql.service(ormariadb.service,postgresql.service,mongod.service) - If your application reads a
.envfile in the website directory itself, you can set the variables there instead of usingEnvironmentlines
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
- On the Service page, the
myappservice shows as active - Visit
https://api.example.comand check that your application responds correctly
Updating the Application
- 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)
- Open the website's Terminal and rebuild:
CGO_ENABLED=0 /usr/local/go/bin/go build -o app . - On the Service page, click the Restart button of the
myappservice
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
- On the Service page, click View Error Log for the
myappservice - Make sure the executable has run permission:
chmod +x app - Run the application manually in the website directory to see the error:
APP_PORT=8080 ./app - Check that the paths in
WorkingDirectoryandExecStartare correct
Database Connection Errors
- On the Apps page, check that the database is running
- 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
- Check that the service is running on the Service page
- Check that the application listens on the right port:
ss -tln | grep 8080 - Make sure this port matches the website's Proxy Port. See also 502 Bad Gateway