Home Backend Development Golang Standalone golang deployment

Standalone golang deployment

May 05, 2023 pm 09:41 PM

With the gradual popularity of go language and the continuous expansion of its application scope, more and more developers are beginning to tend to use golang as the development language for projects. If you want to deploy a golang application, there are two ways: one is to use containerization technology, such as Docker, Kubernetes, etc.; the other is to use stand-alone deployment. This article will introduce in detail the relevant steps and precautions for stand-alone golang deployment.

  1. Confirm the machine environment

Before we start, we need to confirm whether the machine environment meets the requirements. First, you need to confirm whether golang is installed on the machine. On a Linux system, you can use the following command to check whether golang has been installed:

go version
Copy after login

If the following content is output, it means golang has been successfully installed:

go version go1.16.5 linux/amd64
Copy after login

Secondly, you need to confirm whether the machine has git installed. , because we need to pull the code from git during the deployment process. On a Linux system, you can use the following command to check whether git has been installed:

git version
Copy after login

If the following content is output, git has been successfully installed:

git version 2.17.1
Copy after login

Finally, you need to confirm whether the running environment of the machine is have. For golang applications, its running environment needs to contain required dependencies and library files required by the operating system. Before confirming the machine environment, you need to first understand the required library files, such as libssl, libcrypto, etc., to adapt to the use of third-party libraries such as sphinx and elasticsearch. You can use the following command to find the installation path of the library:

ldconfig -p | grep "library-name"
Copy after login

If the corresponding library file is not found, you need to download and install it manually.

  1. Get the code

In order to deploy the golang application, we must obtain the code required for deployment. Among them, the code can be obtained through the git clone command, as shown below:

git clone https://github.com/username/project.git
Copy after login

After executing this command, a directory named project will be generated in the current directory, which is what we need. Golang application code.

  1. Compile program

After obtaining the code, you need to compile an executable binary program. This step usually needs to be set according to the specific environment and parameters of the program, such as the port number the program monitors, the path to output the log, etc. In this article, we take a simple hello world program as an example to illustrate. First, execute the following command in the code directory, and a binary file

go build -o app main.go
Copy after login

will be generated. Among them, app is the name of the output binary program, and main.go is the entry point of the golang application. After executing this command, a binary file named app will be generated in the code directory. Then, use the following command to start the binary program:

./app
Copy after login

At this time, we can use the curl command to detect whether the program has been successfully started:

curl http://localhost:8080
Copy after login

If "Hello, World! ", indicating that the program has been successfully started.

  1. Process Management

In the process of deploying golang applications, in order to facilitate the management of starting, stopping, and restarting the application, we need to use process management tools. Currently, the most commonly used process management tool is systemd. Here we will use systemd as an example to explain how to manage processes.

First, create a new file named app.service in /etc/systemd/system and add the following content to the file:

[Unit]
Description=description-of-app
After=network.target

[Service]
Type=simple
Restart=always
StartLimitInterval=0
RestartSec=2
User=username
Group=username
ExecStart=/path/to/application
WorkingDirectory=/path/to/application
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=app
Environment=ENV=prod

[Install]
WantedBy=multi-user.target
Copy after login

Modify the description in the above configuration file -of-app, username, path/to/application, and ENV respectively represent the description of the application, the username under which the service is running, the path to the binary executable file of the application, and the running environment (test or production environment).

After completing the configuration file, execute the following command to load and start the systemd service:

sudo systemctl daemon-reload
sudo systemctl start app.service
Copy after login

After executing the above command, you can use the following command to check the status of the service:

sudo systemctl status app.service
Copy after login

If the status is "active (running)", it means the service has been started successfully.

  1. Security hardening

In the deployment process of stand-alone golang applications, security hardening is particularly important. Some security reinforcement measures include:

  1. Turn on the firewall wall penetration policy: You can use the iptables command to enable restrictions and protection on the external connections of the machine.
  2. Restrict the use of sudo commands: Prevent non-administrator users from obtaining system privileges through sudo commands.
  3. Restrict public network ssh: You can restrict public network access to ssh by adding the following entries to /etc/ssh/sshd_config:

    Port 22
    PermitRootLogin no
    AllowUsers user1 user2
    Copy after login
  4. Use waf for protection http request: Using waf, you can uniformly filter and detect http requests, thereby avoiding some attacks that may cause system crashes or data leaks.
  5. Summary

The above are the relevant processes and precautions for stand-alone golang deployment. To summarize, the specific steps of stand-alone golang deployment include confirming the machine environment, obtaining code, compiling the program, and process management. , security reinforcement and other steps. For beginners, the above processes and precautions are also worth understanding and learning, which can provide better help for future golang application deployment.

The above is the detailed content of Standalone golang deployment. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the vulnerabilities of Debian OpenSSL What are the vulnerabilities of Debian OpenSSL Apr 02, 2025 am 07:30 AM

OpenSSL, as an open source library widely used in secure communications, provides encryption algorithms, keys and certificate management functions. However, there are some known security vulnerabilities in its historical version, some of which are extremely harmful. This article will focus on common vulnerabilities and response measures for OpenSSL in Debian systems. DebianOpenSSL known vulnerabilities: OpenSSL has experienced several serious vulnerabilities, such as: Heart Bleeding Vulnerability (CVE-2014-0160): This vulnerability affects OpenSSL 1.0.1 to 1.0.1f and 1.0.2 to 1.0.2 beta versions. An attacker can use this vulnerability to unauthorized read sensitive information on the server, including encryption keys, etc.

Transforming from front-end to back-end development, is it more promising to learn Java or Golang? Transforming from front-end to back-end development, is it more promising to learn Java or Golang? Apr 02, 2025 am 09:12 AM

Backend learning path: The exploration journey from front-end to back-end As a back-end beginner who transforms from front-end development, you already have the foundation of nodejs,...

What libraries are used for floating point number operations in Go? What libraries are used for floating point number operations in Go? Apr 02, 2025 pm 02:06 PM

The library used for floating-point number operation in Go language introduces how to ensure the accuracy is...

What is the problem with Queue thread in Go's crawler Colly? What is the problem with Queue thread in Go's crawler Colly? Apr 02, 2025 pm 02:09 PM

Queue threading problem in Go crawler Colly explores the problem of using the Colly crawler library in Go language, developers often encounter problems with threads and request queues. �...

How to specify the database associated with the model in Beego ORM? How to specify the database associated with the model in Beego ORM? Apr 02, 2025 pm 03:54 PM

Under the BeegoORM framework, how to specify the database associated with the model? Many Beego projects require multiple databases to be operated simultaneously. When using Beego...

In Go, why does printing strings with Println and string() functions have different effects? In Go, why does printing strings with Println and string() functions have different effects? Apr 02, 2025 pm 02:03 PM

The difference between string printing in Go language: The difference in the effect of using Println and string() functions is in Go...

What should I do if the custom structure labels in GoLand are not displayed? What should I do if the custom structure labels in GoLand are not displayed? Apr 02, 2025 pm 05:09 PM

What should I do if the custom structure labels in GoLand are not displayed? When using GoLand for Go language development, many developers will encounter custom structure tags...

How to solve the user_id type conversion problem when using Redis Stream to implement message queues in Go language? How to solve the user_id type conversion problem when using Redis Stream to implement message queues in Go language? Apr 02, 2025 pm 04:54 PM

The problem of using RedisStream to implement message queues in Go language is using Go language and Redis...

See all articles