Home > Backend Development > Golang > golang linux deployment

golang linux deployment

WBOY
Release: 2023-05-22 15:59:37
Original
539 people have browsed it

This article will introduce how to deploy Go language applications on Linux systems.

  1. Install the Go environment

The Go language official website provides an installation package for Linux. You can download and install the Go environment through the following command:

wget https://golang.org/dl/go1.15.7.linux-amd64.tar.gz
tar -C /usr/local -xzf go1.15.7.linux-amd64.tar.gz
Copy after login

Installation Finally, you need to add the environment variable to PATH in order to use Go related commands in the command line:

echo 'export PATH=$PATH:/usr/local/go/bin' >> ~/.bashrc
source ~/.bashrc
Copy after login

Verify whether Go is successfully installed through the following command:

go version
Copy after login
  1. Compile application

Go applications need to be compiled into executable files before deployment. You can use the following command to compile the code:

go build -o app main.go
Copy after login

where app is the name of the executable file generated after compilation, and main.go is the entry file of the application .

  1. Deploy the application

Upload the compiled application file to the Linux server and start the application through the following command:

./app
Copy after login

Among them, app is the executable file name. After successful startup, the application will run in the background and listen to the specified port, waiting for user requests.

If you want the application to run in the background, you can use the following command:

nohup ./app > log.out 2>&1 &
Copy after login

where log.out is the output log file name of the application. In this way, the application will run in the background and the output log will be written to log.out.

  1. Nginx reverse proxy

In order to improve the performance and reliability of the application, we can consider deploying the application with the help of Nginx reverse proxy.

First, you need to add the following content to the Nginx configuration file:

server {
    listen 80;
    server_name example.com;
    location / {
        proxy_pass http://localhost:8080; // 8080为应用程序监听的端口号
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}
Copy after login

Among them, example.com is the domain name or server IP address of the application. In this way, when a user accesses http://example.com, Nginx will forward the request to the local port 8080.

Next, start the Nginx service and access it using a browser.

systemctl start nginx
Copy after login

At this point, we have completed the process of deploying Go language applications on Linux systems. Through Nginx reverse proxy, the reliability and performance of the application can be effectively improved.

The above is the detailed content of golang linux 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