前言
最近ai风靡全球,你也许也想蹭一波版本红利,于是你了解到了api中转站这种利润高,用户广泛,涉及行业广泛的项目
你也许第一个听说的是newapi这个ai中转站,但是你后面还是选择了sub2api
也许是因为不想用docker的缘故,也许是因为外观美感的原因
总之sub2api绝对是除了newapi之外文档最多,用途最广的ai中转站
接下来就动起小手,跟随我一起搭建一个sub2api吧
准备阶段
你需要准备以下东西
1.一台服务器,系统选择linux,推荐使用ubuntu22.04+(除了香港,新加坡,日本的服务器以及腾讯云的所有服务器,有些上游或者账号会风控,推荐雨梦云的美国云服务器)
2.一个已经解析到此服务器的域名(最好不是cn后缀的)
第一步:检查服务器环境
连接你的服务器
通过ssh软件连接你的服务器,没有ssh连接软件可以自行搜索好用的
填入ip与用户名密码连接成功
服务器需要一下工具以及环境
PostgreSQL 15+
Redis 7+
nginx
node.js
pm2
安装脚本:

1
2
3
4
5
sudo apt update && sudo apt install postgresql postgresql-contrib ##安装PostgreSQL
sudo apt update && sudo apt install redis-server ##安装Redis
sudo apt update && sudo apt install nginx ##安装nginx
sudo apt update && sudo apt install -y nodejs ##安装node.js以及npm
sudo npm install pm2 -g ##安装pm2,需要在安装好node.js后执行

如果嫌麻烦或者redis和postgresql安装失败,可以安装宝塔面板并在宝塔面板中安装,直接跳过第二步创建数据库

1
2
3
4
5
node -v ##显示版本号则安装成功
npm -v ##显示版本号则安装成功
pm2 -v ##显示版本号则安装成功
sudo systemctl status postgresql ##查看PostgreSQL服务状态,显示active(running)则表示安装成功
sudo systemctl status redis-server ##查看Redis服务状态,显示active(running)则表示安装成功

第二步:创建postgresql数据库
创建数据库:

1
2
3
4
5
6
sudo systemctl enable postgresql ##启动PostgreSQL服务并设置开机自启
sudo -u postgres psql ##进入PostgreSQL
CREATE USER myuser WITH PASSWORD 'password'; ##创建用户,password替换为数据库密码,myuser替换为你的用户名
CREATE DATABASE mydatabase OWNER myuser; ##创建数据库并指向所有者,mydatabase替换为你的数据库名,myuser替换为你的用户名
GRANT ALL PRIVILEGES ON DATABASE mydatabase TO myuser; ##授权数据库权限,mydatabase替换为你的数据库名,myuser替换为你的用户名
\q ##退出PostgreSQL

第三步:安装sub2api
使用一键安装脚本安装sub2api

1
curl -sSL https://raw.githubusercontent.com/Wei-Shaw/sub2api/main/deploy/install.sh | sudo bash

使用pm2启动sub2api

1
sudo pm2 start sub2api

验证

1
pm2 list ##看到sub2api状态为online

设置开机自启

1
2
sudo pm2 startup ##生成并注册开机自启脚本(按提示执行它给出的命令)
sudo pm2 save ##保存当前进程列表(这样开机时才会自动拉起 sub2)

防火墙放行端口

1
sudo ufw allow 8080

第四步:配置反向代理
安装Certbot(获取ssl证书)

1
2
sudo apt update && sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d example.com -d www.example.com # 替换www.example.com为你的真实域名

执行后:

  1. 输入邮箱地址(用于接收证书到期提醒)
  2. 同意服务条款(输入 A)
  3. 选择是否将 HTTP 自动重定向到 HTTPS(建议选 2,启用重定向)
    证书申请成功后,编辑 Nginx 配置文件:
1
sudo nano /etc/nginx/sites-available/www.example.com # 替换www.example.com为你的真实域名

写入以下完整配置(将 example.com换成你的实际域名):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
server {
listen 80;
server_name example.com;

return 301 https://$server_name$request_uri;
}

server {
listen 443 ssl http2;
server_name example.com;

ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

location / {
proxy_pass http://127.0.0.1:8080;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
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;
proxy_cache_bypass $http_upgrade;
}

# location /static/ {
# alias /path/to/your/static/files/;
# }
}

测试配置并重启 Nginx:

1
2
sudo nginx -t
sudo systemctl reload nginx

验证

1
curl http://127.0.0.1:8080 ##检查程序是否在8080端口运行

第五步:配置sub2api
浏览器访问你的域名进入安装页面

  1. 填写postgresql数据库信息
    postgresql的密码和用户名还有数据库名为你上面创建的
  2. 填写redis数据库信息
    redis的默认端口为6379
    其他信息默认即可
  3. 填写sub2api的配置信息
  4. 完成安装,进入主页面
  5. 配置易支付,详情可见另一篇文章
    常见问题
  6. 证书申请失败(DNS 验证超时)
    确保域名已正确解析
    在电脑上点击win+r,输入cmd,输入ping example.com,查看是否能ping通 example.com改为你的实际域名
  7. 80 端口已被占用
    一般是先启动了nginx,导致80端口被nginx占用
    先停掉占用 80 的服务(如 sudo systemctl stop nginx),申请完再启动
  8. 502 Bad Gateway(网关错误)/ 404 Not Found (找不到网页)
    检查
1
2
3
4
curl http://127.0.0.1:8080 ##检查程序是否在8080端口运行
sudo tail -f /var/log/nginx/error.log ##检查nginx错误日志
sudo netstat -tlnp | grep 8080 ##确认端口监听
sudo ufw status ##查看ufw防火墙状态

更多问题欢迎进群交流点击联系