最近刷到Codex号池,于是乎利用之前注册的Oracle自己搭建一个。
写在前面
很多问题都是没仔细看帖子导致的,比如iptables和防火墙放开端口问题,帖子都有写,看不明白再去问ai。
使用Oracle免费服务器部署CLIProxyAPI
注册Oracle
网络上注册Oracle免费套餐的教程有很多,不再赘述。
我之前的帖子:Oracle似乎是注册成功了
创建实例
在创建实例之前,先去创建子网,当然你也可以在创建实例时创建。在虚拟云网络页面,选择创建VCN。
名称:随便写,假设为vcn0
IPv4 CIDR:默认即可,10.0.0.0/16
其他均可以自动配置。
在实例页面创建实例,因为开不出arm的机器,直接选免费的x86即可,应该是1c1g的配置。
镜像选择Ubuntu22.04即可。
在网络部分,选择你刚刚创建的子网,我这里是vcn0,同时勾选公网IP。
在存储部分,你可以把性能拉满,只要在账户免费的200g总容量内,就不会收钱。你可以保持默认大小,或者自己增加容量。
你可以提前创建好ssh的public key,在创建阶段上传,或是选择其他认证方式。
连接实例
你如果尝试直接ssh oracle服务器,会发现无法连接。首先你要去虚拟云网络页面,点击你之前创建的网络,在网关选项卡下,添加一个Internet网关,不然你的服务器是没办法与外网通信的。默认路由表会自动将流量发送到你刚刚创建的网关。
Oracle默认放开了22端口,此时可以尝试用你的认证方式进行ssh连接,ubuntu系统的用户名就是ubuntu。
部署CLIProxyAPI
检测IP
首先可以用下面的脚本测试一下ChatGPT等平台等解锁情况:
https://github.com/xykt/IPQuality
不会用的直接执行下面的命令:
sudo -i
bash <(curl -Ls https://IP.Check.Place) -f
如果ChatGPT显示解锁,即可继续操作,否则需要更换服务器或配置代理。检测结果示例如下:
开始部署
然后根据下面的教程,在服务器上部署CLIProxyAPI。
你可以直接使用安装脚本:
curl -fsSL https://raw.githubusercontent.com/brokechubb/cliproxyapi-installer/refs/heads/master/cliproxyapi-installer | bash
服务器默认使用项目根目录的 YAML 配置文件(config.yaml)。可通过 --config 指定其他路径:
./cli-proxy-api --config /path/to/your/config.yaml
我们需要修改配置文件的remote-management.allow-remote来允许远程访问,如果你在自己的电脑上部署就不需要这一步。同时,设置管理密钥,写明文即可,会自动转换成哈希。比如我这里写123456。
# 服务器绑定主机/接口,默认空字符串同时绑定 IPv4/IPv6。
# 使用 "127.0.0.1" 或 "localhost" 可限制仅本机访问。
host: ""
# 服务器端口
port: 8317
# TLS 设置:启用后使用提供的证书与私钥监听 HTTPS。
tls:
enable: false
cert: ""
key: ""
# 管理 API 设置
remote-management:
# 是否允许远程(非 localhost)访问管理接口。
# 为 false 时仅允许 localhost,仍需管理密钥。
# allow-remote: false
allow-remote: true
# 管理密钥。若填写明文,启动时会自动哈希后生效。
# 所有管理请求(包括本地)都需要该密钥。
# 留空则完全禁用管理 API(所有 /v0/management 路由返回 404)。
secret-key: "123456"
...
把service文件安装到systemd
cd ~/cliproxyapi
sudo cp -f cliproxyapi.service /etc/systemd/system/cliproxyapi.service
sudo systemctl daemon-reload
检查它是否被systemd识别:
systemctl status cliproxyapi.service--no-pager
启动+开机自启
sudo systemctl enable --now cliproxyapi.service
systemctl status cliproxyapi.service --no-pager
看日志:
sudo journalctl -u cliproxyapi.service -e --no-pager
公网IP访问
如果你没有域名,可直接使用公网ip访问,但不建议。首先要放开需要的端口,配置文件中默认是8317 。在虚拟云网络页面,选择你的网络,在安全选项卡下,点击默认创建的安全列表,我这里是Default Security List for vcn0 。
点击添加入站规则,源填写0.0.0.0/0 ,协议选择TCP,源端口范围留空,目的地端口范围选择你要放行的端口,比如8317。
在Oracle配置完成后,我们还需要在服务器上放行端口。使用iptables 命令操作。使用下面命令放行8317 端口。
sudo iptables -A INPUT -p tcp --dport 8317 -j ACCEPT
如果不生效,可以用下面命令来查看完整的iptable 。
sudo iptables -L -n -v
可以用下面的命令来删除指定行号的规则。
sudo iptables -D INPUT <line-number>
可以用下面的命令来查看端口监听状态。
ss -lntp
含义:
l:只看监听n:数字显示端口t:TCPp:显示进程
配合grep 来筛选端口,比如查8317 。
ss-lntp |grep 8317
nft/ufw
如果还是不能访问,就要检查nft/ufw问题。可以用下面的代码来查看规则集。
sudo nft list ruleset
INPUT链顺序是按规则写入顺序匹配的。如果有下面这样的规则:
counter packets 525 bytes 31692 reject with icmp type host-prohibited
它的含义是:无条件拒绝所有走到这里的输入流量,等价于逻辑伪代码:
if (前面的规则都没匹配) {
REJECT 所有包
}
如果我们的放行规则在这样的规则后面,就不会生效,没走到我们的规则,数据包已经被reject了。我们可以删掉旧reject。
sudo nft delete rule ip filter INPUT handle9
然后在最后插入一条新的reject。
sudo nft add rule ip filter INPUT reject with icmp type host-prohibited
这样顺序就正确了。
域名访问
我使用Cloudflare大善人的托管服务,配合Let’s Encrypt实现SSL证书自动续期。假设你的域名为bbb.aaa.com 。
0)安装nginx
sudo apt update
sudo apt install-y nginx
启动并设为开机自启:
sudo systemctl enable --now nginx
检查:
systemctl status nginx
1)Cloudflare 侧准备:创建 API Token(只给 DNS 权限)
Cloudflare → My Profile → API Tokens → Create Token
选模板:Edit zone DNS(或自定义)
权限最小化建议:
- Permissions:
Zone / DNS / Edit - Zone Resources: 仅选择
aaa.com这个 zone
创建后复制 Token(只会显示一次)
2)服务器安装 certbot + cloudflare 插件
sudo apt update
sudo apt install-y certbot python3-certbot-dns-cloudflare
3)在服务器保存 Cloudflare Token(给 certbot 用)
sudo mkdir -p /root/.secrets/certbot
sudo nano /root/.secrets/certbot/cloudflare.ini
写入(把 TOKEN 换成你的):
dns_cloudflare_api_token = YOUR_TOKEN_HERE
权限必须收紧(不然 certbot 拒绝用):
sudo chmod 600 /root/.secrets/certbot/cloudflare.ini
4)签发证书(DNS 验证)
sudo certbot certonly \
--dns-cloudflare \
--dns-cloudflare-credentials /root/.secrets/certbot/cloudflare.ini \
-d bbb.aaa.com \
--preferred-challenges dns-01
成功后证书会在:
/etc/letsencrypt/live/bbb.aaa.com/fullchain.pem/etc/letsencrypt/live/bbb.aaa.com/privkey.pem
5)nginx 配置只监听 443 + 反代 8317
sudo nano /etc/nginx/sites-available/cliproxyapi #名字随便,前后一致即可
粘贴:
server {
listen 443 ssl http2;
server_name bbb.aaa.com;
ssl_certificate /etc/letsencrypt/live/bbb.aaa.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/bbb.aaa.com/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
client_max_body_size 50m; # 解决nginx body 太大的问题
location / {
proxy_pass https://127.0.0.1:8317; #如果你和我一样为cpa启用了https,一定要写https
proxy_http_version 1.1;
proxy_set_header Host $host;
# WebSocket
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
# 这里不转发真实IP也许之前就不用设置允许远程访问管理接口?不太懂
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_read_timeout 300s;
}
}
启用并重载:
sudo ln-s /etc/nginx/sites-available/cliproxyapi /etc/nginx/sites-enabled/ #与前面设置的一致即可
sudo nginx-t
sudo systemctl reload nginx
6)自动续期(默认就有),并确保续期后自动 reload nginx
检查系统是否已有定时任务
systemctl list-timers |grep certbot ||true
Ubuntu 通常自带 certbot.timer。
续期后自动 reload nginx(加一个 deploy hook)
创建 hook:
sudo nano /etc/letsencrypt/renewal-hooks/deploy/reload-nginx.sh
写入:
#!/bin/sh
systemctl reload nginx
赋权:
sudo chmod +x /etc/letsencrypt/renewal-hooks/deploy/reload-nginx.sh
尝试运行:
sudo certbot renew --dry-run
看到成功即可。
最后,在Cloudflare中选择Full (strict)模式,开启Always Use HTTPS(即使源站没开80也没关系,CF边缘重定向)。
在服务器安全组/iptables需要放行:443/tcp,参考前文公网IP配置部分。
在服务器安全组/iptables需要放行:443/tcp,参考前文公网IP配置部分。
在服务器安全组/iptables需要放行:443/tcp,参考前文公网IP配置部分。
7)为CLIProxyApi启用TLS
可以在webui中配置,证书文件路径和私钥文件路径都是我们之前生成的。比如:
证书文件路径:/etc/letsencrypt/live/bbb.aaa.com/fullchain.pem
私钥文件路径:/etc/letsencrypt/live/bbb.aaa.comprivkey.pem
或在配置文件中直接修改:
# 服务器绑定主机/接口,默认空字符串同时绑定 IPv4/IPv6。
# 使用 "127.0.0.1" 或 "localhost" 可限制仅本机访问。
host: ""
# 服务器端口
port: 8317
# TLS 设置:启用后使用提供的证书与私钥监听 HTTPS。
tls:
# enable: false
enable: true
cert: "/etc/letsencrypt/live/bbb.aaa.com/fullchain.pem"
key: "/etc/letsencrypt/live/bbb.aaa.comprivkey.pem"
# 管理 API 设置
remote-management:
# 是否允许远程(非 localhost)访问管理接口。
# 为 false 时仅允许 localhost,仍需管理密钥。
# allow-remote: false
allow-remote: true
...
添加账户
可以使用WebUI添加账户,或者使用接口添加,见:
使用
在Codex中使用
启动CLIProxyAPI服务器, 修改 ~/.codex/config.toml 和 ~/.codex/auth.json 文件。
config.toml:
# 无需确认是否执行操作,危险指令,初次接触codex不建议开启,移除#号即可开启
# approval_policy = "never"
# 沙箱模式超高权限,危险指令,初次接触codex不建议开启,移除#号即可开启
# sandbox_mode = "danger-full-access"
model_provider = "cliproxyapi"
model = "gpt-5.3-codex" # 或者是gpt-5,你也可以使用任何我们支持的模型
model_reasoning_effort = "high"
[model_providers.cliproxyapi]
name = "cliproxyapi"
# 1. 公网ip
# base_url = "http://x.x.x.x:8317/v1"
# 2. 本地部署
# base_url = "http://127.0.0.1:8317/v1"
# 3. 域名+SSL
base_url = "https://bbb.aaa.com/v1"
wire_api = "responses"
auth.json:
{
// CLIProxyAPI的密钥,不是大模型接口的密钥。
"OPENAI_API_KEY": "sk-dummy"
}
在Cherry Studio中使用
见官方文档,可以在CLIProxyApi的管理界面查看支持的模型。


