How to Build a Simple Cloud Hosted Web Server using Ubuntu and GOLang

Sometimes you want to learn something new but you don’t know where to start.

I’ve been working in the Microsoft world & IIS stack so long, I forget how many other web stacks are out there and how many have past us by. OMG!

I wanted to challenge myself to learn how to setup a Cloud hosted, Linux based, web server to host a website. 

Where do I start? Well, I just started looking stuff up and decided to blog my learning process along the way.

I hope this helps somebody else with the same desire to learn this topic as quickly and painlessly as possible.

Here we go…. 

First off, I was not sure what programming language I wanted to use.

This project will use a Linode cloud hosted server and Ubuntu as our operating system.

I started off thinking about a web server using a LAMP stack. I thought it would be OK to consider other options too.

I hate to assume people know tech jargon so I will stop and explain things as best I can along the way. My intent is to inform, not to annoy.

That being said, what is a stack? 

A stack is just a term used to describe a collection of all the moving parts required to have a modern functional website. Includes server operating system, web server software, a database and a framework/language.

When we hear the term, “Full Stack Developer”, someone is talking about a developer with experience in all of the aspects of the stack.
For example,a LAMP stack is Linux OS + Apache Web Server + MySQL Database + PHP language/framework. 

Other server options are replacing Apache with NginX (Pronounced engine x) or building our own using the GO programming language (GoLang). 

I started off with Apache then went to NginX.

I got NginX to work as a server so I’m leaving that information in this article at the bottom.

The rest of this article is focused on trying to get a very basic Linux Ubuntu + GOLang stack up and running.  No database.

If you and I can get a server up and running, I’m going to celebrate then come back later and add the database.

Let’s do this!

What you need before you start:
If you’re using Windows to connect to your Linux server, use Putty to connect via SSH. Details are in the video. Get Putty here.

SSH is a client/server program that enables secure connection to the SSH server on a remote machine. The SSH command is used for remote execution of login, file transfer between machines and executing other remote commands.
Step 1. Go to https://www.Linode.com and create an account. You will need a credit card. A web server can be run on Linode for about .50 a day so learning can be inexpensive. You can delete the server anytime and just rebuild it in about 5 minutes.

Once you have your account, watch and follow along with this video.

Video: Setting up You Linode Ubuntu Server

Command line commands:
– sudo apt-get update
– sudo apt-get upgrade
– nano /etc/hosts

2. Once, You’ve installed your server. You should take some time to secure it. Watch this video and follow along.

Video: Securing Your Linode Server

Command line commands:
– adduser example_user
– adduser example_user sudo
– ssh-keygen -b 4096 

Installing GO and Use It to Setup a Super Simple Web Server

I tried several times and failed to get Apache running quickly so I moved on to NginX and GOLang since this was supposed to be easy.
Use the two videos above to walk you thru building your server operating system and securing it.  
Then go to https://www.linode.com/docs/development/go/install-go-on-ubuntu/ to get the instruction on how to install GO on Ubuntu.
Then watch and follow along with this video to see how to write your own web server and web application using GO.  It has all the moving parts built in, you won’t believe how easy it is. Give it a try!

It may be working when you click. Here is a simple page up and running.

http://74.207.244.122:8000/about/

 I 

**** Learning Note ****

This is where I’ll stop and relate some Microsoft stack items to what we just did.

When we build sites on the Microsoft stack, we already have a server, Internet Information Services (IIS), it runs on Windows as a background service.  If we’re having issues or make an update to the web.config file, we restart the IIS service.

So we just built a super simple IIS web server using a GO net/http library but we have not learned how to run in in the background as a service on Ubuntu. 

**** Learning Note ****

Once you get your simple server working, you will realize it is only running when you run it at the command line.  If you exit, your server stops.

We can cheat a bit by using the Linux command nohup which is short for “No Hangups”.

We don’t run nohup by itself, it is a supplemental command that tells Linux not to stop the main command, even if the user logs out. 

I won’t cover nohup in detail here I recommend this resource for learning more about the nohup command

We can fool our friends in to thinking we’ve built our server by launching our GO app using nohup but this is not the right way to do this. It needs to be running as a background service (daemon).

You will want to learn how to make your GO server run in the background all the time and restart automatically if the server restarts.

I was stuck on this part for a day or so but thank goodness I know the founder of chapbook.com, who is using GOLang so I hit them up and was  pointed to the link below.

https://fabianlee.org/2017/05/21/golang-running-a-go-binary-as-a-systemd-service-on-ubuntu-16-04/

This link is a good example of how to setup a background service called a daemon in Linux terms. 

We’ll do the example on the site above as an exercise to get some daemon code working before we move on to the next task of making our own server code run as a daemon.

It took me a while to get it working but I did. Took longer than I expected as I’m not used to doing everything remotely via SSH and with the nano text editor.  Feels like bootcamp.

Here is a screenshot of my successful attempt.

Installing NginX Web Server on Ubuntu

This is an older part of blog where I was going to go the NginX route and had a simple working server up. I’ll come back to update this later.

I started trying to do this using a LAMP stack but had too many problems getting the Apache2 web server to work and was getting tired and frustrated.

After staying up until 0230 failing to get the Apache2 server installed on Ubuntu, exhaustion was setting in and I was feeling the need a confidence boost so I started my Linode server over from scratch and tried this video on getting NginX web server installed and had success. Use this video first, it was much easier.

Video: Setting up a Linode Server and Hosting a Website using NginX Web Server

As next step, I followed these instructions to install the GO language and get a hello world app to work.

Next we figure out how to use GO to handle business logic on the web server.

Author: Rick Cable / AKA Cyber Abyss

A 16 year US Navy Veteran with 25+ years experience in various IT Roles in the US Navy, Startups and Healthcare. Founder of FinditClassifieds.com in 1997 to present and co-founder of Sports Card Collector Software startup, LK2 Software 1999-2002. For last 7 years working as a full-stack developer supporting multiple agile teams and products in a large healthcare organization. Part-time Cyber Researcher, Aspiring Hacker, Lock Picker and OSINT enthusiast.

Leave a Reply