How To Deploy Golang Application On Any GNU/Linux System, But ...
Có thể bạn quan tâm
Introduction
Hey, hey! 👋 Another interesting article about deploy and Golang is here.
Let's imagine that we need to run an application written on Go on a remote server, but configure Docker too long (or just lazy).
What should we do? Learn from this article! 😉
📝 Table of contents
- Why may I need this?
- The basis of our service
- A few words about Fiber
- Creating systemd service
- Setting up a reverse proxy server
- Final result
Why may I need this?
First, it is just important to know how service applications work on GNU/Linux. Also, it will be interesting for very small projects, that are too early to think about Docker and deployment automation (startups, REST API micro services, etc).
↑ Table of contents
The basis of our service
Okay, time to write a simple web service, that will give us something, when sending a request to it. For example, I'll take my favorite web framework Fiber (v2), which works extremely fast and easy to learn.
If you are not yet familiar with it, follow the comments in the code:
package main import ( "log" "github.com/gofiber/fiber/v2" ) func main() { // Create new Fiber instance app := fiber.New() // Make path with some content app.Get("/hello", func(c *fiber.Ctx) error { // Return a string with a dummy text return c.SendString("Hello, World 👋!") }) // Start server on http://localhost:3000 or error log.Fatal(app.Listen(":3000")) } Enter fullscreen mode Exit fullscreen modeSave this file as main.go and build binary for GNU/Linux x64 within a similar folder with hello_world name:
GOOS=linux GOARCH=amd64 go build main.go -o ./hello_world Enter fullscreen mode Exit fullscreen modeOK. Now, just copy this binary to your remote server inside /root/go folder.
↑ Table of contents
A few words about Fiber
Perhaps, you are asking yourself, so why do I love Fiber web framework so much? The answer is in its main features:
- Robust routing
- Extreme performance
- API-ready web framework
- Flexible middleware support
- Low memory footprint
And, especially, I'd like to mention the extremely friendly community in Discord and detailed documentation (translated into different World languages)!
↑ Table of contents
Creating systemd service
The systemd is a software suite that provides an array of system components for Linux operating systems.
[...] Its main aim is to unify service configuration and behavior across Linux distributions; systemd's primary component is a "system and service manager"—an init system used to bootstrap user space and manage user processes.
— Wikipedia
Perfect! Now let's create a new service.
- Connect to your remote server as root (super user)
- Create a file with the extension *.service within the systemd folder:
- Copy this code to it:
- Hit Ctrl+O to save and Ctrl+X for close editor
- Next, start and enable this service:
☝️ Service will start automatically, even after the remote server reboots.
↑ Table of contents
Setting up a reverse proxy server
I will use Nginx as a reverse proxy server, but you can choose any other one.
- Install nginx using the package manager. For Debian-like systems it will look like this:
- Start nginx service:
- Create a config in /etc/nginx/sites-available/hello_world file:
- Create a symbolic link of our config file to the sites-enabled folder:
- Finally, reload nginx to apply config:
🔥 Sometimes it can be a problem that port 3000 is not available on your remote server. Please check the firewall settings.
↑ Table of contents
Final result
Yep, that's it! Go to http://your_server_ip:3000 and see result 🎉
↑ Table of contents
Photos by
- Simon Abrams https://unsplash.com/@flysi3000
P.S.
If you want more articles (like this) on this blog, then post a comment below and subscribe to me. Thanks! 😻
❗️ You can support me on Boosty, both on a permanent and on a one-time basis. All proceeds from this way will go to support my OSS projects and will energize me to create new products and articles for the community.
And of course, you can help me make developers' lives even better! Just connect to one of my projects as a contributor. It's easy!
My main projects that need your help (and stars) 👇
- 🔥 gowebly: A next-generation CLI tool that makes it easy to create amazing web applications with Go on the backend, using htmx, hyperscript or Alpine.js and the most popular CSS frameworks on the frontend.
- ✨ create-go-app: Create a new production-ready project with Go backend, frontend and deploy automation by running one CLI command.
Other my small projects: yatr, gosl, json2csv, csv2api.
Từ khóa » Vps Golang
-
Cài đặt Và Sử Dụng Golang Trên Linux - Tài Liệu 123Host
-
Upload And Deploy Golang Application To VPS - Stack Overflow
-
Cài đặt Tự động Golang Trên Linux - Viblo
-
Deploy Golang Application On Digital Ocean Server Ubuntu 16.04
-
How To Deploy Go Lang Application On VPS Server - Value In Brief
-
20X Faster Golang Web Hosting - A2 Hosting
-
Các Dịch Vụ Tự động Mở Rộng Quy Mô Và Tự Vệ ở Golang - Bizfly Cloud
-
How To Deploy A Go Web Application Using Nginx On Ubuntu 18.04
-
Manhavn - Npm
-
Vps · GitHub Topics
-
Write And Deploy A Golang Web App - VPS Ranked
-
Lập-Trình-Backend-Senior-Middle-Golang-Developer - CareerBuilder
-
Thảo Luận - Tất Tần Tật Về Go (Golang) | Page 48 - Voz
-
Golang - VinaSupport