Standalone golang deployment
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.
- 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
If the following content is output, it means golang has been successfully installed:
go version go1.16.5 linux/amd64
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
If the following content is output, git has been successfully installed:
git version 2.17.1
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"
If the corresponding library file is not found, you need to download and install it manually.
- 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
After executing this command, a directory named project will be generated in the current directory, which is what we need. Golang application code.
- 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
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
At this time, we can use the curl command to detect whether the program has been successfully started:
curl http://localhost:8080
If "Hello, World! ", indicating that the program has been successfully started.
- 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
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
After executing the above command, you can use the following command to check the status of the service:
sudo systemctl status app.service
If the status is "active (running)", it means the service has been started successfully.
- Security hardening
In the deployment process of stand-alone golang applications, security hardening is particularly important. Some security reinforcement measures include:
- 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.
- Restrict the use of sudo commands: Prevent non-administrator users from obtaining system privileges through sudo commands.
-
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 - 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.
- 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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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.

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,...

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

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. �...

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...

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? When using GoLand for Go language development, many developers will encounter custom structure tags...

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