系统配置
本文档介绍 CarefreeCMS 的各项配置选项和最佳实践。
配置文件
后端配置
CarefreeCMS 后端配置文件位于 backend/config/ 目录:
backend/config/
├── app.php # 应用配置
├── database.php # 数据库配置
├── cache.php # 缓存配置
├── filesystem.php # 文件系统配置
├── log.php # 日志配置
├── middleware.php # 中间件配置
└── route.php # 路由配置
环境变量
环境相关配置通过 .env 文件管理:
[APP]
APP_DEBUG = true
APP_TRACE = false
[DATABASE]
DB_HOST = 127.0.0.1
DB_NAME = cms_database
DB_USER = root
DB_PASS = password
DB_PORT = 3306
DB_CHARSET = utf8mb4
[JWT]
JWT_SECRET = your_secret_key
JWT_EXPIRE = 7200
[CORS]
CORS_ALLOWED_ORIGINS = http://localhost:5173
[UPLOAD]
UPLOAD_PATH = uploads
UPLOAD_MAX_SIZE = 10485760
UPLOAD_ALLOWED_EXT = jpg,png,gif,pdf
[CACHE]
CACHE_TYPE = file
CACHE_PREFIX = cache_
网站配置
基本信息
在管理后台进行配置:系统管理 → 网站配置
网站信息
网站名称: 我的网站
网站域名: www.example.com
网站关键词: 内容管理,博客,新闻
网站描述: 这是一个基于CarefreeCMS构建的网站
ICP备案号: 京ICP备12345678号
联系方式
联系邮箱: contact@example.com
联系电话: 010-12345678
联系地址: 北京市朝阳区xxx路xxx号
版权信息
版权所有: © 2024 我的网站
底部代码: <!-- 统计代码或其他脚本 -->
Logo 和图标
网站 Logo
- 推荐尺寸:200x60px
- 格式:PNG(支持透明)
- 用于网站头部显示
Favicon
- 推荐尺寸:32x32px 或 64x64px
- 格式:ICO 或 PNG
- 用于浏览器标签图标
默认封面
- 推荐尺寸:1200x630px
- 格式:JPG 或 PNG
- 用于文章无封面时的默认图
上传配置
文件上传
允许的文件类型
图片: jpg,jpeg,png,gif,webp,svg
文档: pdf,doc,docx,xls,xlsx,ppt,pptx,txt
压缩: zip,rar,7z
视频: mp4,avi,mov,wmv
音频: mp3,wav,ogg
文件大小限制
单文件大小: 10MB
图片最大宽度: 1920px
图片最大高度: 1080px
上传路径
基础路径: public/uploads/
按日期存储: uploads/2024/01/15/
文件重命名: 时间戳_随机字符串.扩展名
图片处理
压缩设置
图片质量: 85%
是否压缩: 是
压缩阈值: 500KB
缩略图生成
缩略图宽度: 300px
缩略图高度: 200px
裁剪方式: 等比缩放
水印设置
是否启用: 否
水印位置: 右下角
水印透明度: 80%
水印图片: watermark.png
数据库配置
连接设置
编辑 backend/config/database.php:
return [
'default' => env('database.driver', 'mysql'),
'connections' => [
'mysql' => [
'type' => env('database.type', 'mysql'),
'hostname' => env('database.hostname', '127.0.0.1'),
'database' => env('database.database', 'carefreecms'),
'username' => env('database.username', 'root'),
'password' => env('database.password', ''),
'hostport' => env('database.hostport', '3306'),
'charset' => env('database.charset', 'utf8mb4'),
'prefix' => env('database.prefix', ''),
// 连接池配置
'deploy' => 0,
'rw_separate' => false,
'master_num' => 1,
'slave_no' => '',
// 数据库查询类
'query' => '\\think\\db\\Query',
// 字段缓存
'fields_cache' => true,
],
],
];
连接池(生产环境)
启用主从分离:
'rw_separate' => true,
'master_num' => 1,
'slave_no' => '1,2',
缓存配置
缓存驱动
支持多种缓存驱动:
文件缓存(默认)
'default' => 'file',
'stores' => [
'file' => [
'type' => 'file',
'path' => runtime_path() . 'cache/',
'prefix' => 'cache_',
'expire' => 0,
],
],
Redis 缓存(推荐)
'default' => 'redis',
'stores' => [
'redis' => [
'type' => 'redis',
'host' => '127.0.0.1',
'port' => 6379,
'password' => '',
'select' => 0,
'timeout' => 0,
'expire' => 0,
'persistent' => false,
'prefix' => 'cache_',
],
],
缓存策略
页面缓存
// 首页缓存 1 小时
'index' => 3600,
// 文章列表缓存 30 分钟
'article_list' => 1800,
// 文章详情缓存 2 小时
'article_detail' => 7200,
数据缓存
// 分类树缓存 24 小时
'category_tree' => 86400,
// 热门文章缓存 1 小时
'hot_articles' => 3600,
JWT 认证配置
JWT 设置
[JWT]
# 密钥(必须设置强随机字符串)
JWT_SECRET = your_secret_key_here
# 过期时间(秒)
JWT_EXPIRE = 7200
# 签发者
JWT_ISSUER = cms_system
# 接收者
JWT_AUDIENCE = cms_user
# 刷新令牌过期时间
JWT_REFRESH_EXPIRE = 604800
生成密钥
# 方式一:PHP 生成
php -r "echo base64_encode(random_bytes(32));"
# 方式二:OpenSSL 生成
openssl rand -base64 32
# 方式三:在线生成
https://www.random.org/strings/
邮件配置
SMTP 设置
'mail' => [
'type' => 'smtp',
'host' => 'smtp.example.com',
'port' => 465,
'username' => 'noreply@example.com',
'password' => 'your_password',
'from' => ['noreply@example.com' => '网站名称'],
'secure' => 'ssl', // ssl 或 tls
],
邮件模板
系统邮件模板位于 backend/view/email/:
register.html- 注册欢迎邮件reset_password.html- 密码重置邮件verify_email.html- 邮箱验证邮件
静态化配置
基础设置
'static' => [
// 静态文件输出目录
'output_path' => root_path() . 'html/',
// 模板目录
'template_path' => root_path() . 'templates/',
// 默认模板
'default_theme' => 'default',
// URL 模式
'url_mode' => [
'article' => '/article/{id}.html',
'category' => '/category/{alias}.html',
'tag' => '/tag/{id}.html',
'page' => '/{alias}.html',
],
],
生成规则
文章静态页
'article' => [
'path' => 'article/',
'filename' => '{id}.html',
'template' => 'article.html',
],
分类列表页
'category' => [
'path' => 'category/',
'filename' => '{alias}_{page}.html',
'template' => 'category.html',
'pagination' => true,
'per_page' => 20,
],
SEO 配置
全局 SEO
默认标题后缀: - 网站名称
分隔符: "-" 或 "|"
关键词数量: 5-8个
描述长度: 120-160字符
Sitemap 配置
'sitemap' => [
// 输出路径
'path' => 'sitemap.xml',
// 更新频率
'changefreq' => [
'index' => 'daily',
'article' => 'weekly',
'category' => 'daily',
],
// 优先级
'priority' => [
'index' => '1.0',
'article' => '0.8',
'category' => '0.6',
],
],
Robots.txt
在 系统管理 → SEO管理 → Robots.txt 中配置:
User-agent: *
Allow: /
Disallow: /admin/
Disallow: /api/
Sitemap: https://www.example.com/sitemap.xml
安全配置
CORS 设置
[CORS]
# 允许的源
CORS_ALLOWED_ORIGINS = http://localhost:5173,https://admin.example.com
# 允许的方法
CORS_ALLOWED_METHODS = GET,POST,PUT,DELETE,OPTIONS
# 允许的头
CORS_ALLOWED_HEADERS = Content-Type,Authorization,X-Requested-With
# 是否允许凭证
CORS_ALLOW_CREDENTIALS = true
# 预检请求缓存时间
CORS_MAX_AGE = 3600
请求限流
'throttle' => [
// 登录接口限流
'login' => [
'rate' => 5, // 5次
'period' => 60, // 每60秒
],
// API 接口限流
'api' => [
'rate' => 100, // 100次
'period' => 60, // 每60秒
],
],
防护设置
XSS 防护
// 自动过滤
'xss_filter' => true,
// 白名单标签
'allowed_tags' => '<p><a><img><strong><em>',
CSRF 防护
'csrf' => [
'enable' => true,
'token_name' => '__token__',
'token_field' => '__token__',
],
日志配置
日志级别
'log' => [
'default' => 'file',
'channels' => [
'file' => [
'type' => 'file',
'path' => runtime_path() . 'log/',
'level' => ['error', 'warning', 'info'],
'file_size' => 2097152, // 2MB
'max_files' => 10,
'time_format' => 'Y-m-d H:i:s',
],
],
],
操作日志
记录用户操作:
- 登录/登出
- 创建/编辑/删除内容
- 配置修改
- 敏感操作
性能优化
OPcache 配置
编辑 php.ini:
[opcache]
opcache.enable=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=10000
opcache.revalidate_freq=60
opcache.fast_shutdown=1
数据库优化
连接池
'pool_size' => 20,
'min_connections' => 5,
'max_connections' => 20,
'wait_timeout' => 3,
查询缓存
SET GLOBAL query_cache_size = 268435456;
SET GLOBAL query_cache_type = ON;
前端优化
打包配置 (frontend/vite.config.js)
export default {
build: {
// 压缩
minify: 'terser',
terserOptions: {
compress: {
drop_console: true,
},
},
// 分包
rollupOptions: {
output: {
manualChunks: {
'element-plus': ['element-plus'],
'vue-vendor': ['vue', 'vue-router', 'pinia'],
},
},
},
// 生成 gzip
reportCompressedSize: true,
},
}
备份配置
自动备份
设置定时任务:
# 每天凌晨 2 点备份数据库
0 2 * * * cd /path/to/cms/backend && php think db:backup
备份保留
'backup' => [
// 保留天数
'keep_days' => 30,
// 备份路径
'path' => runtime_path() . 'backup/',
// 压缩
'compress' => true,
],
多站点配置
站点配置
每个站点可独立配置:
- 域名绑定
- 模板主题
- SEO 设置
- 存储配置
详见 多站点管理
环境切换
开发环境
APP_DEBUG = true
APP_TRACE = true
LOG_LEVEL = debug
CACHE_TYPE = file
生产环境
APP_DEBUG = false
APP_TRACE = false
LOG_LEVEL = error
CACHE_TYPE = redis
测试环境
APP_DEBUG = true
APP_TRACE = false
LOG_LEVEL = info
CACHE_TYPE = file
配置优化建议
安全性
- 使用强随机密钥
- 启用 HTTPS
- 配置防火墙
- 定期更新密码
性能
- 启用 Redis 缓存
- 配置 OPcache
- 使用 CDN
- 优化数据库
可维护性
- 统一配置管理
- 版本控制配置文件
- 文档记录配置变更
- 定期审查配置
监控
- 配置日志收集
- 设置错误告警
- 监控资源使用
- 定期备份
