阿里云 ECS 部署
本文档介绍如何在阿里云 ECS 服务器上部署 PocketBase,包括服务器配置、安全组设置、反向代理配置等。
购买 ECS 实例
Section titled “购买 ECS 实例”推荐配置:
| 配置项 | 入门级 | 生产级 |
|---|---|---|
| 实例规格 | 1核2GB | 2核4GB |
| 操作系统 | Ubuntu 22.04 | Ubuntu 22.04 |
| 系统盘 | 40GB SSD | 40GB SSD |
| 带宽 | 1Mbps | 3Mbps |
# 使用 SSH 密钥登录ssh -i /path/to/key.pem root@your-ecs-ip
# 或使用密码登录ssh root@your-ecs-ip# 更新软件包apt update && apt upgrade -y
# 安装常用工具apt install -y curl wget vim git ufw fail2ban htop# 启用 UFWufw enable
# 允许 SSHufw allow 22/tcp
# 允许 HTTP 和 HTTPSufw allow 80/tcpufw allow 443/tcp
# 查看状态ufw status# 设置时区为中国上海timedatectl set-timezone Asia/Shanghai
# 验证date安装 PocketBase
Section titled “安装 PocketBase”# 创建应用目录mkdir -p /opt/pocketbasecd /opt/pocketbase
# 下载最新版本PB_VERSION=$(curl -s https://api.github.com/repos/pocketbase/pocketbase/releases/latest | grep 'tag_name' | cut -d\" -f4)wget https://github.com/pocketbase/pocketbase/releases/download/${PB_VERSION}/pocketbase_${PB_VERSION}_linux_amd64.zip
# 解压unzip pocketbase_${PB_VERSION}_linux_amd64.ziprm pocketbase_${PB_VERSION}_linux_amd64.zip
# 添加执行权限chmod +x pocketbase
# 测试运行./pocketbase serve配置 Systemd 服务
Section titled “配置 Systemd 服务”创建服务文件
Section titled “创建服务文件”vim /etc/systemd/system/pocketbase.service[Unit]Description=PocketBase ServiceAfter=network.target
[Service]Type=simpleUser=rootGroup=rootWorkingDirectory=/opt/pocketbaseExecStart=/opt/pocketbase/pocketbase serve --http=127.0.0.1:8090Restart=alwaysRestartSec=5sStandardOutput=journalStandardError=journalSyslogIdentifier=pocketbase
# 安全加固NoNewPrivileges=truePrivateTmp=trueProtectSystem=strictProtectHome=trueReadWritePaths=/opt/pocketbase/pb_data
# 资源限制LimitNOFILE=65535MemoryLimit=512M
[Install]WantedBy=multi-user.target# 重新加载 systemdsystemctl daemon-reload
# 启用服务systemctl enable pocketbase
# 启动服务systemctl start pocketbase
# 查看状态systemctl status pocketbase
# 查看日志journalctl -u pocketbase -f配置 Nginx 反向代理
Section titled “配置 Nginx 反向代理”安装 Nginx
Section titled “安装 Nginx”apt install nginx -y配置 SSL(使用 Let’s Encrypt)
Section titled “配置 SSL(使用 Let’s Encrypt)”# 安装 Certbotapt install certbot python3-certbot-nginx -y
# 获取证书(替换为你的域名)certbot --nginx -d your-domain.com -d www.your-domain.com
# 自动续期certbot renew --dry-run配置 Nginx
Section titled “配置 Nginx”vim /etc/nginx/sites-available/pocketbaseserver { listen 443 ssl http2; server_name your-domain.com www.your-domain.com;
# SSL 证书 ssl_certificate /etc/letsencrypt/live/your-domain.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/your-domain.com/privkey.pem;
# SSL 配置 ssl_protocols TLSv1.2 TLSv1.3; ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384; ssl_prefer_server_ciphers off; ssl_session_cache shared:SSL:10m; ssl_session_timeout 10m;
# 安全头部 add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always; add_header X-Content-Type-Options nosniff always; add_header X-Frame-Options SAMEORIGIN always; add_header X-XSS-Protection "1; mode=block" always;
# 文件上传大小 client_max_body_size 10M;
# 日志 access_log /var/log/nginx/pocketbase-access.log; error_log /var/log/nginx/pocketbase-error.log;
# 反向代理 location / { proxy_pass http://127.0.0.1:8090; proxy_http_version 1.1;
# WebSocket 支持 proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade";
# 标准头部 proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-Port $server_port;
# 超时设置 proxy_connect_timeout 60s; proxy_send_timeout 60s; proxy_read_timeout 60s; }}
# HTTP 重定向到 HTTPSserver { listen 80; server_name your-domain.com www.your-domain.com; return 301 https://$server_name$request_uri;}# 创建符号链接ln -s /etc/nginx/sites-available/pocketbase /etc/nginx/sites-enabled/
# 测试配置nginx -t
# 重启 Nginxsystemctl restart nginx配置阿里云安全组
Section titled “配置阿里云安全组”添加入方向规则
Section titled “添加入方向规则”在阿里云控制台配置安全组规则:
| 协议类型 | 端口范围 | 授权对象 | 描述 |
|---|---|---|---|
| SSH | 22/22 | 0.0.0.0/0 | 远程连接(建议限制 IP) |
| HTTP | 80/80 | 0.0.0.0/0 | Web 访问 |
| HTTPS | 443/443 | 0.0.0.0/0 | 安全 Web 访问 |
注意: 不要开放 8090 端口到公网,只允许本地访问。
使用 OSS 备份
Section titled “使用 OSS 备份”安装阿里云 CLI:
# 安装wget https://aliyuncli.alicdn.com/aliyun-cli-linux-latest-amd64.tgztar -xzf aliyun-cli-linux-latest-amd64.tgzsudo mv aliyun /usr/local/bin/
# 配置aliyun configure备份脚本:
#!/bin/bashBACKUP_DATE=$(date +%Y%m%d_%H%M%S)BACKUP_FILE="pocketbase_backup_${BACKUP_DATE}.tar.gz"PB_DATA_DIR="/opt/pocketbase/pb_data"OSS_BUCKET="oss://your-bucket/backups"
# 创建备份tar -czf /tmp/${BACKUP_FILE} -C ${PB_DATA_DIR} .
# 上传到 OSSaliyun oss cp /tmp/${BACKUP_FILE} ${OSS_BUCKET}/${BACKUP_FILE}
# 清理本地临时文件rm /tmp/${BACKUP_FILE}
# 清理 30 天前的备份aliyun oss ls ${OSS_BUCKET} | awk '{print $5}' | while read file; do # 删除旧文件逻辑done设置定时任务:
# 添加到 crontabcrontab -e
# 每天凌晨 3 点备份0 3 * * * /opt/scripts/backup-to-oss.sh >> /var/log/pb-backup.log 2>&1使用快照备份
Section titled “使用快照备份”在阿里云控制台:
- 进入 ECS 实例
- 选择「磁盘」->「创建快照」
- 设置自动快照策略(如每天凌晨 2 点)
启用 BBR
Section titled “启用 BBR”# 启用 TCP BBRecho "net.core.default_qdisc=fq" >> /etc/sysctl.confecho "net.ipv4.tcp_congestion_control=bbr" >> /etc/sysctl.confsysctl -p
# 验证sysctl net.ipv4.tcp_congestion_control调整文件描述符限制
Section titled “调整文件描述符限制”# 编辑 limits.confvim /etc/security/limits.conf
# 添加以下内容* soft nofile 65535* hard nofile 65535阿里云云监控
Section titled “阿里云云监控”- 在 ECS 控制台启用云监控
- 安装云监控插件
# 安装云监控插件wget https://cms-agent-cn-hangzhou.oss-cn-hangzhou.aliyuncs.com/agent/install/cms_agent_install.shbash cms_agent_install.sh自定义监控脚本
Section titled “自定义监控脚本”#!/bin/bash# 检查 PocketBase 服务状态if ! systemctl is-active --quiet pocketbase; then echo "PocketBase is not running!" | mail -s "Alert: PocketBase Down" admin@example.com systemctl restart pocketbasefi
# 检查磁盘空间DISK_USAGE=$(df -h /opt/pocketbase | awk 'NR==2 {print $5}' | sed 's/%//')if [ $DISK_USAGE -gt 80 ]; then echo "Disk usage is ${DISK_USAGE}%" | mail -s "Alert: High Disk Usage" admin@example.comfiQ: 域名解析如何配置?
Section titled “Q: 域名解析如何配置?”在阿里云 DNS 控制台:
- 添加 A 记录
- 主机记录:
@和www - 记录值:你的 ECS 公网 IP
Q: 如何更新 PocketBase?
Section titled “Q: 如何更新 PocketBase?”# 停止服务systemctl stop pocketbase
# 备份数据cp -r /opt/pocketbase/pb_data /opt/pocketbase/pb_data.backup
# 下载新版本cd /opt/pocketbasePB_VERSION=$(curl -s https://api.github.com/repos/pocketbase/pocketbase/releases/latest | grep 'tag_name' | cut -d\" -f4)wget https://github.com/pocketbase/pocketbase/releases/download/${PB_VERSION}/pocketbase_${PB_VERSION}_linux_amd64.zipunzip -o pocketbase_${PB_VERSION}_linux_amd64.ziprm pocketbase_${PB_VERSION}_linux_amd64.zip
# 重启服务systemctl start pocketbaseQ: 如何查看日志?
Section titled “Q: 如何查看日志?”# PocketBase 日志journalctl -u pocketbase -f
# Nginx 日志tail -f /var/log/nginx/pocketbase-access.logtail -f /var/log/nginx/pocketbase-error.logQ: 内存不足如何处理?
Section titled “Q: 内存不足如何处理?”考虑升级实例规格,或添加 Swap:
# 创建 2GB Swapfallocate -l 2G /swapfilechmod 600 /swapfilemkswap /swapfileswapon /swapfile
# 永久生效echo '/swapfile none swap sw 0 0' >> /etc/fstab