本篇博客将时间从旧系统迁移至Typecho最新版本并且部署到腾讯云SCF云函数中
Typecho升级
按照官方文档 开始升级 - Typecho Docs 删除
将原来的系统 /config.inc.php 和 /usr 目录保留即可
太老版本升级的话可能会出现数据表结构异常,可以先搭建老版本再备份,然后再导入新版本。
文件存储
云函数是不支持写操作的,即不能像平时建站那样将文件上传到/usr/uploads目录 只能在/tmp目录下进行写操作,或者使用云函数的CFS文件系统。但这里考虑到文件CDN加速分发,就直接上传至COS中。
按照腾讯云官方提供的Typecho插件在最新版本中会出现问题,会报错配置信息未找到。
我对插件进行了修复直接克隆我仓库下的插件到Plugin目录下按照Readme文件配置安装即可
仓库地址:https://github.com/tiancheng-66/tencentcloud-typecho-plugin-cos
PR地址:https://github.com/Tencent-Cloud-Plugins/tencentcloud-typecho-plugin-cos/pull/4
至于COS和CDN的配置本篇文章就不做讲解了,很简单
部署云函数
首先我们需要先创建腾讯云的TDSQL-ServerLess版本的数据库,然后保证数据库和云函数在同一VPC(虚拟子网)下
将typecho数据导入进去之后就创建云函数 选择函数类型选择Web函数,地域和数据库存放在同一地域。选择本地测试好的源码进行上传,上传完毕之后 进入 函数配置 网络配置 将云函数和云数据库存放在同一VPC内。
删除完毕之后进入在线编辑,在 config.inc.php 中配置好数据库信息。host填写云数据库的内网IP即可
然后需要新建云函数的两个入口方法 scf_bootstrap 和 handle.php 去监听入口文件和一些静态资源的访问,直接放在项目目录下即可
scf_bootstrap 代码:
#!/bin/bash
/var/lang/php80/bin/php -c /var/runtime/php80 -S 0.0.0.0:9000 handle.php
handle.php 代码:
<?php
error_reporting(0);
$extension_map = array(
"css" => "text/css",
"js" => "application/javascript",
"png" => "image/png",
"jpeg" => "image/jpeg",
"jpg" => "application/x-jpg",
"svg" => "image/svg+xml",
"gif" => "image/gif",
"pdf" => "application/pdf",
"mp4" => "video/mpeg4",
"bmp" => "application/x-bmp",
"c4t" => "application/x-c4t",
"img" => "application/x-img",
"m2v" => "video/x-mpeg",
"mp2v" => "video/mpeg",
"mpeg" => "video/mpg",
"ppt" => "application/x-ppt",
"rm" => "application/vnd.rn-realmedia",
"swf" => "video/mpeg4",
"tif" => "image/tiff",
"tiff" => "image/tiff",
"ttf" => "application/x-font-ttf",
"woff" => "application/x-font-woff",
"vcf" => "text/x-vcard",
"wav" => "audio/wav",
"wma" => "audio/x-ms-wma",
"wmv" => "video/x-ms-wmv",
"apk" => "application/vnd.android.package-archive",
"m1v" => "video/x-mpeg",
"m3u" => "audio/mpegurl",
"mp2" => "audio/mp2",
"mp3" => "audio/mp3",
"mpa" => "video/x-mpg",
"mpe" => "video/x-mpeg",
"mpg" => "video/mpg",
"mpv2" => "video/mpeg",
"rmvb" => "application/vnd.rn-realmedia-vbr",
"torrent" => "application/x-bittorrent",
);
$request_uri = explode("?", $_SERVER['REQUEST_URI']);
$local_file_path = $_SERVER['DOCUMENT_ROOT'] . urldecode($request_uri[0]);
$remote_file_path = "/mnt/" . urldecode($request_uri[0]);
if ( $local_file_path == __FILE__ ) {
http_response_code(400);
echo 'Sorry';
exit();
}
$db_mode = "";
$db_mode = getenv("DB_MODE");
$cdb_st = microtime(true);
$split = explode(".", $local_file_path);
$extension = end($split);
$mapped_type = $extension_map[$extension];
if ( $mapped_type && file_exists( $remote_file_path ) ) {
header("Content-Type: {$mapped_type}");
$file_size=filesize($remote_file_path);
header("Accept-Length:$file_size");
$fp=fopen($remote_file_path,"r");
$buffer=1024;
$file_count=0;
while(!feof($fp)&&($file_size-$file_count>0)){
$file_data=fread($fp,$buffer);
$file_count+=$buffer;
echo $file_data;
}
fclose($fp);
} elseif ( $mapped_type && file_exists( $local_file_path ) ) {
header("Content-Type: {$mapped_type}");
$file_size=filesize($local_file_path);
header("Accept-Length:$file_size");
$fp=fopen($local_file_path,"r");
$buffer=1024;
$file_count=0;
while(!feof($fp)&&($file_size-$file_count>0)){
$file_data=fread($fp,$buffer);
$file_count+=$buffer;
echo $file_data;
}
fclose($fp);
} elseif ( $extension == "php" && file_exists( $local_file_path ) ) {
$cdb_et = microtime(true);
$cdb_time = ($cdb_et - $cdb_st) * 1000 . 'ms';
header("X-cdb-time:{$cdb_time}");
header('Cache-Control:no-cache,must-revalidate');
header('Pragma:no-cache');
header("Expires:0");
header("X-ExecFile: {$local_file_path}");
require( $local_file_path );
} elseif ( substr($local_file_path, -1) == "/" && file_exists( $local_file_path . "index.php" ) ) {
$cdb_et = microtime(true);
$cdb_time = ($cdb_et - $cdb_st) * 1000 . 'ms';
header("X-cdb-time:{$cdb_time}");
header('Cache-Control:no-cache,must-revalidate');
header('Pragma:no-cache');
header("Expires:0");
$exec_file_path = $local_file_path . "index.php";
header("X-ExecFile: {$exec_file_path}");
require( $exec_file_path );
} else {
$cdb_et = microtime(true);
$cdb_time = ($cdb_et - $cdb_st) * 1000 . 'ms';
header("X-cdb-time:{$cdb_time}");
header('Cache-Control:no-cache,must-revalidate');
header('Pragma:no-cache');
header("Expires:0");
$exec_file_path = dirname(__FILE__) . '/index.php';
header("X-ExecFile: {$exec_file_path}");
require( $exec_file_path );
}
这里需要注意一下 scf_bootstrap 文件的权限设置为755或777 并且文件格式为UNIX 直接在在线编辑框中用vi编辑器打开 scf_bootstrap 文件,执行:set ff命令查看文件格式,验证是否为 UNIX 文件格式。如果不是,可以使用:set fileformat=unix命令将其更改为 UNIX 文件格式。
然后针对云函数还需要改写一下Typecho的文件 /var/Typecho/Common.php 中的 slugName 函数 原代码中使用的mbstring库在云函数中不能使用正则去替换该用PCRE正则表达式。
public static function slugName(?string $str, ?string $default = null, int $maxLength = 128): ?string
{
$str = trim($str ?? '');
if (!strlen($str)) {
return $default;
}
// 使用 PCRE 正则表达式进行匹配
$pattern = "/[\w" . preg_quote('_-') . "]+/";
preg_match_all($pattern, $str, $matches);
$return = '';
if (!empty($matches[0])) {
$return = implode('-', $matches[0]);
}
$str = trim($return, '-_');
$str = !strlen($str) ? $default : $str;
return substr($str, 0, $maxLength);
}
OK到这一步就万事俱备点击部署即可。然后绑定域名进入触发管理绑定即可。
启用HTTPS
在config.inc.php中增加一条 define('__TYPECHO_SECURE__',true); 即可