前提
- 擁有一台雲伺服器(這裡示範是阿裡雲)
- 已安裝Nginx服務
部署
配置nginx
- nginx代理配置
server { listen 80; server_name demo.com; # 線上域名 charset utf-8; root /data/gocode/src/myproject; # 項目目錄 #error_page 404 /404.html; #error_page 502 /502.html; location /(css|js|fonts|img)/ { access_log off; expires 1d; root "/data/gocode/src/myproject/static"; try_files $uri @backend; } location / { try_files /_not_exists_ @backend; } location @backend { proxy_set_header X-Forwarded-For $remote_addr; proxy_set_header Host $http_host; proxy_pass http://127.0.0.1:10010; # Go項目啟動端口改成10010 } }
使用nohup
- 首先編寫一個beego.sh腳本,将其放在/usr/local/sbin目錄下
vim /usr/local/sbin/beego.sh #! /bin/bash cd /data/gocode/src/myproject && bee run # /data/gocode 是GOPATH路徑 # /data/gocode/src/myproject 是項目路徑 # bee run 啟動
- 使用nohup運作腳本
mkdir /var/log/beego # 建立日志目錄 chmod 755 /usr/local/sbin/beego.sh # beego.sh指派可執行權限 # 0 – stdin (standard input),1 – stdout (standard output),2 – stderr (standard error) nohup /usr/local/sbin/beego.sh > /var/log/beego/beego.log 2>&1 & # 2>&1是将标準錯誤(2)重定向到标準輸出(&1),标準輸出(&1)再被重定向輸入到/var/log/beego/beego.log中
- 關閉背景運作
ps -ef | grep bee # 查詢beego.sh和bee運作程序 root 8218 7880 0 Jul01 pts/0 00:00:00 /bin/bash /usr/local/sbin/beego.sh root 8224 8218 0 Jul01 pts/0 00:00:00 bee run root 10964 7880 0 10:05 pts/0 00:00:00 grep --color bee lsof -i :10010 # 查詢項目端口 \COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME myproject 8278 root 3u IPv6 71490 0t0 TCP *:rxapi (LISTEN) kill -9 8218 8224 8278
使用screen部署
- 開啟screen會話
cd /data/gocode/src/myproject # 進入項目目錄 screen # 在該項目目錄開啟一個會話 bee run # 運作項目
- 退出會話
ctrl + A 再按 d # ctrl鍵 + A(大寫字母) 再按 d(小寫字母)
- 恢複會話
screen -ls # 檢視會話清單 There is a screen on: 7464.pts-3.one (Detached) 1 Socket in /var/run/screen/S-root. screen -rD 7464 # 恢複會話
- 删除會話
# 如是Detached狀态使用 screen -X -S 7464 quit # 如是Dead狀态使用 screen -wipe 7464