🚀 服务器部署指南

三长两短 - PHP后端部署完整教程

📋 环境要求

最低要求

推荐配置

PHP扩展

php -m | grep -E 'pdo|mysql|json|mbstring|openssl'

需要启用:PDO, pdo_mysql, json, mbstring, openssl

🎯 方式1:Web安装向导(推荐)

✅ 最简单的方式 - 通过浏览器完成全部配置,无需命令行操作
1 上传文件
# 下载并解压
cd /www/wwwroot/
unzip threelongtwo-server.zip
cd um.dazitai.com

# 设置权限
chmod 755 /www/wwwroot/um.dazitai.com
chmod 755 install/
2 访问安装向导

浏览器打开:http://um.dazitai.com/install/

3 按照向导完成配置
  1. 环境检查 - 自动检测PHP和扩展
  2. 数据库配置 - 输入MySQL信息(自动创建数据库)
  3. 应用配置 - 设置应用名称和URL
  4. 开始安装 - 自动生成配置文件和数据表
  5. 完成!
4 删除安装目录
rm -rf /www/wwwroot/um.dazitai.com/install/
💡 提示:安装完成后,访问首页会看到服务器状态和API可用性检查

🎛️ 方式2:宝塔面板部署

环境搭建

1 安装宝塔面板
# CentOS
yum install -y wget && wget -O install.sh https://download.bt.cn/install/install_6.0.sh && sh install.sh

# Ubuntu/Debian
wget -O install.sh https://download.bt.cn/install/install-ubuntu_6.0.sh && sudo bash install.sh
2 安装运行环境

在宝塔面板 > 软件商店中安装:

  • Nginx 1.22.1
  • MySQL 5.7.40
  • PHP 8.0

PHP配置

3 配置PHP

宝塔面板 > 软件商店 > PHP 8.0 > 设置

  • 安装扩展:opcache, redis(可选)
  • 禁用函数:删除 putenv, proc_open
  • 上传限制:调整 upload_max_filesize = 20M

创建网站

4 添加站点

宝塔面板 > 网站 > 添加站点

  • 域名:um.dazitai.com
  • 根目录:/www/wwwroot/um.dazitai.com
  • PHP版本:8.0
  • 创建数据库:是

配置Nginx

5 Nginx配置

网站 > um.dazitai.com > 设置 > 配置文件

server {
    listen 80;
    server_name um.dazitai.com;
    root /www/wwwroot/um.dazitai.com;
    index index.php index.html;

    # API路由
    location /api/ {
        try_files $uri $uri/ /api/index.php?$query_string;
    }

    # PHP处理
    location ~ \.php$ {
        fastcgi_pass unix:/tmp/php-cgi-80.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    # 安全配置
    location ~ /\. {
        deny all;
    }
}

上传文件并运行安装

6 上传并安装
  1. 通过宝塔文件管理器上传项目文件
  2. 访问 http://um.dazitai.com/install/
  3. 按向导完成配置

🐳 方式3:Docker部署

优势:环境隔离、快速部署、易于迁移
1 准备Docker环境
# 安装Docker
curl -fsSL https://get.docker.com | bash
systemctl start docker
systemctl enable docker

# 安装Docker Compose
curl -L "https://github.com/docker/compose/releases/download/v2.20.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
2 使用docker-compose.yml
version: '3.8'

services:
  php:
    image: php:8.1-fpm-alpine
    volumes:
      - ./:/var/www/html
    depends_on:
      - mysql

  nginx:
    image: nginx:alpine
    ports:
      - "80:80"
    volumes:
      - ./:/var/www/html
      - ./nginx.conf:/etc/nginx/conf.d/default.conf
    depends_on:
      - php

  mysql:
    image: mysql:8.0
    environment:
      MYSQL_ROOT_PASSWORD: root_password
      MYSQL_DATABASE: threelongtwo
      MYSQL_USER: app_user
      MYSQL_PASSWORD: app_password
    volumes:
      - mysql_data:/var/lib/mysql

  redis:
    image: redis:alpine
    command: redis-server --appendonly yes

volumes:
  mysql_data:
3 启动容器
docker-compose up -d
4 访问安装向导

浏览器打开:http://localhost/install/

⚙️ 方式4:手动部署

安装MySQL

# CentOS/RHEL
yum install -y mysql-server
systemctl start mysqld
systemctl enable mysqld

# Ubuntu/Debian
apt install -y mysql-server
systemctl start mysql
systemctl enable mysql

创建数据库

mysql -u root -p

CREATE DATABASE threelongtwo CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'app_user'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON threelongtwo.* TO 'app_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;

导入数据库架构

mysql -u app_user -p threelongtwo < database/schema.sql

配置.env文件

cp .env.example .env
vim .env

# 编辑以下内容
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=threelongtwo
DB_USERNAME=app_user
DB_PASSWORD=your_password

配置Nginx

vim /etc/nginx/conf.d/threelongtwo.conf

# 添加配置(参考宝塔部署中的Nginx配置)

nginx -t
systemctl reload nginx

设置权限

chown -R www-data:www-data /var/www/threelongtwo
chmod -R 755 /var/www/threelongtwo
chmod 644 .env

✅ 验证部署

检查健康状态

curl http://um.dazitai.com/api/health
预期响应:
{
  "status": "healthy",
  "services": {
    "database": {
      "status": "connected"
    }
  }
}

测试API接口

# 测试分享码检查
curl -X POST http://um.dazitai.com/api/share-code/check \
  -H "Content-Type: application/json" \
  -d '{"shareCode":"ABC123","codeLength":6}'

🔧 故障排查

常见问题

1. 404错误

  • 检查Nginx配置中的 try_files 是否正确
  • 确认 /api/ 路由配置
  • 重启Nginx:systemctl reload nginx

2. 数据库连接失败

  • 检查.env文件配置
  • 确认MySQL服务运行:systemctl status mysql
  • 测试连接:mysql -u app_user -p -h 127.0.0.1

3. 权限错误

  • 检查文件权限:ls -la
  • 确认PHP-FPM用户:ps aux | grep php-fpm
  • 设置正确的所有者:chown -R www-data:www-data .

调试工具

← 返回首页