Serve landing page with Go Iris backend on CentOS

For one of my current project, I should serve landing page with Go Iris backend. So under more tage you can find a ready solution from me and deep explanation of how to prepare it for production.

Requirements and constraints

  • Server with CentOS 7.5. I used for it digitalocean.com
  • Golang 1.11
  • All actions in this article done under root user

Install and configure Go

Get the needed version of Go

For this guide, I use Golang 1.11. In this article I get it by curl  instead of wget in my previous article:

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

Install Golang

Unpack it to /usr/local. Please do not change destination directory because it is the recommended path from the publisher:

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

Make needed dirs

For proper work of the Go, we need to create a workspace directory with bin, pkg and src subdirectories. I will use ~/workspace/go for it:

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

Setting paths for Go

Firstly we need to add /usr/local/go/bin to the $PATH variable by creating a custom sript in the /etc/profile.d directory:

vi /etc/profile.d/path.sh

and add to it following line:

export PATH=$PATH:/usr/local/go/bin

Also we should create the GOPATH and GOBIN variables in own user’s .bash_profile. Open it:

vi ~/.bash_profile

and add the following lines at the end of the file:

export GOBIN="$HOME/workspace/go/bin"
export GOPATH="$HOME/workspace/go"

That’s it. To apply the changes to your current session use source command instead simple reboot:

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

You can check variables for go using:

go env

In my case I get somethig like this:

GOARCH="amd64"
GOBIN="/root/workspace/go/bin"
GOCACHE="/root/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/root/workspace/go/src"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build135090936=/tmp/go-build -gno-record-gcc-switches"

Use golanding for serve a landing page

I’m developing for my purposes golanding – simple backend for serving static pages. I have app

For gettings things done we should install the git which needed for go get command:

yum install git 

Now we can get golanding from github by a simple command:

 go get -u github.com/danilborchevkin/golanding

Remember, own sourses placed in ~/workspace/go/src. Change current directory:

cd ~/workspace/go/src/github.com/danilborchevkin/golanding/

At this time we can build current project by typing:

go build

After this in current directory appears golanding executable file which we can run as any other program:

./golanding

But we will get an error linked with an absent .env file. About setup, you may check golanding Github Page but in a simple case you just need to create .env by copying .env.example:

cp .env.example .env

and change needed variables. Now we can start the golanding and see provided Coming Soon page on http://localhost:80

./golanding

You may change .env for your needs

Prepare to production

Install golanding

inside golanding directory run:

go install

There are some differences between go build and go install but the main difference is that the go install build and place binary file to GOBIN directory (we set up is as ~/workspace/go/bin):

Put golanding in /opt directory

There are opinions that placing apps to home way is a bad way so we place golanding to /opt with .env file:

mkdir /opt/golanding
cp ~/workspace/go/bin/golanding /opt/golanding/golanding
cp ~/workspace/go/src/github/danilborchevkin/golanding/.env /opt/golanding/.env

Place landing page to /var/www directory

Good way so save your static pages is putting it to /var/www directory. If it not exist – create it.

Select directory fo UPLOAD_PATH

I think that put the uploads to /var/tmp is a good idea because the files inside /var/tmp directory periodically deleted. So problem about overflow is vanished by this feature.

Change .env file

We need to fix .env file because we put the landing page onto /var/www and for the uploads we start use /var/tmp.

Daemonize golanding

There are a lot of ways doing it (supervisor, forever, pmgo) but my choice is create a service for systemd. It’s quite simple.

Let’s create file /usr/lib/systemd/system/golanding.service with following content:

[Unit]
Description=golanding service
After=network.target

[Service]
Type=simple
User=root
WorkingDirectory=/opt/golanding
ExecStart=/opt/golanding/golanding
Restart=always

[Install]
WantedBy=multi-user.target

After this we need to enable and start own service:

systemctl status golanding
systemctl enable golanding
systemctl start golanding
systemctl status golanding

Cool – no dependencies=))

This is not the end

Manual deploy is a bad way if you need some agility. For your project will be better if you setup CI/CD for your project. Also, you need to check all security issues and the systemd’s service options – all of it should be revised and improved by yourself.

Option – firewall installation and setup

Seems I put it in my everything article about DevOps. Even for PostgreSQL on CentOS. It’s usual:

yum install firewalld
systemctl enable firewalld
systemctl start firewalld
firewall-cmd --permanent --zone=public --add-service=http

Conclusion

I spent 16 hours for this article and coding. If it save your time then I will be happy. Let me know about it=))

Useful links

Your or your company have similar projects or tasks which need to be done?

I offer services linked with things which mentioned in this article. Please write to me by this feedback form

Published by

Danil Borchevkin

IoT Full-Stack Developer

Leave a Reply