部署上线你的项目

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

后端(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          # 检查配置语法
相关推荐
fendouweiqian2 天前
编写后端JAR包蓝绿发布脚本
运维开发
半梦半醒*2 天前
ansible阶段练习题
linux·运维·自动化·ansible·负载均衡·运维开发
数据爬坡ing3 天前
C++ 类库管理系统的分析与设计:面向对象开发全流程实践
java·运维·开发语言·c++·软件工程·软件构建·运维开发
肖祥4 天前
k9s监控k8s集群工具
运维开发
半梦半醒*5 天前
ansible判断
linux·运维·centos·ansible·运维开发
半梦半醒*6 天前
ansible变量
linux·运维·开发语言·ansible·运维开发
半梦半醒*7 天前
ansible常用命令的简单练习
linux·运维·服务器·ansible·运维开发
半梦半醒*7 天前
ansible的playbook练习题
linux·运维·服务器·ssh·ansible·运维开发
我命由我123458 天前
Word - Word 查找文本中的特定内容
运维·经验分享·笔记·word·运维开发·文档·文本
半梦半醒*9 天前
playbook剧本
linux·运维·服务器·ssh·ansible·运维开发