🏄

项目部署

独立部署

nohup

使用简单的nohup命令来运行应用程序,使其作为后台守护进程运行。
nohup ./app & # 如果不想输出日志 # nohup ./app >/dev/null 2>&1 &

服务安装

直接把 Go 程序安装成服务实现开机自动启动,进程守护等

system

# 建立服务配置文件 vi /etc/systemd/system/z-app.service # 写入以下内容 [Unit] # 服务的简单描述 Description=z-app-web After=network.target [Service] # 执行命令,每一次修改 ExecStart 都需执行 'systemctl daemon-reload' ExecStart=/www/app --port=3788 [Install] WantedBy=multi-user.target # 开机自动启动 systemctl enable z-app.service # 启动服务 systemctl start z-app.service # 查看状态 systemctl status z-app.service # 停止服务 systemctl stop z-app.service # 重启服务 systemctl restart z-app.service

tmux

tmux是一款Linux下的终端复用工具
# ubuntu 下安装 sudo apt-get install tmux tmux new -s zapp # 在新终端窗口中执行 ./app # 使用 ctrl + B & D 快捷键可以退出当前终端窗口 # 使用 tmux attach -t zgo 可进入到之前的终端窗口
 

supervisor

supervisor 是用 Python 开发的一套通用的进程管理程序,官方网站:http://supervisord.org/
# /etc/supervisor/conf.d/zapp.conf [program:z-app] user=root command=/var/www/app stdout_logfile=/var/log/zapp-stdout.log stderr_logfile=/var/log/zapp-stderr.log autostart=true autorestart=true # 启动 supervisor 服务: sudo service supervisor start # 进入supervisor管理终端: sudo supervisorctl # 使用 reload 重新读取配置文件 # 使用 status 查看当前进程状态
 

代理部署

请自行启动 Go程序(可以参考上面方法)

Nginx

server { listen 80; server_name zapp.com; location ^~ /static { access_log off; expires 1d; root /var/www/zapp/static; try_files $uri @backendApp; } location / { try_files $uri @backendApp; } location @backendApp{ proxy_pass http://127.0.0.1:3788;# 启动端口 proxy_redirect off; proxy_set_header Host $http_host;#$host:$server_port proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Remote-Addr $remote_addr:$remote_port; # 如果需要配置长链接 # proxy_http_version 1.1; # proxy_set_header Connection ""; # SEE 长链接示例 # proxy_http_version 1.1; # proxy_set_header Connection ''; # chunked_transfer_encoding off; # proxy_buffering off; # proxy_cache off; # proxy_redirect off; # proxy_set_header Host $host; # proxy_set_header X-Real-IP $remote_addr; # proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }