Golang Web Service
Ставим golang 18 на Debian и создаем на нем веб-сервис с прокси на nginx
Шаг первый. Установка Golang
sudo apt update
wget https://go.dev/dl/go1.18.linux-amd64.tar.gz
sudo tar -zxvf go1.18.linux-amd64.tar.gz -C /usr/local/
Для системной установки
echo "export PATH=/usr/local/go/bin:${PATH}" | sudo tee /etc/profile.d/go.sh
Для текущего пользователя
echo "export PATH=/usr/local/go/bin:${PATH}" | sudo tee -a $HOME/.profile
Проверяем установку
go version
go version go1.18 linux/amd64
Шаг второй. Создаем golang скрипт
package main
import (
"encoding/json"
"fmt"
"github.com/gorilla/mux"
"log"
"net/http"
"os"
"strings"
)
func handlerIndex(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
}
func main() {
http.HandleFunc("/", handlerIndex)
log.Fatal(http.ListenAndServe(":8000", nil))
}
Компилируем файл
go build index.go
Шаг 3. Создаем веб-сервис
Создаем файл сервиса /lib/systemd/system/goweb.service с таким содержимым:
[Unit]
Description=goweb
ConditionPathExists=/var/www/mysite.com/index
After=network.target
[Service]
Type=simple
User=www-data
Group=www-data
StandardOutput=append:/var/log/goweb/goweb.log
StandardError=append:/var/log/goweb/goweb-errors.log
WorkingDirectory=/var/www/mysite.com/
ExecStart=/var/www/mysite.com/index
Restart=on-failure
RestartSec=10
ExecStartPre=/bin/mkdir -p /var/log/goweb
ExecStartPre=/bin/chmod 775 /var/www/mysite.com/index
[Install]
WantedBy=multi-user.target
Сохраняем.
Добавляем в автозапуск
systemctl enable goweb
Запускаем
sudo service goweb start
Проверяем статус.
sudo service goweb status
Шаг 4. Подключаем nginx
cd /etc/nginx/sites-available
Создаем файл mysite.com
server {
server_name mysite.com www.mysite.com;
location / {
proxy_pass http://localhost:8000;
}
}
Создаем линк
sudo ln -s /etc/nginx/sites-available/mysite.com /etc/nginx/sites-enabled/mysite.com
Обновляем nginx
sudo nginx -s reload