Install Fiber Server with Golang on AlmaLinux

By Chandrashekhar Fakirpure

Updated on Feb 02, 2024

In this article, we will show how to install Fiber Server with Golang on AlmaLinux 9.

Fiber is a Go web framework built on top of Fast http, the fastest HTTP engine for Go. It’s designed to ease things up for fast development with zero memory allocation and performance in mind. It features an efficient HTTP engine, a growing middleware community, and an easy-to-use Express-inspired API.

We have covered installation of Fiber Server with Golang on AlmaLinux 9.

Prerequisites

  • A AlmaLinux 9 installed dedicated server or KVM VPS.
  • A root user access or normal user with sudo privileges.

Step 1 - Update The Server

Keep the server up to date. Use following command to update the Ubuntu server.

dnf update -y

Step 2 - Install Golang

First, we will download Go using wget command and extract it in /usr/local path. If wget command not found in your server, you can install it using this command dnf install wget -y.

wget https://go.dev/dl/go1.20.2.linux-amd64.tar.gz
tar zxvf go1.20.2.linux-amd64.tar.gz -C /usr/local

At the time of writing this guide, the latest available version was 1.20.2. You can check the latest Go version from the Go official download page.

Setup Environment variables

The Go’s runtime and build executables are now available under /usr/local/go/bin. Add the executable path to PATH environment variable. Add the GOROOT environment variable referencing your local Go installation. Use thesource command to reload the updated values.

echo 'export GOROOT=/usr/local/go' | sudo tee -a /etc/profile
echo 'export PATH=$PATH:/usr/local/go/bin' | sudo tee -a /etc/profile
source /etc/profile

To verify the installation, we can check the version of the Go by using following command:

go version

Output will show similar like:

go version go1.20.2 linux/amd64

Step 3 - Initialize the Project

Create the project directory.

mkdir fiberserver && cd fiberserver

Create a Go module using the go mod command.

go mod init fiberserver

Install Fiber.

go get github.com/gofiber/fiber/v2

Create a main Golang file in the project directory.

vi main.go

Copy and paste following content in the main.go file.

package main

import "github.com/gofiber/fiber/v2"

func main() {
  app := fiber.New()

  app.Get("/", func(c *fiber.Ctx) error {
    return c.SendString("Hello, World!")
  })

  app.Listen(":3000")
}

If you are a root user you can modify the app.Listen port to 80. You can set up a proxy server like Nginx too.

Step 4 - Configure firewall

Let’s configure firewalld.

firewall-cmd --add-port=3000/tcp --permanent
firewall-cmd --reload

Note: Add the post which one you have set in main.go app.Listen(“:3000”).

Step 3 - Start The Server

Compile the project using following command:

go build

Run the compiled binary file:

./fiberserver

Output:

 

 ┌───────────────────────────────────────────────────┐ 
 │                   Fiber v2.42.0                   │ 
 │               http://127.0.0.1:3000               │ 
 │       (bound on host 0.0.0.0 and port 3000)       │ 
 │                                                   │ 
 │ Handlers ............. 2  Processes ........... 1 │ 
 │ Prefork ....... Disabled  PID .............. 5540 │ 
 └───────────────────────────────────────────────────┘ 

Navigate to your server’s IP address at port 3000 in a web browser. For example:

http://192.0.2.123:3000

Basic Routing

Routing refers to determining how an application responds to a client request to a particular endpoint, which is a URI (or path) and a specific HTTP request method (GET, PUT, POST and so on).

Simple route

// Respond with "Hello, World!" on root path, "/"
app.Get("/", func(c *fiber.Ctx) error {
  return c.SendString("Hello, World!")
})

Parameters

// GET http://localhost:8080/hello%20world

app.Get("/:value", func(c *fiber.Ctx) error {
  return c.SendString("value: " + c.Params("value"))
  // => Get request with value: hello world
})

Optional parameter

// GET http://localhost:3000/john

app.Get("/:name?", func(c *fiber.Ctx) error {
  if c.Params("name") != "" {
    return c.SendString("Hello " + c.Params("name"))
    // => Hello john
  }
  return c.SendString("Where is john?")
})

Wildcards

// GET http://localhost:3000/api/user/john

app.Get("/api/*", func(c *fiber.Ctx) error {
  return c.SendString("API path: " + c.Params("*"))
  // => API path: user/john
})

Static files

To serve static files such as images, CSS, and JavaScript files, replace your function handler with a file or directory string.

Function signature:

app.Static(prefix, root string)

Use the following code to serve files in a directory named ./public:

app := fiber.New()

app.Static("/", "./public") 

app.Listen(":3000")

Now, you can load the files that are in the ./public directory:

http://localhost:8080/hello.html
http://localhost:8080/js/jquery.js
http://localhost:8080/css/style.css

We have successfully installed and deployed Fiber server with Golang.

We have seen, how to install Fiber Server with Golang on AlmaLinux 9. Here is Fiber server official Documentation.