Home  >  Article  >  Backend Development  >  How to deploy and defend golang applications using systemd

How to deploy and defend golang applications using systemd

PHPz
PHPzOriginal
2023-04-05 09:09:501306browse

In the era of cloud computing, rapid deployment allows developers to quickly deliver products and respond quickly to user needs. Golang has become an indispensable programming language in the field of cloud computing in recent years. It combines fast compilation and efficient concurrency performance. But what if the application crashes? In order to keep the application running, we need to use a daemon to monitor and restart the application. This article will introduce how to use systemd to deploy and defend golang applications.

1. Installation and configuration systemd

systemd is one of the most popular initialization and daemon management systems today. It is responsible for starting system services and daemons, monitoring their status and restarting them when necessary. Before using systemd, it must be installed.

Use the following command to install systemd:

$ sudo apt-get update
$ sudo apt-get install -y systemd

2. Create a service file

Before creating the daemon, we need to create a systemd service file. A service file is a text file that contains information describing a service. The location of the service file is in the /etc/systemd/system/ directory. For example, we create a service file named golang-demo.service:

$ sudo touch /etc/systemd/system/golang-demo.service

Edit the golang-demo.service file:

$ sudo nano /etc/systemd/system/golang-demo.service

Add the following to the file:

[Unit]
Description=My golang app
After=network.target

[Service]
Type=simple
User=ubuntu
Group=ubuntu
ExecStart=/usr/local/bin/golang-demo

[Install]
WantedBy=multi-user.target

where the

  • [Unit] section contains the service name and description, and when the service should be started ( after network.target). The
  • [Service] section describes the details of starting the service, including the user, group used, and the location of the startup script file. The
  • [Install] section specifies the target on which systemd should start the service.

3. Create a golang application

Next, we need to create a golang application named golang-demo. In the terminal, use the following git command to clone a simple go application:

$ git clone https://github.com/shalar/golang-demo.git

Go into the project directory, and build and compile the application:

$ cd golang-demo
$ go build

We need to copy the generated binary filegolang-demo Move to the /usr/local/bin directory.

$ sudo mv golang-demo /usr/local/bin/

4. Start the service and test it

Now, we are ready to start the service and test it. Use the command to start the service:

$ sudo systemctl start golang-demo.service

Check the status of the service:

$ sudo systemctl status golang-demo.service

See active (running), which means the service is running and can be accessed through the browser http:/ /localhost:8080, you should be able to see the output.

5. Auto-start at boot

As mentioned above, in the [Install] section, we specify which target systemd should start the service on. Now, we can use the following command to start automatically at boot:

$ sudo systemctl enable golang-demo.service

Now, the system will automatically start the service every time it is started.

6. Conclusion

This article introduces how to use systemd to deploy and protect golang applications. systemd is a popular daemon manager that can easily manage the starting, stopping, restarting and other operations of services, keeping our applications always online.

The above is the detailed content of How to deploy and defend golang applications using systemd. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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