环境准备
-
安装 Node.js

# 使用 nvm(推荐) curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.4/install.sh | bash source ~/.bashrc nvm install 18 # 安装 Node.js 18 LTS node -v # 验证版本 # 或直接安装 sudo apt install nodejs npm # Ubuntu/Debian brew install node # macOS
-
安装 Python(如需 AI 模型推理)
sudo apt install python3 python3-pip # Ubuntu pip3 install torch torchvision # 示例:PyTorch
-
安装 Git
sudo apt install git
获取项目代码
git clone https://github.com/your-repo/ai-crayfish.git cd ai-crayfish
安装依赖
-
Node.js 依赖
npm install # 或使用 yarn/pnpm yarn install
-
Python 依赖(如项目包含 Python 后端)
pip3 install -r requirements.txt
-
安装数据库(如需要)
# MongoDB 示例 sudo apt install mongodb # 或使用 Docker docker run --name mongo -d -p 27017:27017 mongo
环境配置
-
创建环境变量文件
.envNODE_ENV=production PORT=3000 API_KEY=your_ai_api_key DB_URL=mongodb://localhost:27017/crayfish_db
-
配置模型文件
- 将预训练模型(如
model.pth或model.onnx)放入./models/ - 确保路径与代码中的配置一致
- 将预训练模型(如
构建与启动
-
前端构建(如有)
npm run build
-
启动服务
# 开发模式 npm run dev # 生产模式(使用 PM2 守护进程) npm install -g pm2 pm2 start server.js --name "ai-crayfish" pm2 save pm2 startup # 设置开机自启
-
启动 Python 服务(如独立后端)
# 使用 gunicorn(Flask/FastAPI) pip3 install gunicorn gunicorn -w 4 -b 0.0.0.0:5000 app:app # 或使用 PM2 启动 Python 脚本 pm2 start "python3 ai_model.py" --name "ai-model"
配置反向代理(Nginx)
listen 80;
server_name your_domain.com;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
}
# 如需代理 Python API
location /api/ {
proxy_pass http://localhost:5000/;
}
}
启用配置:
sudo ln -s /etc/nginx/sites-available/ai-crayfish /etc/nginx/sites-enabled/ sudo nginx -t && sudo systemctl reload nginx
安全与优化
-
防火墙设置
sudo ufw allow 22/tcp # SSH sudo ufw allow 80/tcp # HTTP sudo ufw allow 443/tcp # HTTPS sudo ufw enable
-
SSL 证书(Let‘s Encrypt)
sudo apt install certbot python3-certbot-nginx sudo certbot --nginx -d your_domain.com
-
监控日志
pm2 logs ai-crayfish tail -f /var/log/nginx/access.log
测试部署
- 验证服务
curl http://localhost:3000/health
- 测试 AI 接口
curl -X POST http://localhost:3000/predict \ -H "Content-Type: application/json" \ -d '{"image": "base64_data"}'
常见问题排查
- 端口冲突
netstat -tulpn | grep :3000
- Node.js 内存溢出
export NODE_OPTIONS="--max-old-space-size=4096"
- Python 依赖错误
pip3 install --upgrade pip setuptools wheel
备注
- 根据实际项目调整步骤(如使用 Docker、K8s 等)。
- AI 模型部署可能需要 GPU 环境,需安装 CUDA/cuDNN。
- 可结合 Docker 简化环境部署:
FROM node:18-slim COPY . /app WORKDIR /app RUN npm install CMD ["node", "server.js"]
按此流程即可完成 AI 小龙虾项目的部署,根据具体技术栈调整细节即可。
版权声明:除非特别标注,否则均为本站原创文章,转载时请以链接形式注明文章出处。