Install Fiber Server with Golang on Rocky Linux 8

In this guide, we want to teach you How To Install Fiber Server with Golang on Rocky Linux 8.

Go is a general-purpose language. You can use Go to build web apps, microservices, cloud services, APIs, DevOps tooling, and any application.

Go Fiber is an Express-inspired framework for Golang. Go Fiber is a web framework built on top of fast HTTP. It can be used to handle operations such as routing/endpoints, middleware, server request, etc.

Steps To Install Fiber Server with Golang on Rocky Linux 8

To Set up a Fiber server with Golang, you must 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 guide the Initial Server Setup with Rocky Linux 8.

Now follow the steps below to complete this guide.

Install Golang on Rocky Linux 8

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

sudo dnf update

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

wget https://go.dev/dl/go1.19.linux-amd64.tar.gz

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

tar -zxvf go1.19.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:

# export GOROOT=/usr/local/go
# export GOPATH=$HOME/go
# export PATH=$GOPATH/bin:$GOROOT/bin:$PATH

Reload the updated values with the following command:

source ~/.bashrc

Verify your installation by checking the Go version:

go version

In your output you will see:

Output
go version go1.19 linux/amd64

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

Set up Fiber Server on Rocky Linux 8

The first step is to initialize the project.

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
Output
go: creating new go.mod: module fiberserver

Now install the Fiber server on Rocky Linux 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.

Configure Firewall

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

Run Fiber Server

Now compile the project with the following command:

go build

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

./fiberserver

In your output you will see:


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

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

http://server-ip-address:3000

You will see the Hello, World! massage.

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

Conclusion

At this point, you learn to Install Fiber Server with Golang on Rocky Linux 8.

Hope you enjoy it.

You may be like these articles:

Install and Configure Nextcloud on Rocky Linux 8

How To Install LEMP Stack on AlmaLinux 8

Install and Secure Redis on AlmaLinux 8

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Leave a Reply

Stay informed and not overwhelmed, subscribe now!