Install Fiber Server with Golang on Rocky Linux 8

Posted on

Install Fiber Server with Golang on Rocky Linux 8

Install Fiber Server with Golang on Rocky Linux 8

This guide, brought to you by Orcacore, will walk you through the process of setting up a Fiber server using Golang on Rocky Linux 8. Go is a versatile programming language suitable for a wide range of applications, including web apps, microservices, cloud services, APIs, and DevOps tools.

Go Fiber is a web framework inspired by Express, designed for Golang. Built on top of Fasthttp, it offers speed and efficiency in handling web development tasks like routing, middleware, and server requests. Let’s explore how to Install Fiber Server with Golang on Rocky Linux 8.

Before proceeding, ensure you have a non-root user with sudo privileges and a basic firewall configured on your Rocky Linux 8 server. You can refer to our guide on "Initial Server Setup with Rocky Linux 8" for assistance with this prerequisite.

Now, let’s dive into the steps:

1. Install Golang on Rocky Linux 8

First, update your system’s package index:

sudo dnf update -y

Next, download the Go distribution using wget. Visit the Go downloads page to find the latest version and replace it in the command below:

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

Extract the downloaded archive to the /usr/local directory:

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

Now, add the Go executable path to your system’s PATH environment variable. You can do this by adding the following lines to your ~/.bashrc file:

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

Reload your .bashrc file to apply the changes:

source ~/.bashrc

Verify the installation by checking the Go version:

go version

The output should resemble:

**Output**
go version go1.19 linux/amd64

With Go installed, let’s proceed to setting up the Fiber server.

2. Set up Fiber Server on Rocky Linux 8

Start by creating a project directory and navigating into it:

mkdir fiberserver && cd fiberserver

Initialize a Go module within the project directory:

go mod init fiberserver
create a Go module

Install the Fiber framework:

go get github.com/gofiber/fiber/v2

Create a main.go file in your project directory using your preferred text editor (e.g., vi):

vi main.go

Add the following code to main.go:

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")
}

Save and close the file.

Note: If you are running as root, you can change the app.Listen port to 80. Alternatively, you can use a reverse proxy like Nginx.

3. Configure Firewall for Go Fiber

Configure your firewall to allow traffic on the port your Fiber server is listening on. Assuming you have firewalld enabled, run:

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

Note: Ensure the port number matches the one specified in your main.go file.

Reload the firewall to apply the changes:

sudo firewall-cmd --reload

4. Run Fiber Server

Compile your Go application:

go build

Run the compiled Fiber server executable:

./fiberserver

The output will look something like this:

Run Fiber Server

Open your web browser and navigate to your server’s IP address followed by port 3000:

http://server-ip-address:3000

You should see the "Hello, World!" message. This confirms that your Fiber server is running correctly on Rocky Linux 8. Now you know how to Install Fiber Server with Golang on Rocky Linux 8.

Conclusion

In conclusion, installing a Fiber server with Golang on Rocky Linux 8 offers a straightforward and efficient way to develop high-performance web applications. Its lightweight nature and speed make it an excellent choice for Go developers. Hopefully you can now easily Install Fiber Server with Golang on Rocky Linux 8.

Alternative Solutions

While the above method using go get is common, here are two alternative approaches to setting up a Fiber server with Golang on Rocky Linux 8, focusing on dependency management and containerization:

1. Using Go Modules with Vendoring

Vendoring involves copying your project’s dependencies into a vendor directory within your project. This ensures that your application always uses the specific versions of dependencies that it was tested with, regardless of what’s available in the global module cache. This is particularly useful for production deployments.

Steps:

  1. Initialize the Go module (if you haven’t already):

    go mod init fiberserver
  2. Download the dependencies:

    go get github.com/gofiber/fiber/v2
  3. Vendor the dependencies:

    go mod vendor

This command creates a vendor directory in your project root, containing all the dependencies listed in your go.mod file. When you build your application, Go will prioritize dependencies in the vendor directory over those in the global module cache.

Code Remains the Same: The main.go file remains identical to the original example:

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")
}

Benefits:

  • Reproducible Builds: Ensures that your application builds consistently across different environments.
  • Dependency Isolation: Prevents conflicts between different projects that might use different versions of the same dependencies.

2. Containerizing with Docker

Using Docker allows you to package your application and its dependencies into a single container, making it easy to deploy and run in any environment that supports Docker.

Steps:

  1. Create a Dockerfile in your project root:

    FROM golang:1.19-alpine AS builder
    
    WORKDIR /app
    
    COPY go.mod go.sum ./
    RUN go mod download && go mod verify
    
    COPY . .
    
    RUN go build -o fiberserver
    
    FROM alpine:latest
    WORKDIR /app
    COPY --from=builder /app/fiberserver .
    EXPOSE 3000
    CMD ["./fiberserver"]

    Explanation:

    • FROM golang:1.19-alpine AS builder: Uses the official Go image based on Alpine Linux as the builder stage. Alpine is a lightweight Linux distribution, resulting in smaller Docker images.
    • WORKDIR /app: Sets the working directory inside the container.
    • COPY go.mod go.sum ./: Copies the go.mod and go.sum files, which define your project’s dependencies.
    • RUN go mod download && go mod verify: Downloads the dependencies and verifies their integrity.
    • COPY . .: Copies the rest of your project’s source code.
    • RUN go build -o fiberserver: Builds the Go application.
    • FROM alpine:latest: Starts a new stage using the Alpine Linux base image.
    • COPY --from=builder /app/fiberserver .: Copies the compiled executable from the builder stage.
    • EXPOSE 3000: Exposes port 3000 to the outside world.
    • CMD ["./fiberserver"]: Defines the command to run when the container starts.
  2. Build the Docker image:

    docker build -t fiberserver .
  3. Run the Docker container:

    docker run -p 3000:3000 fiberserver

Code Remains the Same: The main.go file remains identical to the original example:

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")
}

Benefits:

  • Portability: The application can be easily deployed to any environment that supports Docker.
  • Isolation: The application runs in its own isolated container, preventing conflicts with other applications.
  • Scalability: Docker containers can be easily scaled up or down as needed.
  • Reproducibility: The Dockerfile ensures that the application is built and deployed consistently across different environments.

These alternative methods offer enhanced dependency management and deployment options for your Fiber server on Rocky Linux 8. Choosing the right approach depends on your specific needs and project requirements.

Leave a Reply

Your email address will not be published. Required fields are marked *