Typecho无服务器迁移云函数的过程

本篇博客将时间从旧系统迁移至Typecho最新版本并且部署到腾讯云SCF云函数中

Typecho升级

按照官方文档 开始升级 - Typecho Docs 删除
将原来的系统 /config.inc.php 和 /usr 目录保留即可
太老版本升级的话可能会出现数据表结构异常,可以先搭建老版本再备份,然后再导入新版本。

文件存储

云函数是不支持写操作的,即不能像平时建站那样将文件上传到/usr/uploads目录 只能在/tmp目录下进行写操作,或者使用云函数的CFS文件系统。但这里考虑到文件CDN加速分发,就直接上传至COS中。

按照腾讯云官方提供的Typecho插件在最新版本中会出现问题,会报错配置信息未找到。
 

2024-02-26T02:06:45.png


我对插件进行了修复直接克隆我仓库下的插件到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(虚拟子网)下
 

2024-02-26T04:02:13.png


将typecho数据导入进去之后就创建云函数 选择函数类型选择Web函数,地域和数据库存放在同一地域。选择本地测试好的源码进行上传,上传完毕之后 进入 函数配置 网络配置 将云函数和云数据库存放在同一VPC内。
 

2024-02-26T04:04:25.png


删除完毕之后进入在线编辑,在 config.inc.php 中配置好数据库信息。host填写云数据库的内网IP即可
然后需要新建云函数的两个入口方法 scf_bootstrap 和 handle.php 去监听入口文件和一些静态资源的访问,直接放在项目目录下即可

2024-02-26T04:11:38.png

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 文件格式。
 

2024-02-26T04:19:23.png


然后针对云函数还需要改写一下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); 即可

2024-02-26T04:24:34.png

  • 10
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Typecho Docker是使用Docker技术来部署和运行Typecho博客平台的一种方法。首先,我们需要拉取Typecho镜像,可以使用以下命令:docker pull 80x86/typecho。拉取镜像完毕后,我们可以创建一个Typecho容器来测试博客平台的运行,可以使用以下命令:docker run -d -p 90:80 --name="typecho" 80x86/typecho。接下来,可以按照《云原生之Docker实战》中的步骤进行环境配置和安装Typecho。首先,需要创建数据挂载目录,并确保镜像支持。然后,可以安装Typecho并进行初始环境配置。最后,可以通过设置界面外观、查看访问效果和查看插件列表等方式进行Typecho的基本使用。最后,可以测试博客效果以确认Typecho在Docker中的运行情况。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [利用Docker从零搭建Typecho博客并启用TLS](https://blog.csdn.net/zt06081108/article/details/115555924)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [【云原生之Docker实战】使用Docker部署Typecho个人博客平台](https://blog.csdn.net/jks212454/article/details/126107261)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值