Trojan代理服务服务器搭建教程:零基础从零开始完整实战
如果你想自己搭建一个稳定、隐蔽的代理服务,Trojan 是一个不错选择。
它伪装成 HTTPS 流量,不容易被墙识别。
下面我会用最直白的步骤,带你从头部署一套完整可用的 Trojan 代理服务。
动手前需要备好哪些东西
- 一台境外服务器:推荐 CentOS 7/8 或 Ubuntu 20.04+,内存 512MB 以上即可。
- 一个域名:建议用 Namesilo、Cloudflare 等平台购买,并做好 A 记录指向你的服务器 IP。
- SSH 客户端:Windows 用 PuTTY,Mac/Linux 直接用终端。
- 基础 Linux 命令知识:会用
cd、vim、systemctl就行,我会给出完整命令。
第一步:给服务器装上基础环境
登录服务器后,先更新系统并安装必要软件包。
Ubuntu / Debian 用户执行:
apt update && apt upgrade -y
apt install curl wget vim unzip nginx -y
CentOS / Rocky Linux 用户执行:
yum update -y
yum install curl wget vim unzip nginx -y
装完后启动 Nginx 并设为开机自启:
systemctl start nginx
systemctl enable nginx
第二步:获取 SSL 证书(Trojan 必须依赖 HTTPS)
Trojan 需要一张合法的 SSL 证书才能伪装成 HTTPS 通讯。
推荐使用 acme.sh 申请 Let's Encrypt 免费证书。
先用你的域名解析 IP,确保 ping yourdomain.com 返回服务器 IP。
安装 acme.sh:
curl https://get.acme.sh | sh
source ~/.bashrc
然后申请证书(把 yourdomain.com 换成你自己的域名):
acme.sh --issue -d yourdomain.com --webroot /usr/share/nginx/html
成功后证书会自动存放在 ~/.acme.sh/yourdomain.com/ 目录下。
记下这两个文件路径:
- 公钥:
~/.acme.sh/yourdomain.com/fullchain.cer - 私钥:
~/.acme.sh/yourdomain.com/yourdomain.com.key
第三步:安装并配置 Trojan 服务端
下载最新版 Trojan:
wget https://github.com/trojan-gfw/trojan/releases/download/v1.16.0/trojan-1.16.0-linux-amd64.tar.xz
tar xf trojan-1.16.0-linux-amd64.tar.xz
cd trojan
编辑配置文件 config.json(用 vim 或 nano):
cp examples/server.json-example config.json
vim config.json
修改以下几个关键项:
{
"run_type": "server",
"local_addr": "0.0.0.0",
"local_port": 443,
"remote_addr": "127.0.0.1",
"remote_port": 80,
"password": [
"你自定义的密码"
],
"ssl": {
"cert": "/root/.acme.sh/yourdomain.com/fullchain.cer",
"key": "/root/.acme.sh/yourdomain.com/yourdomain.com.key"
}
}
local_port用 443(常用 HTTPS 端口,更隐蔽)。password改成你自己的强密码,连接客户端时要用。ssl.cert和ssl.key填刚才申请的证书绝对路径。
配置 Nginx 反代(提升伪装效果)
为了让 Trojan 在 443 端口与正常网站共存,需要用 Nginx 做前端反代。
在 /etc/nginx/nginx.conf 的 server 块里加上:
server {
listen 127.0.0.1:80;
server_name yourdomain.com;
root /usr/share/nginx/html;
index index.html index.htm;
}
然后重启 Nginx:
systemctl restart nginx
启动 Trojan 服务
./trojan -c config.json &
建议设置开机自启。
创建一个 systemd 服务文件 /etc/systemd/system/trojan.service:
[Unit]
Description=Trojan Server
After=network.target
[Service]
Type=simple
ExecStart=/root/trojan/trojan -c /root/trojan/config.json
Restart=on-failure
[Install]
WantedBy=multi-user.target
之后执行:
systemctl daemon-reload
systemctl enable trojan
systemctl start trojan
第四步:用客户端连接验证是否成功
在你自己的电脑或手机上安装 Trojan 客户端(Windows 用 Trojan-Qt5,
Android 用 Igniter,
macOS 用 V2rayU 也支持 Trojan)。
填好以下信息:
- 服务器地址:你的域名
- 端口:443
- 密码:你在
config.json里设置的密码 - 传输方式:默认 TCP
连接成功后,打开浏览器访问 ip.sb 或 whatismyip.com,应该看到你服务器的 IP 地址,说明代理生效。
避坑指南与高频问题
Q:启动 Trojan 提示 bind: permission denied
A:端口1024以下需要 root 权限,用 sudo ./trojan -c config.json 或直接以 root 登录启动。
Q:客户端连接后无法打开网页
A:检查服务器防火墙是否放行了 443 端口。执行 ufw allow 443/tcp(Ubuntu)或 firewall-cmd --add-port=443/tcp --permanent(CentOS)。
Q:证书申请失败,提示域名未指向服务器
A:确认域名解析已生效,等几分钟再试。也可以临时用 Nginx 默认站点验证。
Q:Trojan 服务正常,但访问时显示 Nginx 默认页面
A:说明 Trojan 没有正确处理流量。检查 config.json 中 remote_port 是否为 80,且 Nginx 确实在 80 端口监听 127.0.0.1。同时确认 Trojan 的 local_port 不是被 Nginx 占用。
避坑提醒:不要直接用 root 账户运行 Trojan,建议创建一个普通用户 trojan 来管理,避免安全风险。
另外定期更新 Trojan 版本和证书,确保长期可用。
如果你跟着本文一步步操作,应该能在半小时内跑通自己的 Trojan 代理服务。
建议先在小流量环境下测试稳定后,再正式使用。
遇到异常时优先回看上文“避坑指南与高频问题”部分,多数问题都能解决。