安装 Supervisor

apt-get update

apt-get install -y supervisor 

service supervisor start

上传编译后的可执行文件

GOOS=linux GOARCH=amd64 go build main.go

编译完成以后可以 GIT 或者 SCP 将文件上传到服务器。

scp binaryname  user@host.address:/root/go

编写 supervisorctl file 管理应用

# cmd
nano /etc/supervisor/conf.d/main.conf

# file content
[program:main]
directory=/root/go/go-gin-example-start
command=/root/go/go-gin-example-start/main
autostart=true
autorestart=true
stderr_logfile=/root/go/go-gin-example-start/main.err
stdout_logfile=/root/go/go-gin-example-start/main.log

# reload
supervisorctl reload

# status
supervisorctl status 

应用迭代后操作 Supervisor。

# start
supervisorctl start main

# stop
supervisorctl stop main

# restart
supervisorctl restart main

使用 Nginx 代理

# install
apt install -y nginx

# add site
nano /etc/nginx/sites-available/main

# file content
upstream gogin {
    server 127.0.0.1:8214;
    keepalive 64;
}
server {
    listen 80;
    server_name example.com service.example.com;
    location / {
        proxy_set_header    X-Real-IP $remote_addr;
        proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header    Host  $http_host;
        proxy_set_header    X-Nginx-Proxy true;
        proxy_set_header    Connection "";
        proxy_pass http://gogin;
    }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
        expires 30d;
    }
    location ~ .*\.(js|css)?$ {
        expires 1h;
    }
}

# create a symbolic link
ln -s /etc/nginx/sites-available/main /etc/nginx/sites-enabled

# restart nginx
systemctl restart nginx

# status
nginx -t

# check
curl http://127.0.0.1
telnet 127.0.0.1 80
telnet 127.0.0.1 8214

对外打开 80 端口

# port
netstat -aptn

# open
iptables -I INPUT -p tcp --dport 80 -j ACCEPT

# status
iptables -nL

版权声明

本博客所有的原创文章,作者皆保留版权。转载必须包含本声明,保持本文完整,并以超链接形式注明作者后除和本文原始地址:https://blog.mazey.net/2624.html

(完)