Home  >  Article  >  Operation and Maintenance  >  How to deploy Golang services on Linux

How to deploy Golang services on Linux

王林
王林forward
2023-05-15 15:22:061132browse

1. Install Golang and necessary tools

Before deploying Golang services, you first need to install Golang and necessary tools. It can be installed through the following command:

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install golang
sudo apt-get install git

The last line installs the Git tool. Because Golang projects are usually version managed through Git. After the installation is complete, you can check whether the installation is successful by running the following command:

go version
git version

If both the above two commands output the version number, it means the installation is successful.

2. Writing Golang services

After installing Golang and the necessary tools, you can start writing Golang services. Taking a simple HTTP service as an example, the following is a code example:

package main

import (
    "fmt"
    "net/http"
)

func main() {
    http.HandleFunc("/", helloHandler)
    fmt.Println("Server started on port 8080")
    http.ListenAndServe(":8080", nil)
}

func helloHandler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hello, World!")
}

The service code has a simple function, listens to the local port 8080, and returns the string "Hello, World!" for each request. Use the http.HandleFunc method to specify a route and a handler function for the route.

3. Build and test the Golang service

After writing the Golang service code, you need to build and test it. The service can be built using the following command: go build. This command will generate an executable file in the current directory. Before testing, you need to modify the current user's firewall rules to allow the service to listen on port 8080. You can use the following command:

sudo ufw allow 8080/tcp

After the modification is completed, you can start the service for testing. You can use the following command to start the service:

./<可执行文件名> &

The & symbol means running the service in the background. After successful startup, you can use a browser or the curl command to access the service.

4. Use PM2 for Golang service deployment

Manually starting the Golang service is obviously not very friendly, especially when the service needs to be restarted or monitored, it is even more inconvenient. Therefore, PM2 tools can be used for service management and deployment. PM2 is a process management tool in the Node.js ecosystem, but it also supports managing processes written in other languages, including Golang. The following is an example of using PM2 for Golang service deployment:

  1. Install PM2

sudo npm install -g pm2
  1. Enter the directory where the service program is located, And use the following command to start the service:

pm2 start <可执行文件名> --name=<服务名称>

Among them, the --name parameter is used to specify the name of the service.

  1. You can use the following command to view the status of the service:

pm2 list
  1. For processes that have been managed by PM2, you can Use the following command to operate the service:

pm2 restart <服务名称>
pm2 stop <服务名称>
pm2 delete <服务名称>

The above is the detailed content of How to deploy Golang services on Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete