一覧
状態:-
閲覧数:651
投稿日:2018-04-30
更新日:2018-05-08
内容
「CentOS7 / Nginx / コマンド起動 / Go」 / 2018/4/28
「CentOS7 / Nginx / CentOS7サービス経由で起動 / Go」 / 2018/4/29 → 2018/5/6
「CentOS7 / Nginx / CentOS7サービス経由で起動(1アプリ毎に1サービス登録) / Go」 / 2018/5/6 →2018/5/7
「CentOS7 / Nginx / CentOS7サービス経由で起動(複数アプリを1サービスへ登録) / Go」 / 2018/5/7 →
「CentOS7 / Nginx / コマンド起動 / Go」 / 2018/4/28
Nginx
▼/etc/nginx/conf.d/go-demo.w4c.work.conf
server {
listen 80;
server_name go-demo.w4c.work;
root /var/www/◇◇/go-demo.w4c.work;
#デフォルト 60秒を120秒までアップ
fastcgi_read_timeout 120;
location / {
fastcgi_pass unix:/run/go/go-fcgi.sock;
include fastcgi_params;
}
}
Go
下記ファイルは動作確認できたので、既に削除済
▼/var/www/◇◇/go-demo.w4c.work/index.go
package main
import (
"os"
"fmt"
"net"
"net/http"
"net/http/fcgi"
)
func handler(res http.ResponseWriter, req *http.Request) {
fmt.Fprint(res, "Hello World!")
}
func main() {
os.RemoveAll("/run/go/go-fcgi.sock")
l, err := net.Listen("unix", "/run/go/go-fcgi.sock")
if err != nil {
return
}
os.Chmod("/run/go/go-fcgi.sock", 0777)
if err := os.Chown("/run/go/go-fcgi.sock", 994, 992); err != nil {//uid=994,gid=992
fmt.Println("chown not permitted")
}
http.HandleFunc("/", handler)
fcgi.Serve(l, nil)
}
コマンド起動
$ sudo go run index.go
「CentOS7 / Nginx / CentOS7サービス経由で起動 / Go」 / 2018/4/29 → 2018/5/6
Nginx
▼/etc/nginx/conf.d/go-demo.w4c.work.conf
server {
listen 80;
server_name go-demo.w4c.work;
root /var/www/◇◇/go-demo.w4c.work;
#デフォルト 60秒を120秒までアップ
fastcgi_read_timeout 120;
location ~ ^/demo.*/(.*)\.go$ {
fastcgi_pass unix:/run/go/app/$1.sock;
include fastcgi_params;
}
}
実行可能ファイルを生成
$ go build goweb.go
実行可能ファイルを配置
▼/opt/goweb
CentOS7サービス登録
Unit定義ファイルを作成
▼/etc/systemd/system/goweb.service
[Unit]
Description = go web start
[Service]
ExecStart = /opt/goweb
Restart = always
Type = simple
[Install]
WantedBy = multi-user.target
CentOS7サービス設定
自動起動on
$ sudo systemctl enable goweb
起動
$ sudo systemctl start goweb
「CentOS7 / Nginx / CentOS7サービス経由で起動(1アプリ毎に1サービス登録) / Go」 / 2018/5/6 →2018/5/7
実行可能ファイルを生成&配置
$ sudo go build -o /opt/go/a1 a1.go
$ sudo go build -o /opt/go/a2 a2.go
$ sudo -s
CentOS7サービス登録
Unit定義ファイルを作成
▼/etc/systemd/system/goa1.service
# vi /etc/systemd/system/goa1.service
[Unit]
Description = go web start
[Service]
ExecStart = /opt/go/a1
Restart = always
Type = simple
[Install]
WantedBy = multi-user.target
▼/etc/systemd/system/goa2.service
# vi /etc/systemd/system/goa2.service
[Unit]
Description = go web start
[Service]
ExecStart = /opt/go/a2
Restart = always
Type = simple
[Install]
WantedBy = multi-user.target
CentOS7サービス設定
自動起動on
$ sudo systemctl enable goa1.service
$ sudo systemctl enablegoa2.service
再起動
# systemctl restart goa1.service
# systemctl restart goa2.service
デプロイ
編集する度に、下記作業が必要
$ cd /var/www/◇◇/go-demo.w4c.work/demo
$ sudo go build -o /opt/go/a1 a1.go
$ sudo go build -o /opt/go/a2 a2.go
$ systemctl restart goa1.service
$ systemctl restart goa2.service
「CentOS7 / Nginx / CentOS7サービス経由で起動(複数アプリを1サービスへ登録) / Go」 / 2018/5/7 →
実行可能ファイルを生成&配置
$ sudo go build -o /opt/go/a1 a1.go
$ sudo go build -o /opt/go/a2 a2.go
$ sudo -s
CentOS7サービス登録
Unit定義ファイルを作成
▼/etc/systemd/system/goweb.service
# vi /etc/systemd/system/goweb.service
[Unit]
Description = go web start
[Service]
Type=forking
WorkingDirectory=/opt/go
ExecStart=/bin/bash -c 'for x in *; do ./$x & done'
Restart = always
[Install]
WantedBy = multi-user.target
CentOS7サービス設定
自動起動on
$ sudo systemctl enable goweb.service
再起動
# systemctl restart goweb.service
デプロイ
編集する度に、下記作業が必要
$ cd /var/www/◇◇/go-demo.w4c.work/demo
$ sudo go build -o /opt/go/a1 a1.go
$ sudo go build -o /opt/go/a2 a2.go
$ systemctl restart goweb.service