Sunday, September 22, 2019

How To Setup Go Web Application Using Apache on Ubuntu 18.04

Video Tutorial- https://youtu.be/t9Wl5hyGJec

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Go is an open source programming language developed by a team at Google. It provides easy to build simple, reliable, and efficient software.
Apache is the most widely used web server software.               
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Our Server Specification-    
Os:Ubuntu 18.04 LTS Bionic Beaver 64Bit   |IP address- 192.168.1.50  | Hostname :www.yourdomain.com
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
lsb_release -cd ; hostname ; hostname -I ; whoami ; getconf LONG_BIT ; apt install -y build-essential software-properties-common curl gdebi net-tools wget curl sqlite3 dirmngr nano lsb-release apt-transport-https leafpad git sudo unzip socat bash-completion checkinstall imagemagick openssl

curl -O https://dl.google.com/go/go1.12.1.linux-amd64.tar.gz
tar -xvf go1.12.1.linux-amd64.tar.gz -C /usr/local
chown -R root:root /usr/local/go ; mkdir -p $HOME/go/{bin,src} ; nano ~/.profile
export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin
export PATH=$PATH:$GOPATH/bin:/usr/local/go/bin
. ~/.profile
echo $PATH ; go version

mkdir $GOPATH/go-web ; cd $GOPATH/go-web
nano main.go
package main

import (
    "fmt"
    "net/http"
)

func main() {
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "Hello World")
    })

    http.HandleFunc("/greet/", func(w http.ResponseWriter, r *http.Request) {
        name := r.URL.Path[len("/greet/"):]
        fmt.Fprintf(w, "Hello %s\n", name)
    })

    http.ListenAndServe(":9990", nil)
}

go build main.go
nano /lib/systemd/system/goweb.service
[Unit]
Description=goweb

[Service]
Type=simple
Restart=always
RestartSec=5s
ExecStart=/root/go/go-web/main


[Install]
WantedBy=multi-user.target

systemctl daemon-reload
service goweb start ; service goweb status

apt-get install apache2 -y
a2enmod proxy proxy_http
leafpad /etc/apache2/sites-available/yourdomain.conf &>/dev/null
<VirtualHost *:80>
    ServerName yourdomain.com
    ServerAdmin admin@yourdomain.com
    ProxyPreserveHost On
    ProxyPass / http://127.0.0.1:9990/
    ProxyPassReverse / http://127.0.0.1:9990/
    TransferLog /var/log/apache2/yourdomain_access.log
    ErrorLog /var/log/apache2/yourdomain_error.log
</VirtualHost>

a2ensite yourdomain ; a2dissite 000-default.conf  ; apache2ctl configtest ; echo "192.168.1.50 www.yourdomain.com" >> /etc/hosts ; systemctl reload apache2
http://www.yourdomain.com

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

1 comment:

  1. hi I have this problemç
    root@tes-server:/lib/systemd/system# systemctl start goweb
    root@tes-server:/lib/systemd/system# systemctl status goweb
    ● goweb.service - Go Server
    Loaded: loaded (/lib/systemd/system/goweb.service; enabled; vendor preset: enabled)
    Active: activating (auto-restart) (Result: exit-code) since Tue 2020-06-16 09:21:19 UTC; 1s ago
    Process: 5836 ExecStart=/root/victoryDash/webapp (code=exited, status=2)
    Main PID: 5836 (code=exited, status=2)

    Jun 16 09:21:19 tes-server systemd[1]: goweb.service: Main process exited, code=exited, status=2/INVALIDARGUMENT
    Jun 16 09:21:19 tes-server systemd[1]: goweb.service: Failed with result 'exit-code'.

    I put this in goweb.service
    [Unit]
    Description=Go Server

    [Service]
    Type=simple
    Restart=always
    RestartSec=5s
    ExecStart=/root/victoryDash/webapp

    [Install]
    WantedBy=multi-user.target



    when run the binary in their folder is ok
    root@tes-server:~/victoryDash# ./webapp
    2020/06/16 09:16:15 The server is lisening on 3000 port


    do you have some idea where is the problem

    ReplyDelete