阿里云 ECS 云服务器,支持 Node.js / Python / PHP / Java 等全栈应用,含数据库与 HTTPS
选择合适配置,开放必要端口
| 配置项 | 推荐值 | 说明 |
|---|---|---|
| 实例规格 | ecs.t6-c1m2.large(2核2G) | 个人项目足够,可随时升配 |
| 操作系统 | Ubuntu 22.04 LTS 64位 | 社区支持最好,教程最多 |
| 系统盘 | 40GB 高效云盘 | 存放系统和代码 |
| 网络带宽 | 按量付费 or 5Mbps 固定 | 访问量小用按量,大用固定 |
| 安全组 | 开放 22/80/443 端口 | SSH、HTTP、HTTPS |
| 付费方式 | 按量付费(测试)/ 包年包月(正式) | 先按量测试,稳定后再包月省钱 |
SSH 登录,更新系统,创建安全用户
# 使用密码连接(购买时设置的密码) ssh root@你的服务器公网IP # 使用密钥连接(推荐,更安全) ssh -i ~/.ssh/your-key.pem root@你的服务器公网IP # 如果提示 "WARNING: UNPROTECTED PRIVATE KEY FILE" chmod 400 ~/.ssh/your-key.pem
# 更新系统包 apt update && apt upgrade -y # 创建普通用户(不要一直用 root!) adduser deploy usermod -aG sudo deploy # 安装常用工具 apt install -y curl wget git vim unzip ufw fail2ban # 配置防火墙 ufw allow OpenSSH ufw allow 'Nginx Full' ufw enable ufw status
根据你的技术栈选择对应安装脚本
# 使用 nvm 安装(推荐,方便管理版本) curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash source ~/.bashrc # 安装 Node.js LTS 版本 nvm install --lts nvm use --lts # 验证安装 node -v # v20.x.x npm -v # 10.x.x # 安装 PM2(进程管理器,保持后台运行) npm install -g pm2
# Ubuntu 22.04 自带 Python 3.10,确认版本 python3 --version # 安装 pip 和 venv apt install -y python3-pip python3-venv # 创建虚拟环境(推荐每个项目独立环境) cd /var/www/myproject python3 -m venv venv source venv/bin/activate pip install -r requirements.txt # 安装 Gunicorn(生产环境 WSGI 服务器) pip install gunicorn
apt install -y mysql-server systemctl start mysql systemctl enable mysql # 安全初始化(设置 root 密码,移除测试库) mysql_secure_installation # 创建项目数据库和专用用户 mysql -u root -p CREATE DATABASE myapp_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; CREATE USER 'myapp_user'@'localhost' IDENTIFIED BY '强密码123!'; GRANT ALL PRIVILEGES ON myapp_db.* TO 'myapp_user'@'localhost'; FLUSH PRIVILEGES; EXIT;
从 Git 仓库拉取代码,安装依赖,启动服务
# 克隆代码 mkdir -p /var/www && cd /var/www git clone https://github.com/你的用户名/你的项目.git myapp cd myapp # 安装依赖 npm install --production # 创建环境变量文件 cp .env.example .env nano .env # 填写数据库密码、API Key 等 # 构建(如果是 Next.js / Nuxt 等) npm run build # 使用 PM2 启动并保持后台运行 pm2 start npm --name "myapp" -- start # 设置开机自启 pm2 startup pm2 save # 查看运行状态 pm2 status pm2 logs myapp
cd /var/www/myapp source venv/bin/activate pip install -r requirements.txt # 创建 Systemd 服务文件(系统级进程管理) nano /etc/systemd/system/myapp.service
[Unit] Description=My Flask/FastAPI App After=network.target [Service] User=deploy WorkingDirectory=/var/www/myapp Environment="PATH=/var/www/myapp/venv/bin" ExecStart=/var/www/myapp/venv/bin/gunicorn \ --workers 3 \ --bind unix:/run/myapp.sock \ app:app Restart=always RestartSec=3 [Install] WantedBy=multi-user.target
systemctl daemon-reload systemctl start myapp systemctl enable myapp systemctl status myapp
让 Nginx 接收 80/443 请求,转发给后端服务
apt install -y nginx systemctl start nginx systemctl enable nginx # 浏览器访问服务器IP,看到 Nginx 欢迎页即成功
server { listen 80; server_name yourdomain.com www.yourdomain.com; # 前端静态文件(如果是分离部署) root /var/www/myapp/dist; index index.html; # 前端路由(SPA 必须配置) location / { try_files $uri $uri/ /index.html; } # API 请求转发给 Node.js 后端 location /api/ { proxy_pass http://127.0.0.1:3000; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/ nginx -t # 检查配置语法,必须显示 "ok" systemctl reload nginx
Let's Encrypt 免费证书,自动续期,无需任何费用
# 确保域名已解析到服务器 IP # 安装 Certbot apt install -y certbot python3-certbot-nginx # 自动申请证书并配置 Nginx certbot --nginx -d yourdomain.com -d www.yourdomain.com # 按提示输入邮箱,同意协议,选择是否强制 HTTPS # 测试自动续期 certbot renew --dry-run # 查看证书状态 certbot certificates
每次推送代码自动更新服务器,无需手动 SSH
name: Deploy to ECS on: push: branches: [ main ] jobs: deploy: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Deploy to server uses: appleboy/ssh-action@v1.0.0 with: host: ${{ secrets.SERVER_IP }} username: deploy key: ${{ secrets.SERVER_SSH_KEY }} script: | cd /var/www/myapp git pull origin main npm install --production npm run build pm2 restart myapp