# Dịch vụ chatbot — đặt tại /etc/systemd/system/chatbot.service # # Chạy dưới user `admin` vì nó phải đọc được private/.env của website # (tệp đó chmod 640, chủ admin:admin). Chạy bằng root là thừa quyền, mà # dịch vụ này có nhận dữ liệu từ Internet qua PHP. # # Cổng 8787 CHỈ nghe ở 127.0.0.1 — PHP gọi nội bộ, không mở ra ngoài. # Muốn kiểm từ xa thì dùng SSH tunnel, đừng đổi thành 0.0.0.0. [Unit] Description=TradingAuto chatbot (cong 8787) After=network-online.target Wants=network-online.target [Service] Type=simple User=admin Group=admin WorkingDirectory=/home/admin/web/tradingauto.org/private/services/chatbot # Khoá API và mật khẩu CSDL nằm ở đây, chmod 600. Không bao giờ viết # thẳng vào tệp unit này — `systemctl cat` ai đọc cũng được. EnvironmentFile=-/home/admin/web/tradingauto.org/private/.env EnvironmentFile=-/etc/tradingauto-chatbot.env ExecStart=/home/admin/web/tradingauto.org/private/services/chatbot/moitruong/bin/python \ -m uvicorn app:app --host 127.0.0.1 --port 8787 --workers 1 # Một worker là đủ: lịch sử phiên đang giữ trong BỘ NHỚ của tiến trình # (xem nao/tra_loi.py). Tăng workers lên mà chưa chuyển lịch sử ra chỗ # dùng chung thì khách sẽ thấy bot quên chuyện vừa nói, tuỳ lượt request # rơi vào worker nào — lỗi kiểu đó rất khó lần. Restart=on-failure RestartSec=5 KillSignal=SIGINT TimeoutStopSec=20 # Siết quyền: dịch vụ chỉ cần đọc mã của chính nó và ghi log. NoNewPrivileges=true PrivateTmp=true ProtectSystem=strict ProtectHome=read-only ReadWritePaths=/home/admin/web/tradingauto.org/private/logs ReadWritePaths=/home/admin/web/tradingauto.org/private/services/chatbot # Ảnh khách gửi qua Messenger (kenh/anh.py, 25/09/2026). Thiếu dòng này thì lưu ảnh # báo "Read-only file system" và bot trả lời mà không thấy ảnh. Dấu '-' = thư mục chưa có thì bỏ qua. ReadWritePaths=-/home/admin/web/tradingauto.org/private/du_lieu/anh_chat StandardOutput=journal StandardError=journal SyslogIdentifier=chatbot [Install] WantedBy=multi-user.target