明确需求
本地开发环境搭建
npm
、pip
、Maven
等管理依赖。node -v npm -v
// 文件名:server.js const http = require('http'); http.createServer((req, res) => { res.writeHead(200, { 'Content-Type': 'text/plain' }); res.end('Hello Android! '); }).listen(3000, () => { console.log('Server running at http://127.0.0.1:3000/'); });
node server.js
服务商 | 特点 |
---|---|
阿里云 | 国内访问快,学生机优惠 |
酷盾安全 | 生态与微信集成,新手友好 |
AWS | 全球覆盖,适合国际化应用 |
Heroku | 免费托管,适合小型项目(需遵守平台限制) |
ssh root@服务器公网IP
apt update apt install nodejs npm -y
scp
或 FTP)。pm2 start server.js --name "my-app" pm2 startup pm2 save
数据库类型 | 适用场景 | 安装命令(Linux) |
---|---|---|
MySQL | 关系型数据存储 | apt install mysql-server |
MongoDB | 文档型数据存储 | apt install mongodb |
SQLite | 轻量级本地存储(适合测试) | 通过 Python/Node.js 库集成 |
const mysql = require('mysql'); const connection = mysql.createConnection({ host: 'localhost', user: 'root', password: 'yourpassword', database: 'testdb' }); connection.connect((err) => { if (err) throw err; console.log('Connected to MySQL!'); });
开放防火墙端口(以 Ubuntu 为例):
ufw allow 3000/tcp
外网访问测试:
http://服务器公网IP:3000
Android 端测试:
OkHttp
或 Retrofit
发送请求: OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() .url("http://服务器公网IP:3000/") .build(); client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) {} @Override public void onResponse(Call call, Response response) throws IOException { Log.d("Server", response.body().string()); } });
问题 | 解决方案 |
---|---|
外网无法访问 | 检查防火墙规则、云服务商安全组配置,确认端口开放。 |
CORS 跨域问题 | 服务器设置响应头:res.setHeader('Access-Control-Allow-Origin', ''); |
服务器崩溃 | 使用 pm2 管理进程,或配置 systemd 服务。 |
SSL 证书配置 | 使用 Let's Encrypt 免费证书,或购买云服务商提供的 SSL 服务。 |
解答:
# 修改 SSH 配置 vi /etc/ssh/sshd_config # 设置 PermitRootLogin no,重启 SSH 服务
ufw enable ufw allow 80/tcp ufw allow 443/tcp
apt update && apt upgrade -y
解答:
Prometheus + Grafana
监控服务器状态