Deploying a Go Application on Ubuntu Server

In this tutorial, we’ll go over the steps to deploy a Go application on an Ubuntu server.

Step 1: Install Go

First, we need to install Go on the server. Open a terminal and run the following commands:

sudo apt-get update
sudo apt-get install golang-go

This will install the latest version of Go on your server. You can verify the installation by running go version.

Step 2: Set up GOPATH

Go requires a workspace directory where all Go projects will reside. To set this up, we need to set the GOPATH environment variable in the .bashrc file.

nano ~/.bashrc

Add the following line at the end of the file:

export GOPATH=$HOME/go

Save the file and run the following command to reload the .bashrc file:

source ~/.bashrc

Step 3: Copy the Application to the Server

We can copy the application to the server using scp. This allows us to transfer files over SSH.

scp <local_path_to_app> <username>@<server_ip>:<remote_path>

For example:

scp ~/go/src/app [email protected]:/root/go/src/

Step 3 (Alternate): Clone the Application from Git

If you are using Git, we can clone it directly to the server.

cd /root/
mkdir go
cd go/
git clone https://github.com/<username>/<repo>.git

Step 4: Build the Application

Once the application is copied, we can build it on the server.

cd app/cmd/backend/
go build

This will compile the application and create an executable binary file named after the main package.

Step 5: Set up a Systemd Service

To run the application as a service, we’ll set up a Systemd service. This ensures that the application runs automatically on startup and can be easily managed using the Systemd commands.

Create a new Systemd service file by running:

nano /etc/systemd/system/backend.service

Add the following content to the file:

[Unit]
Description=Go Application

[Service]
WorkingDirectory=/root/go/app/cmd/backend
ExecStart=/root/go/app/cmd/backend/backend
Restart=always
User=root

[Install]
WantedBy=multi-user.target

Save and close the file. Then, run the following commands to reload the Systemd configuration and start the service:

systemctl daemon-reload
systemctl start backend
systemctl enable backend

The application should now be running as a service on the server. You can check the status of the service using the systemctl status app command.

Step 6: Install Nginx

To install Nginx, run the following command:

sudo apt-get install nginx

Step 7: Configure Nginx as a Reverse Proxy

Create a new Nginx server block configuration file for your application:

sudo nano /etc/nginx/sites-available/backend

Add the following content to the file:

server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://127.0.0.1:<port>;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_pragma;
    }
}

Replace example.com with your domain name or IP address, and <port> with the port on which your Go application is running.

Next, create a symbolic link to the configuration file in the sites-enabled directory:

sudo ln -s /etc/nginx/sites-available/backend /etc/nginx/sites-enabled/

Step 8: Restart Nginx

Finally, restart Nginx for the changes to take effect:

sudo systemctl restart nginx

Your Go application should now be accessible via port 80. You can test it by visiting http://example.com in a web browser.

Note: Make sure that the firewall on the server allows incoming connections on port 80. If not, you may need to configure the firewall to allow incoming traffic on port 80.

Open chat