Home >Backend Development >Golang >How to deploy Golang applications on Linux operating system
In today's software development, the proportion of using Linux as the operating system is increasing, and the Golang language is favored by more and more developers because of its efficiency, simplicity, and concurrency performance. In this article, we will introduce how to deploy Golang applications on Linux operating system.
Before we start deploying Golang programs, we need to install the Golang environment first. First download the Golang binary installation package. You can download the latest version of the binary installation package from the official website: https://golang.org/dl/.
After downloading, decompress the file and move the decompressed folder to the /usr/local directory:
$ tar -C /usr/local -xzf go1.17.2.linux -amd64.tar.gz
Next, set the environment variables and add /usr/local/go/bin to PATH:
$ export PATH=$PATH:/usr/local/ go/bin
In order to verify whether the Golang environment is installed correctly, you can use the following command:
$ go version
If the Golang version information is returned, it means that the Golang environment is correct Install.
Before you start deploying a Golang application, you need to write a Golang application first. Here we take a simple HTTP service as an example:
package main import ( "fmt" "net/http" ) func main() { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello, World!") }) http.ListenAndServe(":8080", nil) }
This program will start an HTTP service on the local port 8080. Visit http://localhost:8080 in the browser, and you can see the output of Hello, World!.
After writing the Golang program, you need to build it. Use the following command to build:
$ go build
This command will compile the program into an executable file. After execution, a file named main will be generated in the current directory.
Golang applications can be deployed in the following two ways:
4.1 Directly run the executable file
The easiest way is to run the executable file directly. Run the following command:
$ ./main
This command will start the Golang application and start the HTTP service on the local port 8080.
However, this method has some shortcomings, such as the inability to automatically restart after a program crashes, and the inability to perform cluster deployment.
4.2 Use systemd for management
In order to solve the above problems, you can use systemd to manage Golang programs. systemd is a system and service manager under Linux systems. We can use systemd to run Golang programs as a system service.
First, create a service configuration file named myapp.service and copy the following content into the file:
[Unit] Description=My Golang App After=syslog.target network.target [Service] Type=simple ExecStart=/usr/local/golang/bin/myapp Restart=on-failure [Install] WantedBy=multi-user.target
This configuration file describes the service we want to run named My Golang App and restart it if there is a problem with the program.
Next, save the file to the /etc/systemd/system directory and use the following command to reload the systemd configuration:
$ systemctl daemon-reload
Then, Start the service using the following command:
$ systemctl start myapp
Check the service status using the following command:
$ systemctl status myapp
If everything is OK, run The result should read active (running).
Finally, use the following command to start the service automatically:
$ systemctl enable myapp
At this point, we have successfully deployed a Golang program and managed the program with systemd.
Summary
This article introduces how to deploy Golang applications on the Linux operating system. First, we installed the Golang environment, then wrote a simple HTTP service program, and then used the go build command to build it. Finally, we introduced two deployment methods: running the executable directly and using systemd to manage it. I hope this article can help developers successfully deploy Golang programs.
The above is the detailed content of How to deploy Golang applications on Linux operating system. For more information, please follow other related articles on the PHP Chinese website!