部署上线你的项目

如何部署你的项目到服务器上?

后端(Go 项目)部署流程

在项目根目录下执行编译命令,生成适用于 Linux 系统的二进制文件:

bash 复制代码
GOOS=linux GOARCH=amd64 go build -o oscAppLinux
一般大家都是Windows系统去编译
如果是Windows系统需要注意不要用cmd,用powershell去编译,执行如下命令:

$env:CGO_ENABLED="0"

$env:GOOS="linux"

$env:GOARCH="amd64"

go build -o oscAppLinux .  # oscAppLinux 是编译后的文件名,可更改

将编译后的文件和配置文件上传到服务器:

注意后端除了上传你编译后的可执行文件,还要注意上传后端的配置文件(config.yaml,如果有静态资源文件夹也要进行上传,如resource这样的目录。

bash 复制代码
scp oscAppLinux config.yaml user@server_ip:/usr/local/osc

除了这种方式以外还可以在你要上传的服务器终端上面输入 rz
rz  # 上传文件到当前目录

或者你使用xshell 活finalshell这样工具直接上传

使用 nohup 命令让程序在后台运行并记录日志:

在使用这条命令之前,可以先直接运行你的可执行文件,让其结果在终端输出,以验证你的输出结果对不对。

bash 复制代码
nohup ./oscAppLinux > osc.log 2>&1 &

检查程序运行状态或终止进程:

bash 复制代码
ps -ef | grep oscAppLinux
kill -9 <pid>

前端(Web 项目)部署流程

在本地前端项目目录下执行打包命令:

bash 复制代码
npm run build

将生成的 dist 文件夹上传到服务器:

同理,上传方式不止这一种,参考后端上传方式

bash 复制代码
scp -r dist user@server_ip:/usr/local/osc

修改 Nginx 配置文件托管静态文件并配置反向代理:

nginx 复制代码
# nginx的配置示列

user root;
events {
  worker_connections  1024;  ## Default: 1024
} 

http {
     include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

server {
    listen 8080;   # 要监听的端口
    index index.php index.html index.htm default.php default.htm default.html;
    server_name home.mychat.cloud;
    root /usr/local/osc/dist;   #你dist方法目录
    
    location  /api {
        rewrite ^/api/(.*)$ /$1 break;
        proxy_pass http://127.0.0.1:8888; # 设置代理服务器的协议和地址  端口要和后端部署保持一致
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}
}

重启 Nginx 使配置生效:

bash 复制代码
systemctl restart nginx

常见问题与解决方案

二进制文件无法执行 检查文件格式并重新编译:

bash 复制代码
file oscAppLinux
GOOS=linux GOARCH=amd64 go build -o oscAppLinux
chmod +x oscAppLinux

浏览器访问 403 错误 调整目录权限并检查索引文件:

bash 复制代码
chown -R nginx:nginx /usr/local/osc/dist
chmod -R 755 /usr/local/osc/dist
ls /usr/local/osc/dist/index.html

接口访问 404 问题 检查后端路由配置和 Nginx 代理规则:

go 复制代码
// 确保后端路由存在
router.GET("/user", UserHandler)

查看 Nginx 日志排查问题:

bash 复制代码
tail -f /var/log/nginx/error.log

nginx -t          # 检查配置语法
相关推荐
我命由我123454 天前
Git 推送报错:error: src refspec main does not match any
运维·git·gitee·github·运维开发·学习方法·版本控制
我命由我123454 天前
Mac 操作系统 - 一些使用记录
运维·windows·学习·系统安全·运维开发·mac·学习方法
天天喝旺仔5 天前
Linux 性能排查实战:用 perf、strace 与火焰图定位 CPU、内存与 IO 瓶颈
linux·性能优化·运维开发
Turboex邮件分享7 天前
DNSBL 实时黑名单的技术原理:从 DNS 查询到垃圾邮件拦截
运维开发·软件需求
Ahtacca7 天前
Linux 基础实验:从终端操作到 C 语言编译
linux·运维·运维开发·虚拟机
Databuff8 天前
五款主流 SSH 免费工具介绍
运维·ssh·运维开发
蓝速科技9 天前
便民服务大厅 AI 数字人一体机场景适配与落地指南丨蓝速科技
大数据·网络·数据结构·人工智能·自然语言处理·数据分析·运维开发
程曦曦9 天前
MySQL 生产库误删 98 张表后的时间点恢复实战:从 binlog 解析到资金对账
linux·数据结构·其他·算法·ubuntu·运维开发
Dawn-bit12 天前
Linux 运维基础扩展:跳板机、堡垒机与物理服务器全流程
linux·运维·服务器·云计算·运维开发
蓝速科技12 天前
蓝速科技 F100 双屏翻译机:中小企业跨国会议提效方案
大数据·网络·数据结构·人工智能·科技·运维开发