How to deploy Golang applications in Linux

06-30-2023

This article mainly introduces how to deploy Golang applications in Linux. In daily operations, I believe that many people have doubts about how to deploy Golang applications in Linux. I hope it will help you answer your doubts about how to deploy Golang applications in Linux! Next, please follow the editor to learn together!

1. Install the Golang environment

In the Linux system, the installation of Golang is very simple. First, you need to go to the official website of Golang (https://golang.org/dl/) to download the corresponding version of the binary file. Then unzip the downloaded file to the /usr/local directory, and set the environment variables GOPATH, GOROOT:

tar -C /usr/local -xzf go1.15.2.linux-amd64.tar.gz< br/>export GOROOT=/usr/local/go
export GOPATH=$HOME/go
export PATH=$GOPATH/bin:$GOROOT/bin:$PATH

Run go version command, you can check whether Golang is successfully installed:

go version

Second, build Golang applications

Before building Golang applications, you need to first To install Git, you can use the following command to install:

apt-get install git

Building Golang applications is very simple, just use the go command to build. In this article, we use a simple Hello World program as an example:

1.package main
2.

  1. import "fmt"
    4.

  2. func main() {

  3. fmt.Println("Hello World ")

  4. }

Download the program with the following command:

go get github.com /username/helloworld

Use the following command to enter the program directory:

cd $GOPATH/src/github.com/username/helloworld

Use the following command to build the program:

go build -o helloworld

After the build is complete, you can see the generated executable file helloworld in the current directory .

Third, configure Nginx

So far, we have successfully built a Golang application, the problem now is how to let the application access the Internet. This requires reverse proxying through Nginx.

1. Install Nginx

Use the following command to install Nginx:

sudo apt-get update
sudo apt-get install nginx

p>

2. Configure Nginx

Edit the Nginx configuration file /etc/nginx/sites-enabled/default, and add the following configuration:

1.server {

  1. listen 80;

  2. server_name example.com;
    4.

  3. location / {

  4. proxy_pass http://127.0.0.1:8080;

  5. proxy_set_header Host $host;

  6. proxy_set_header X-Real-IP $remote_addr;

  7. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

  8. }
    11.}

Among them, server_name needs to be replaced with your own domain name or IP address , proxy_pass needs to be set to the port number where the Golang application is running.

After saving the configuration file, restart Nginx with the following command:

sudo service nginx restart

Now, just access the IP address or domain name of the Nginx server, You can access the Golang application.

At this point, the learning about how to deploy Golang applications in Linux is over, and I hope to solve your doubts. The combination of theory and practice can better help everyone learnXi, go and try it! If you want to continue to learn more relevant knowledge, please continue to pay attention to the Yisuyun website, and the editor will continue to work hard to bring you more practical articles!

Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us