Set up Fiber Server with Golang on AlmaLinux 8

In this article, we want to teach you how to Set up a Fiber Server with Golang on AlmaLinux 8.

Fiber is an Express-inspired web framework built on top of Fasthttp, the fastest HTTP engine for Go. Designed to ease things up for fast development with zero memory allocation and performance in mind.

How To Set up Fiber Server with Golang on AlmaLinux 8

Before you start to set up a Fiber server you need to log in to your server as a non-root user with sudo privileges and set up a basic firewall. To do this, you can follow our article about the Initial Server Setup with AlmaLinux 8.

Now follow these steps to install Fiber Server with Golang.

Install Golang on AlmaLinux 8

First, update your local package index with the following command:

sudo dnf update

Then, use the wget tool to download the Go on AlmaLinux 8, you can check the latest version from the Go downloads page and replace it in the command below:

wget https://golang.org/dl/go1.17.3.linux-amd64.tar.gz

Here extract the tar file in the /usr/local path with the following command:

tar -zxvf go1.17.3.linux-amd64.tar.gz -C /usr/local

At this point, you need to add the executable path to the PATH environment variable. To do this, run the following commands:

# echo ‘export GOROOT=/usr/local/go’ | sudo tee -a /etc/profile
# echo ‘export PATH=$PATH:/usr/local/go/bin’ | sudo tee -a /etc/profile

Reload the updated values with the following command:

source /etc/profile

Verify your installation by checking the Go version:

go version

In your output you will see:

Output
go version go1.17.3 linux/amd64

Now let’s see how to install the Fiber server on AlmaLinux 8.

Install and Configure Fiber Server with Golang on AlmaLinux 8

First, create the project directory and switch to it with the following command:

mkdir fiberserver && cd fiberserver

Then, you need to create a Go module with the go mod command:

go mod init fiberserver

Now install the Fiber server on AlmaLinux 8 with the following command:

go get github.com/gofiber/fiber/v2

Next, create a main Golang file in the project directory with your favorite text editor, here we use vi:

vi main.go

Add the following content to the 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") 
}

When you are done, save and close the file.

Note: 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.

Now you need to configure the firewall. we assume that you have enabled the firewalld from the requirements. Run the command below:

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

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

Reload the firewall to apply the new rules:

sudo firewall-cmd --reload

Now compile the project with the following command:

go build

Then run the Fiber server file on AlmaLinux 8 with the following command:

./fiberserver

In your output you will see:

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

At this point, in your web browser type your IP address followed by 3000:

http://server-ip-address:3000

You will see:

Fiber server test page

If you see this page means that you have set up the Fiber server correctly on your AlmaLinux 8.

Conclusion

At this point, you learn to Install and Configure a Fiber server with Golang on AlmaLinux 8.

Hope you enjoy it.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles

POPULAR TAGS

Most Popular