281 lines
5.7 KiB
Markdown
281 lines
5.7 KiB
Markdown
# MoldInsight Linux 部署指南
|
||
|
||
## 系统要求
|
||
- Linux 系统 (Ubuntu 20.04+ / CentOS 8+)
|
||
- Python 3.8+
|
||
- PostgreSQL 12+
|
||
- Git
|
||
|
||
## 1. 环境准备
|
||
|
||
### 安装系统依赖
|
||
```bash
|
||
# Ubuntu/Debian
|
||
sudo apt update
|
||
sudo apt install python3 python3-pip python3-venv postgresql postgresql-contrib git
|
||
|
||
# CentOS/RHEL
|
||
sudo yum update
|
||
sudo yum install python3 python3-pip postgresql postgresql-server git
|
||
```
|
||
|
||
### 配置PostgreSQL
|
||
```bash
|
||
# 启动PostgreSQL服务
|
||
sudo systemctl start postgresql
|
||
sudo systemctl enable postgresql
|
||
|
||
# 创建数据库和用户
|
||
sudo -u postgres psql
|
||
```
|
||
|
||
在PostgreSQL中执行:
|
||
```sql
|
||
CREATE DATABASE moldinsight;
|
||
CREATE USER molduser WITH PASSWORD 'moldpassword';
|
||
GRANT ALL PRIVILEGES ON DATABASE moldinsight TO molduser;
|
||
\q
|
||
```
|
||
|
||
## 2. 项目部署
|
||
|
||
### 克隆或复制项目
|
||
```bash
|
||
# 如果使用Git
|
||
cd /opt
|
||
sudo git clone <your-repo-url> moldinsight
|
||
sudo chown -R $USER:$USER moldinsight
|
||
cd moldinsight
|
||
|
||
# 或者直接复制项目文件到Linux服务器
|
||
```
|
||
|
||
### 创建Python虚拟环境
|
||
```bash
|
||
cd moldinsight_project
|
||
python3 -m venv venv
|
||
source venv/bin/activate
|
||
```
|
||
|
||
### 安装依赖
|
||
```bash
|
||
pip install --upgrade pip
|
||
pip install -r requirements.txt
|
||
```
|
||
|
||
## 3. 环境配置
|
||
|
||
### 修改环境配置文件
|
||
编辑 `.env` 文件:
|
||
```bash
|
||
nano .env
|
||
```
|
||
|
||
修改为Linux环境的配置:
|
||
```env
|
||
# 数据库配置(Linux环境)
|
||
DATABASE_URL=postgresql+asyncpg://molduser:moldpassword@localhost:5432/moldinsight
|
||
|
||
# 服务配置
|
||
DEBUG=false
|
||
HOST=0.0.0.0
|
||
PORT=8000
|
||
|
||
# Redis配置(可选)
|
||
REDIS_HOST=localhost
|
||
REDIS_PORT=6379
|
||
REDIS_PASSWORD=
|
||
|
||
# Kafka配置(可选)
|
||
KAFKA_BOOTSTRAP_SERVERS=localhost:9092
|
||
KAFKA_SECURITY_PROTOCOL=PLAINTEXT
|
||
```
|
||
|
||
### 创建必要的目录
|
||
```bash
|
||
mkdir -p uploads html_output logs
|
||
chmod 755 uploads html_output logs
|
||
```
|
||
|
||
## 4. 启动服务
|
||
|
||
### 开发模式启动
|
||
```bash
|
||
cd moldinsight_project
|
||
source venv/bin/activate
|
||
python src/main.py
|
||
```
|
||
|
||
### 生产环境启动(使用Gunicorn)
|
||
```bash
|
||
# 安装Gunicorn
|
||
pip install gunicorn uvloop httptools
|
||
|
||
# 启动服务
|
||
cd moldinsight_project
|
||
source venv/bin/activate
|
||
gunicorn src.main:app --workers 4 --worker-class uvicorn.workers.UvicornWorker --bind 0.0.0.0:8000
|
||
```
|
||
|
||
## 5. 系统服务配置(可选)
|
||
|
||
### 创建systemd服务文件
|
||
```bash
|
||
sudo nano /etc/systemd/system/moldinsight.service
|
||
```
|
||
|
||
添加以下内容:
|
||
```ini
|
||
[Unit]
|
||
Description=MoldInsight Geometry Analysis Service
|
||
After=network.target postgresql.service
|
||
|
||
[Service]
|
||
Type=simple
|
||
User=www-data
|
||
Group=www-data
|
||
WorkingDirectory=/opt/moldinsight/moldinsight_project
|
||
Environment=PATH=/opt/moldinsight/moldinsight_project/venv/bin
|
||
ExecStart=/opt/moldinsight/moldinsight_project/venv/bin/gunicorn src.main:app --workers 4 --worker-class uvicorn.workers.UvicornWorker --bind 0.0.0.0:8000
|
||
Restart=always
|
||
|
||
[Install]
|
||
WantedBy=multi-user.target
|
||
```
|
||
|
||
### 启用并启动服务
|
||
```bash
|
||
sudo systemctl daemon-reload
|
||
sudo systemctl enable moldinsight
|
||
sudo systemctl start moldinsight
|
||
sudo systemctl status moldinsight
|
||
```
|
||
|
||
## 6. Nginx反向代理配置(可选)
|
||
|
||
### 安装Nginx
|
||
```bash
|
||
# Ubuntu/Debian
|
||
sudo apt install nginx
|
||
|
||
# CentOS/RHEL
|
||
sudo yum install nginx
|
||
```
|
||
|
||
### 创建Nginx配置文件
|
||
```bash
|
||
sudo nano /etc/nginx/sites-available/moldinsight
|
||
```
|
||
|
||
添加以下内容:
|
||
```nginx
|
||
server {
|
||
listen 80;
|
||
server_name your-domain.com;
|
||
|
||
location / {
|
||
proxy_pass http://127.0.0.1:8000;
|
||
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;
|
||
}
|
||
|
||
location /static {
|
||
alias /opt/moldinsight/moldinsight_project/static;
|
||
expires 30d;
|
||
}
|
||
}
|
||
```
|
||
|
||
### 启用站点并重启Nginx
|
||
```bash
|
||
sudo ln -s /etc/nginx/sites-available/moldinsight /etc/nginx/sites-enabled/
|
||
sudo nginx -t
|
||
sudo systemctl restart nginx
|
||
```
|
||
|
||
## 7. 防火墙配置
|
||
|
||
```bash
|
||
# Ubuntu/Debian (ufw)
|
||
sudo ufw allow 80
|
||
sudo ufw allow 8000
|
||
sudo ufw allow ssh
|
||
sudo ufw enable
|
||
|
||
# CentOS/RHEL (firewalld)
|
||
sudo firewall-cmd --permanent --add-port=80/tcp
|
||
sudo firewall-cmd --permanent --add-port=8000/tcp
|
||
sudo firewall-cmd --permanent --add-service=ssh
|
||
sudo firewall-cmd --reload
|
||
```
|
||
|
||
## 8. 验证部署
|
||
|
||
### 检查服务状态
|
||
```bash
|
||
# 检查应用服务
|
||
curl http://localhost:8000/health
|
||
|
||
# 检查数据库连接
|
||
sudo -u postgres psql -d moldinsight -c "SELECT version();"
|
||
```
|
||
|
||
### 测试文件上传
|
||
访问 `http://your-server-ip:8000` 上传STP文件测试功能。
|
||
|
||
## 9. 故障排除
|
||
|
||
### 常见问题
|
||
|
||
1. **数据库连接失败**
|
||
- 检查PostgreSQL服务状态:`sudo systemctl status postgresql`
|
||
- 验证数据库连接:`psql -h localhost -U molduser -d moldinsight`
|
||
|
||
2. **端口被占用**
|
||
- 检查端口使用:`netstat -tulpn | grep 8000`
|
||
- 修改端口或停止占用进程
|
||
|
||
3. **权限问题**
|
||
- 确保目录权限正确:`chmod 755 uploads html_output logs`
|
||
- 检查文件所有者:`ls -la`
|
||
|
||
4. **依赖安装失败**
|
||
- 更新pip:`pip install --upgrade pip`
|
||
- 使用国内镜像:`pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple`
|
||
|
||
## 10. 备份和恢复
|
||
|
||
### 数据库备份
|
||
```bash
|
||
# 备份数据库
|
||
sudo -u postgres pg_dump moldinsight > moldinsight_backup.sql
|
||
|
||
# 恢复数据库
|
||
sudo -u postgres psql -d moldinsight < moldinsight_backup.sql
|
||
```
|
||
|
||
### 文件备份
|
||
```bash
|
||
# 备份上传的文件和配置
|
||
tar -czf moldinsight_backup.tar.gz uploads/ html_output/ .env requirements.txt
|
||
```
|
||
|
||
## 快速启动脚本
|
||
|
||
创建启动脚本 `start.sh`:
|
||
```bash
|
||
#!/bin/bash
|
||
cd /opt/moldinsight/moldinsight_project
|
||
source venv/bin/activate
|
||
python src/main.py
|
||
```
|
||
|
||
赋予执行权限:
|
||
```bash
|
||
chmod +x start.sh
|
||
./start.sh
|
||
```
|
||
|
||
现在您的MoldInsight项目已经可以在Linux环境下正常运行! |