- 新增 deploy.sh 脚本,自动化部署 crawling-service,包括 Node.js 安装、PM2 配置和环境变量设置 - 新增 ecosystem.config.js 文件,配置 PM2 启动参数和日志管理 - 新增 nginx.conf 文件,配置 Nginx 反向代理和安全设置 - 这些更改旨在简化部署流程,提高服务的可维护性和安全性master
#!/bin/bash | |||||
# 部署脚本 | |||||
echo "开始部署 crawling-service..." | |||||
# 1. 安装 Node.js 22.x | |||||
echo "安装 Node.js 22.x..." | |||||
curl -fsSL https://deb.nodesource.com/setup_22.x | sudo -E bash - | |||||
sudo apt-get install -y nodejs | |||||
# 2. 安装 PM2 | |||||
echo "安装 PM2..." | |||||
sudo npm install -g pm2 | |||||
# 3. 安装项目依赖 | |||||
echo "安装项目依赖..." | |||||
npm install | |||||
# 4. 创建环境变量文件 | |||||
echo "创建环境变量文件..." | |||||
cat > .env << EOL | |||||
# 服务器配置 | |||||
PORT=8991 | |||||
HOST=0.0.0.0 | |||||
NODE_ENV=production | |||||
# 安全配置 | |||||
ALLOWED_ORIGINS=http://your-domain.com | |||||
# 爬虫配置 | |||||
CRAWLER_TIMEOUT=60000 | |||||
CRAWLER_RETRY_ATTEMPTS=3 | |||||
CRAWLER_RETRY_DELAY=2000 | |||||
# 日志配置 | |||||
LOG_LEVEL=info | |||||
LOG_FORMAT=combined | |||||
# 上传配置 | |||||
UPLOAD_URL=https://apibase.sohomall.jp/uploaders | |||||
UPLOAD_SCENE=goods | |||||
UPLOAD_TIMEOUT=30000 | |||||
EOL | |||||
# 5. 创建 screenshots 目录 | |||||
echo "创建截图目录..." | |||||
mkdir -p screenshots | |||||
# 6. 使用 PM2 启动服务 | |||||
echo "启动服务..." | |||||
pm2 start src/server.js --name "crawling-service" --env production | |||||
# 7. 保存 PM2 进程列表 | |||||
echo "保存 PM2 进程列表..." | |||||
pm2 save | |||||
# 8. 设置开机自启 | |||||
echo "设置开机自启..." | |||||
pm2 startup | |||||
echo "部署完成!" |
module.exports = { | |||||
apps: [{ | |||||
name: 'crawling-service', | |||||
script: 'src/server.js', | |||||
instances: 1, | |||||
autorestart: true, | |||||
watch: false, | |||||
max_memory_restart: '1G', | |||||
env: { | |||||
NODE_ENV: 'development' | |||||
}, | |||||
env_production: { | |||||
NODE_ENV: 'production' | |||||
}, | |||||
error_file: 'logs/err.log', | |||||
out_file: 'logs/out.log', | |||||
log_file: 'logs/combined.log', | |||||
time: true | |||||
}] | |||||
}; |
server { | |||||
listen 80; | |||||
server_name your-domain.com; # 替换为你的域名 | |||||
# 日志配置 | |||||
access_log /var/log/nginx/crawling-service.access.log; | |||||
error_log /var/log/nginx/crawling-service.error.log; | |||||
# 安全配置 | |||||
add_header X-Frame-Options "SAMEORIGIN"; | |||||
add_header X-XSS-Protection "1; mode=block"; | |||||
add_header X-Content-Type-Options "nosniff"; | |||||
# 反向代理配置 | |||||
location / { | |||||
proxy_pass http://localhost:8991; | |||||
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; | |||||
proxy_set_header X-Forwarded-Proto $scheme; | |||||
# 超时设置 | |||||
proxy_connect_timeout 60s; | |||||
proxy_send_timeout 60s; | |||||
proxy_read_timeout 60s; | |||||
} | |||||
# 静态文件缓存 | |||||
location /screenshots { | |||||
alias /path/to/your/crawling-service/screenshots; # 替换为实际路径 | |||||
expires 7d; | |||||
add_header Cache-Control "public, no-transform"; | |||||
} | |||||
} |