Home > Backend Development > Golang > Standalone golang deployment

Standalone golang deployment

王林
Release: 2023-05-05 21:41:10
Original
554 people have browsed it

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!

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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template