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

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

配置 EC2

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

  • 参考链接:使用 Amazon EC2 创建密钥对

  • 新建 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

配置前端部署后端 api