[LINUX] Easy HTTP server and Systemd autostart settings in Go

On this page, I will post a simple HTTP server code in Go, and systemd will automatically start the program of the Linux (Ubuntu, Archlinux) service.

Simple HTTP server code in Go

$ pwd
$HOME/go/src/SimpleHttp

$ cat SimpleHttp.go
package main

import (
        "fmt"
        "net/http"
)

func SimpleHttpHandler(w http.ResponseWriter, r *http.Request) {
        fmt.Fprint(w, "Simple HTTP server&Installation of Systemd Service")
}

func main() {
        http.HandleFunc("/", SimpleHttpHandler)

        //Start on port 8080
        http.ListenAndServe(":8080", nil)
}

Build SimpleHttp

$ pwd
$HOME/go/src/SimpleHttp

$ tree
.
└── SimpleHttp.go

0 directories, 1 file

$ go build SimpleHttp.go

$ tree
.
├── SimpleHttp
└── SimpleHttp.go

0 directories, 2 files

Contents of the unit file SimpleHttp.service

$ cat /lib/systemd/system/SimpleHttp.service 
[Unit]
Description = SimpleHttp.service daemon

[Service]
Environment="GOPATH=$HOME/go"
ExecStart=$HOME/go/src/SimpleHttp/SimpleHttp
Restart=always
Type=simple
User=ubuntu
Group=ubuntu
# Other directives omitted
# (file size)
LimitFSIZE=infinity
# (cpu time)
LimitCPU=infinity
# (virtual memory size)
LimitAS=infinity
# (open files)
LimitNOFILE=64000
# (processes/threads)
LimitNPROC=64000

[Install]
WantedBy = multi-user.target

Reloading the SimpleHttp service

Always run the following command after creating or modifying a new unit file, SimpleHttp.service.

sudo systemctl daemon-reload

Enable SimpleHttp service

$ sudo systemctl enable SimpleHttp.service

Start SimpleHttp service

$ sudo systemctl start SimpleHttp.service

Stop SimpleHttp service

sudo systemctl stop SimpleHttp.service

Disabling the SimpleHttp service

$ sudo systemctl disable SimpleHttp.service

Check Arch Linux version

$ uname -a
Linux arch.local 5.6.10-arch1-1 #1 SMP PREEMPT Sat, 02 May 2020 19:11:54 +0000 x86_64 GNU/Linux

$ lsb_release -a
LSB Version:	1.4
Distributor ID:	Arch
Description:	Arch Linux
Release:	rolling
Codename:	n/a

Check Ubuntu version

$ uname -a
Linux ip-172-31-8-186 5.3.0-1017-aws #18~18.04.1-Ubuntu SMP Wed Apr 8 15:12:16 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

$ cat /etc/lsb-release 
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=18.04
DISTRIB_CODENAME=bionic
DISTRIB_DESCRIPTION="Ubuntu 18.04.4 LTS"

$ lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 18.04.4 LTS
Release:	18.04
Codename:	bionic

Recommended Posts

Easy HTTP server and Systemd autostart settings in Go
Create a web server in Go language (net / http) (1)
Easy HTTP server with Python
Tomcat installation and autostart settings
Write an HTTP / 2 server in Python
Network settings and confirmation in CentOS7
Easy server monitoring with AWS Lambda (Python) and result notification in Slack
Build a lightweight server in Python and listen for Scratch 2 HTTP extensions
Implement and understand union-find trees in Go
Implemented memoization recursion and exploration in Python and Go
Y / n processing in bash, python and Go
Launch an HTTP server in the current directory
Create initial settings and staff apps in Django
GRPC starting with Go server and Dart client
Create a web server in Go language (net/http) (2)