Skip to content

Instantly share code, notes, and snippets.

@Dev-Dipesh
Last active June 24, 2020 20:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dev-Dipesh/a4a064bab31625217383f66fbae8febd to your computer and use it in GitHub Desktop.
Save Dev-Dipesh/a4a064bab31625217383f66fbae8febd to your computer and use it in GitHub Desktop.
Golang and Redis Setup on Amazon 64 bit AMI Linux

Update Server

sudo yum -y update
sudo yum -y install gcc make

Golang Installation

Download Go binary

curl -LO https://storage.googleapis.com/golang/go1.7.3.linux-amd64.tar.gz

Although the tarball came from a genuine source, it is best practice to verify both the authenticity and integrity of items downloaded from the Internet. This verification method certifies that the file was neither tampered with nor corrupted or damaged during the download process. The sha256sum command produces a unique hash:

sha256sum go1.7*.tar.gz
OUTPUT
508028aac0654e993564b6e2014bf2d4a9751e3b286661b0b0040046cf18028e  go1.7.3.linux-amd64.tar.gz

Compare the hash in your output to the checksum value on the Go download page. If they match, then it is safe to conclude that the download is legitimate.

Installation

sudo tar -C /usr/local -xvzf go1.7.3.linux-amd64.tar.gz

Note: The publisher officially recommends placing Go in the /usr/local directory. Installing it in another location does not impact its usability, but the custom path would need to be defined in the Go environment variable, GOROOT. The next step discusses working with environment variables.

Create bin, pkg and src directories in a workspace directory under your home directory

mkdir -p ~/go-projects/{bin,pkg,src}

The bin directory will contain executable programs compiled from the human-readable source files in the src directory. The pkg directory stores package objects, which is reusable code shared between programs.

Setting Paths for Go

Create a path.sh script in the /etc/profile.d directory using the vi editor:

sudo vi /etc/profile.d/path.sh
export PATH=$PATH:/usr/local/go/bin

Additionally, define the GOPATH and GOBIN Go environment variables in your user’s .bash_profile file to point to the recently created workspace. The GOPATH variable tells Go the location of your source files, while the GOBIN variable instructs it where to create the compiled binary files.

Add the following to the end of the file, save and exit:

export GOBIN="$HOME/go-projects/bin"
export GOPATH="$HOME/go-projects/src"

Reload Updated Profiles

source /etc/profile && source ~/.bash_profile

You can check your path value using:

env | grep PATH

To test the installation with a simple program, check the last section of this gist.

Redis Installation

Download and Extract Binary

cd /usr/local/src

sudo wget http://download.redis.io/releases/redis-3.2.0.tar.gz
sudo tar xzf redis-3.2.0.tar.gz
sudo rm -f 3.2.0.tar.gz

Install Redis

cd redis-3.2.0
sudo make distclean
sudo make

Install tcl to Test Redis Installation

sudo yum install -y tcl
sudo make test

Make Directories and Copy Files

sudo mkdir -p /etc/redis /var/lib/redis /var/redis/6379
sudo cp src/redis-server src/redis-cli /usr/local/bin
sudo cp redis.conf /etc/redis/6379.conf

Open Config File and Config Redis

sudo vim /etc/redis/6379.conf
  bind 127.0.0.1                                //line 61
  daemonize yes                                 //line 127
  logfile "/var/log/redis_6379.log"             //line 162
  dir /var/redis/6379                           //line 246

Download and install the init script

sudo wget https://raw.githubusercontent.com/saxenap/install-redis-amazon-linux-centos/master/redis-server
sudo mv redis-server /etc/init.d
sudo chmod 755 /etc/init.d/redis-server

Open the Redis server init script with vim

sudo vim /etc/init.d/redis-server
  REDIS_CONF_FILE="/etc/redis/6379.conf"                 //line 26

Auto-enable and start the Redis server

sudo chkconfig --add redis-server
sudo chkconfig --level 345 redis-server on
sudo service redis-server start

Add the following lines to ensure background saves and fix low-memory issue. This is a new file.

sudo vim /etc/systctl.conf
# ensure redis background save issue
  vm.overcommit_memory = 1
  systctl vm.overcommit_memory=1

Test Redis Server

redis-cli ping

Response Should be PONG


Creating a Golang Program

To get started, create a new .go file:

vi ~/go-projects/src/hello.go

The code below uses the main Go package, imports the formatted IO content component, and sets a new function to print the string Hello, World!. Add the following to the file:

package main

import "fmt"

func main() {
    fmt.Printf("Hello, World!\n")
}

Then, save and exit the file.

Next, compile the hello.go source file with the go install command:

go install $GOPATH/hello.go

We are now ready to run our program:

$GOBIN/hello

The hello.go program should produce a Hello, World! message, confirming a successful installation of Go.

Thanks! Feel free to comment for any queries or updates.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment