KB
cPanel & Hosting

Deploying a Golang Backend on cPanel (Shared Hosting)

3 min read604 words10 code blocks

At a glance#

  • Purpose: Deploy and run a compiled Go application on cPanel shared hosting using Application Manager.
  • Applies to: cPanel & WHM with Application Manager enabled.
  • Risk: Low - application-level deployment.
  • Time: About 1 hour.

This document describes the procedure used to deploy a Golang backend on a cPanel shared hosting environment. It covers building the binary, uploading it to the server, configuring a subdomain, and setting up reverse proxy routing.


1. Requirements#

  • cPanel access
  • SSH/Terminal access to the hosting account
  • Ability to upload files (File Manager or FTP)
  • Go installed on the local machine
  • A Golang project with a defined main.go entry point

2. Build the Go Application for Linux#

Shared hosting servers run Linux, so the Go application must be compiled as a Linux binary, even if the development machine is Windows or macOS.

Windows CMD:#

bash
set GOOS=linux
set GOARCH=amd64
go build -o backend ./cmd/server

Windows PowerShell:#

powershell
$env:GOOS="linux"
$env:GOARCH="amd64"
go build -o backend ./cmd/server

This produces a Linux binary named:

text
backend

3. Upload the Binary to cPanel#

Upload the generated binary to a logical directory in the hosting account, for example:

text
/home/<cpanel-user>/backend/backend/

Do not upload the Windows .exe file. Upload only the Linux binary.


4. Make the Binary Executable and Run It#

SSH into the server:

bash
cd ~/backend/backend
chmod +x backend
./backend

If the server prints startup logs (for example, "Server starting on port 8080"), it is running correctly.


5. Run the Application in the Background#

To keep the application running after closing the terminal session:

bash
nohup ./backend > backend.log 2>&1 &

Verify:

bash
ps aux | grep backend

6. Create a Subdomain for the API#

Since the main domain may already be running another application (such as Node.js via Passenger), create a separate subdomain for the Go backend.

Example subdomain: api.example.com or test.example.com.

In cPanel:

  1. Open Domains.
  2. Create subdomain.
  3. Confirm document root (example: /home/<user>/test.example.com).

7. Disable PHP-FPM for the Subdomain#

PHP-FPM handlers override reverse proxy behavior. To allow Apache rewrite proxying, PHP-FPM must be disabled for the subdomain.

In cPanel:

  1. Go to MultiPHP Manager.
  2. Locate the subdomain.
  3. Disable PHP-FPM (uncheck the box) or set PHP version to "inherit".

This removes automatic SetHandler directives that break reverse proxy routing.


8. Configure Reverse Proxy via .htaccess#

Create or edit:

text
/home/<user>/test.example.com/.htaccess

Use the following configuration:

text
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://127.0.0.1:8080/$1 [P,L]

This proxies all HTTP requests from the subdomain to the local Go application running on port 8080.


9. Test the Deployment#

Open the subdomain in a browser:

text
https://test.example.com/health

If the Go application returns a valid JSON response, the deployment is working.


10. Optional Improvements#

Automatic Restart#

Use cron or a lightweight monitoring script to restart the Go application if it stops.

Permanent API Subdomain#

Migrate from a test subdomain to a production-ready API subdomain, such as api.example.com.

Logging#

Configure log rotation to prevent backend.log from growing indefinitely.

Hardening#

Add CORS configuration, HTTP security headers, and input validation based on production requirements.


Overview#

  • The Go application runs locally on the server at 127.0.0.1:8080.
  • The subdomain uses Apache rewrite rules to proxy requests to the Go process.
  • PHP-FPM must be disabled for proxying to work on shared hosting.
  • The main domain remains unaffected and can serve other applications (such as a Node.js Passenger app).

If you need a condensed version, a printable PDF, or a diagram illustrating this setup, I can provide that as well.