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.
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.
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.
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.
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.
In the deployment process of stand-alone golang applications, security hardening is particularly important. Some security reinforcement measures include:
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
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!