跨平台部署与调试:AWS EC2、Docker 和 Nginx 配置实践

继上一次后端部署,从 ECS 迁移到 EC2,配置 PostgreSQL 数据库、域名绑定、SSL 证书和 Nginx 反向代理的全过程。

配置 EC2

  • 新建 key-pair(一次就够了,点击创建会自动下载到电脑,复制保存到~/.ssh/文件夹)

  • 新建 instance

    Name, ubuntu, t2.micro, key-pair, SSH_HTTPS_HTTP -> Launch instance

  • 本地登陆服务器

    • EC2 找到 IP 地址
    • 打开本地 terminal,输入ssh -i ~/.ssh/kp-mac.pem ubuntu@your ipaddress 其中 kp-mac.pem 是下载的 key-pair
  • 安装 docker

    https://docs.docker.com/engine/install/ubuntu/

  • 新建 compose.yaml 文件,copy 内容过来

  • 运行 sudo docker compose up

配置 EC2 过程中遇到的问题

ubuntu 正常跑起来 docker compose 后,配置 instance 的端口

instance -> networking -> network interface ->左滑到 Security group -> edit inbound rules -> add port 8080

image-20250115165510745

配置域名链接到 EC2 的 ip

安装 nginx

https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-ubuntu-20-04

1
2
sudo apt update
sudo apt install nginx

安装完成后打开 http://api.trillobe.com/得到

image-20250115184728164

配置证书 let’s encrypt -> with shell access

https://certbot.eff.org/instructions?ws=nginx&os=pip

完全按照这个链接步骤在服务器中安装,最后生效后 http 会变为 https,Not Secure 的标识也会消失

image-20250115190605919

配置端口连接 nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

#### 配置前端部署后端api

- 无法获取api,但后端api正常,显示cors错误

- 在后端cors配置中应该配置https://ella0110.github.io,删除`trillo/`,重新build,push,在服务器修改yaml,重跑

- env文件不应暴露在github

- 新建github action environment vairable

https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/store-information-in-variables#creating-configuration-variables-for-a-repository

- 修改.github/workflow/nextjs.yaml文件

https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/store-information-in-variables#creating-configuration-variables-for-a-repository

```yaml
env:
NEXT_PUBLIC_API_URL: ${{ vars.NEXT_PUBLIC_API_URL }}
```

- Nextconfig basePath 失效,导致所有本地图片和 icon 失效

- 删除 next.config.mjs 文件,修改所有引用为 next.config.js

- order 页面不正常跳转

- 修改跳转 button 的`<a>`为`<Link>`