Detailed explanation of Golang server deployment method

PHPz
Release: 2024-01-16 10:11:09
Original
1361 people have browsed it

Detailed explanation of Golang server deployment method

Golang is a programming language that has attracted much attention in recent years. It has the advantages of high concurrency and fast execution efficiency. More and more companies are beginning to use Golang to develop backends. Application, therefore, Golang’s deployment method on the server has also attracted much attention. This article will introduce in detail the deployment method of Golang on the server, and attach specific code examples.

1. Preparation

Before deploying Golang, you need to download and install Golang. If you have not installed Golang, you can download and install it from the Golang official website (https://golang.org/dl/).

In addition, you also need to configure your server environment. If you are using a Linux server, it is recommended to install systemd to manage your services. If you are using a Windows server, you can use Windows services to manage your services in subsequent steps.

2. Write Golang application

Before deploying Golang, you need to write a Golang application first. The following is sample code for a simple Golang application:

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

This application simply starts an HTTP server, listens on port 8080, and returns the "Hello, World!" string when the client accesses it.

3. Compile the application

After you finish writing the application, you need to compile it into an executable file. You can use the following command to compile the application in the previous step:

GOOS=linux GOARCH=amd64 go build -o app main.go
Copy after login

This command compiles the application into an executable file for the Linux system, which can be run directly on the Linux server.

Note: If you are compiling an application for Windows system, you can change GOOS to windows to compile an executable file for Windows system.

4. Deploy the application

Now that we have written and compiled the Golang application, we can deploy it to our server.

  1. Upload the executable file to the server

First, you need to upload the executable file to the server. This can be achieved using the scp command:

scp app root@[服务器IP地址]:/opt/app/
Copy after login
  1. Create and run the systemd service

Next, we will create a systemd service to manage Golang applications.

Open a terminal on the server and enter the following command:

sudo nano /etc/systemd/system/app.service
Copy after login

This command will create a file named "app.service". In this file, the following content will be written:

[Unit] Description=My Go Application [Service] ExecStart=/opt/app/app Restart=always User=root WorkingDirectory=/opt/app [Install] WantedBy=multi-user.target
Copy after login

The meaning of this file is as follows:

  • [Unit]: Describes the basic information of the service.
  • Description: Description information of the service.
  • [Service]: Specifies the specific configuration information of the service.
  • ExecStart: Specify the command or executable file to run.
  • Restart: Specify that the service should be automatically restarted when it exits abnormally.
  • User: Specify the user to run the service.
  • WorkingDirectory: Specify the working directory when the service is running.
  • [Install]: Specifies how to install this service.
  • WantedBy: Indicates which target (target) the service should be started when the system runs to it.

Save and close the file, and then follow the following commands to start and manage the service:

sudo systemctl daemon-reload sudo systemctl start app sudo systemctl enable app
Copy after login

The first command is used to reload the system configuration file information, and the second command is used to Start the service. The third command is used to set the service to start automatically at boot.

5. Test the application

Now that we have successfully deployed the Golang application to the server, we can use the browser or curl command to test the operation of the application:

curl http://[服务器IP地址]:8080
Copy after login

If everything goes well, you will see the "Hello, World!" string on the command line.

6. Summary

The above is the detailed process of deploying Golang applications on the server using systemd. The deployment of Golang on the server side is very simple. You only need to write a Golang application first, and then use systemd or Windows services to manage your services. I hope this article will be helpful to developers who are learning Golang and deploying Golang server-side.

The above is the detailed content of Detailed explanation of Golang server deployment method. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
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!