How to deploy Golang services on Linux

王林
Release: 2023-05-15 15:22:06
forward
1169 people have browsed it

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
Copy after login

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
Copy after login

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!")
}
Copy after login

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
Copy after login

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

./<可执行文件名> &
Copy after login

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
Copy after login
  1. Enter the directory where the service program is located, And use the following command to start the service:

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

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
Copy after login
  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 <服务名称>
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!