1. 方案目标

sv66(无 root、共享容器环境)上部署轻量 Python/PHP 服务,并通过 Cloudflare 反向代理/CDN 对外提供稳定访问。

核心链路:

用户 -> Cloudflare(反向代理/CDN/WAF) -> cloudflared 隧道 -> 127.0.0.1:本地服务端口

该方案不依赖公网高位端口直连,通常比直接开放端口更稳定。

2. 架构与端口规划

  • api.yourdomain.com -> Python 服务 127.0.0.1:18080
  • php.yourdomain.com -> PHP 服务 127.0.0.1:18081
  • cloudflared 以普通用户方式常驻运行(无需 root)

3. 部署 Python 轻服务(示例)

mkdir -p ~/apps/py-api
cat > ~/apps/py-api/server.py <<'PY'
#!/usr/bin/env python3
# 简单健康检查接口
from http.server import BaseHTTPRequestHandler, HTTPServer

class H(BaseHTTPRequestHandler):
    def do_GET(self):
        if self.path == "/health":
            body = b'{"ok":true,"service":"py-api"}'
            self.send_response(200)
            self.send_header("Content-Type", "application/json")
            self.send_header("Content-Length", str(len(body)))
            self.end_headers()
            self.wfile.write(body)
        else:
            self.send_response(404)
            self.end_headers()

HTTPServer(("127.0.0.1", 18080), H).serve_forever()
PY

nohup python3 ~/apps/py-api/server.py > ~/apps/py-api/run.log 2>&1 &
curl -sS http://127.0.0.1:18080/health

4. 部署 PHP 轻服务(示例)

mkdir -p ~/apps/php-web/public
cat > ~/apps/php-web/public/index.php <<'PHP'
<?php
header('Content-Type: application/json; charset=utf-8');
echo json_encode(['ok'=>true,'service'=>'php-web']);
PHP

nohup php -S 127.0.0.1:18081 -t ~/apps/php-web/public > ~/apps/php-web/run.log 2>&1 &
curl -sS http://127.0.0.1:18081/

5. 接入 Cloudflare Tunnel(推荐)

方式A(最快)

  1. 在 Cloudflare Zero Trust 后台创建 Tunnel,获取 token
  2. sv66 下载并运行 cloudflared(用户态):
mkdir -p ~/bin ~/cloudflared
cd ~/bin
curl -L -o cloudflared https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64
chmod +x cloudflared

nohup ~/bin/cloudflared tunnel --no-autoupdate run --token '你的TOKEN' > ~/cloudflared/run.log 2>&1 &
  1. 在 Cloudflare 后台给 Tunnel 绑定 Hostname:
    • api.yourdomain.com -> http://127.0.0.1:18080
    • php.yourdomain.com -> http://127.0.0.1:18081

6. 保活(已验证 sv66 的 crontab 可用)

编辑 crontab:

crontab -e

加入以下内容:

* * * * * pgrep -f "server.py" >/dev/null || nohup python3 /home/motodsnx/apps/py-api/server.py >/home/motodsnx/apps/py-api/run.log 2>&1 &
* * * * * pgrep -f "php -S 127.0.0.1:18081" >/dev/null || nohup php -S 127.0.0.1:18081 -t /home/motodsnx/apps/php-web/public >/home/motodsnx/apps/php-web/run.log 2>&1 &
* * * * * pgrep -f "cloudflared tunnel --no-autoupdate run" >/dev/null || nohup /home/motodsnx/bin/cloudflared tunnel --no-autoupdate run --token '你的TOKEN' >/home/motodsnx/cloudflared/run.log 2>&1 &

7. 建议开启的 Cloudflare 功能

  • Always Use HTTPS
  • WAF 基础规则
  • Rate Limiting(建议保护 /api/*
  • Cache Rules(静态路径缓存,API 默认不缓存)

8. 注意事项

  • sv66 是受限共享环境,不适合高并发和重计算场景。
  • 建议优先保持 Python/PHP 服务轻量化,避免长时间高 CPU 占用。
  • 如需长期稳定,建议后续迁移到可控 VPS,并保留 Cloudflare 入口架构不变。