167 lines
4.2 KiB
Markdown
167 lines
4.2 KiB
Markdown
# 创建Wantsong APP 服务
|
||
|
||
## 创建 Wantsong 用户并赋权
|
||
|
||
```bash
|
||
sudo useradd -m wantsong
|
||
sudo chown -R wantsong:wantsong /data/app-wantsong-life
|
||
sudo chown wantsong:wantsong /usr/local/bin/app-wantsong-chat-service.sh
|
||
|
||
sudo systemctl daemon-reload
|
||
sudo systemctl start app-wantsong-chat.service
|
||
sudo systemctl status app-wantsong-chat.service
|
||
```
|
||
|
||
你可以使用以下命令来确保 `wantsong` 用户有权限访问 `conda` 环境和相关目录:
|
||
|
||
1. 更改 `anaconda3` 目录及其子目录的所有者为 `wantsong` 用户:
|
||
|
||
```bash
|
||
sudo chown -R wantsong:wantsong /root/anaconda3
|
||
```
|
||
|
||
2. 确保 `wantsong` 用户对 `anaconda3` 目录及其子目录有读写执行权限:
|
||
|
||
```bash
|
||
sudo chmod -R 755 /root/anaconda3
|
||
```
|
||
|
||
3. 确保 `wantsong` 用户对工作目录 `/data/app-wantsong-life` 有读写执行权限:
|
||
|
||
```bash
|
||
sudo chown -R wantsong:wantsong /data/app-wantsong-life
|
||
sudo chmod -R 755 /data/app-wantsong-life
|
||
```
|
||
|
||
sudo chown -R wantsong:wantsong /data/
|
||
|
||
完成这些操作后,重新加载 systemd 配置并启动服务:
|
||
|
||
```bash
|
||
sudo systemctl daemon-reload
|
||
sudo systemctl start app-wantsong-chat.service
|
||
sudo systemctl status app-wantsong-chat.service
|
||
```
|
||
|
||
这样应该可以确保 `wantsong` 用户有权限访问 `conda` 环境和相关目录,并成功启动服务。
|
||
|
||
## 创建服务
|
||
|
||
根据您提供的命令和需求,建议通过Systemd服务实现自动化管理。以下是具体实现步骤和注意事项:
|
||
|
||
**完整解决方案:**
|
||
|
||
1. **创建启动脚本**(避免直接在服务文件中执行多命令)
|
||
```bash
|
||
sudo nano /usr/local/bin/app-wantsong-chat-service.sh
|
||
```
|
||
写入内容:
|
||
```bash
|
||
#!/bin/bash
|
||
source /opt/conda/etc/profile.d/conda.sh # 根据实际conda安装路径调整
|
||
conda activate python312
|
||
cd /data/app-wantsong-life && \
|
||
streamlit run chat/app.py \
|
||
--server.port 5065 \
|
||
--server.address 127.0.0.1 \
|
||
--server.enableCORS false \
|
||
--server.enableXsrfProtection false \
|
||
--server.baseUrlPath "chat" \
|
||
--browser.gatherUsageStats false
|
||
```
|
||
|
||
2. **设置脚本权限**
|
||
```bash
|
||
sudo chmod +x /usr/local/bin/app-wantsong-chat-service.sh
|
||
```
|
||
|
||
3. **创建Systemd服务文件**
|
||
```bash
|
||
sudo nano /etc/systemd/system/app-wantsong-chat.service
|
||
```
|
||
写入内容:
|
||
```ini
|
||
[Unit]
|
||
Description=Wantsong Chat Service
|
||
After=network.target
|
||
|
||
[Service]
|
||
Type=simple
|
||
User=wantsong # 替换为实际运行用户
|
||
WorkingDirectory=/data/app-wantsong-life
|
||
ExecStart=/bin/bash /usr/local/bin/app-wantsong-chat-service.sh
|
||
Restart=always
|
||
RestartSec=30
|
||
Environment="PATH=/opt/conda/envs/python312/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin"
|
||
# 如果遇到环境变量问题,可添加更多环境变量:
|
||
# Environment="LANG=en_US.UTF-8"
|
||
|
||
[Install]
|
||
WantedBy=multi-user.target
|
||
# 如果遇到环境变量问题,可添加更多环境变量:
|
||
# Environment="LANG=en_US.UTF-8"
|
||
|
||
[Install]
|
||
WantedBy=multi-user.target
|
||
```
|
||
|
||
**关键配置说明:**
|
||
|
||
- `source conda.sh`:确保正确加载conda环境(路径需根据实际安装位置调整)
|
||
- `Restart策略`:进程意外终止时自动重启
|
||
- `Environment PATH`:显式指定路径,避免环境变量缺失
|
||
- `User指定`:必须与conda环境所有者一致(重要)
|
||
|
||
**部署验证步骤:**
|
||
|
||
1. 重新加载服务配置
|
||
```bash
|
||
sudo systemctl daemon-reload
|
||
```
|
||
|
||
2. 启动服务
|
||
```bash
|
||
sudo systemctl start app-wantsong-chat
|
||
```
|
||
|
||
3. 设置开机启动
|
||
```bash
|
||
sudo systemctl enable app-wantsong-chat
|
||
```
|
||
|
||
4. 查看运行状态
|
||
```bash
|
||
sudo systemctl status app-wantsong-chat
|
||
```
|
||
|
||
**常见问题排查:**
|
||
|
||
1. 如果出现`conda: command not found`:
|
||
- 检查conda.sh路径是否正确(常见路径:`/opt/conda/etc/profile.d/conda.sh`或`/home/用户名/miniconda3/etc/...`)
|
||
|
||
2. 若端口冲突:
|
||
```bash
|
||
netstat -tulnp | grep 5065
|
||
```
|
||
|
||
3. 查看详细日志:
|
||
```bash
|
||
journalctl -u wantsong-chat -f
|
||
```
|
||
|
||
**扩展建议:**
|
||
1. 建议添加日志重定向到文件:
|
||
```ini
|
||
# 在[Service]段添加
|
||
StandardOutput=file:/var/log/wantsong-chat.log
|
||
StandardError=file:/var/log/wantsong-chat-error.log
|
||
```
|
||
|
||
2. 如果涉及GPU资源,需添加:
|
||
```ini
|
||
Environment="CUDA_VISIBLE_DEVICES=0"
|
||
```
|
||
|
||
3. 生产环境建议配合Nginx反向代理,添加SSL证书保护
|
||
|
||
执行完毕后,服务会自动在后台运行,并可通过systemctl命令进行管理。建议首次部署后观察日志30分钟确认稳定性。 |