网站博客

Typecho博客搭建

2019-12-02  本文已影响0人  CSeroad

前言

自从搭建了onedrive网盘后,就想着再搭建了博客了。个人比较喜欢简洁的Typecho模板,也支持markdown语法,可以将简书的文章同步到Tyecho博客上。

搭建

搭建起来比较简单
下载Typecho源码放到宝塔,配置域名进行访问即可。具体操作参考 Onedrive搭建个人网盘

image.png

按照操作创建config.inc.php文件,并赋予权限。

升级

先进行升级,为了更好的适用于插件。
参考文章更新 Typecho 1.1

image.png

主题

Typecho有许多好看使用的主题。Typecho 主题:修改方法及优秀主题推荐

尝试使用的是Maupassant极简主题,从github上进行下载
https://github.com/pagecho/maupassant/

下载到Typecho/usr/themes目录下。

外观

查看网站外观,可切换外观。

插件

为了方便操作、浏览,我用了许多插件。

image.png

依次介绍一下。

Parsedown插件

高效的markdown解析插件。github地址

Paste Image插件

优秀的图片上传插件
可以直接截图后粘贴。github地址

image.png

TinyPNG 插件

发现网页在加载图片时比较缓慢,TinyPNG 插件可以在上传图片的时候对其进行压缩,缩小图片大小,加开访问速度。配合Paste Image插件可谓方便之极。
参考文章一款图片压缩插件《TinyPNG for Typecho》
需要申请TinyPNG ApiKey后在后台配置。

附加
原本的想法是写段php代码,将简书的图片下载到本地后,再将数据库的text字段的图片外链地址改为本地链接地址。但是在代码写出后,发现图片下载下来特别大,即使经过optipng命令进行压缩后还是比较大。所以这个方法就夭折了,采用了插件。

<?php 
$curl = curl_init();
$con = mysqli_connect('localhost', 'typecho', 'x2P3r2tNxxxx','typecho');//数据库信息修改为你自己的数据库账号信息
$sql = "select * from typecho_contents where type = 'post' order by cid asc";//jq_contents修改为你的数据库中的contents表名称
$result = mysqli_query($con,$sql);
$row=mysqli_fetch_assoc($result);

preg_match_all('/https:\/\/upload-images.jianshu.io\/.*.(jpg|png).*\d+/', $row['text'], $matche);
//var_dump($matche);
preg_match_all('/https:\/\/upload-images.jianshu.io\/.*.(jpg|png)/', $row['text'], $matches);
//var_dump($matches);

$local_date =  date('Y-m-d', $row['created']);
$arr_date = explode('-', $local_date);

foreach($matches[0] as $k=>$pic){
    //var_dump($matches[0]);
    echo $matches[0][$k];
    curl_setopt($curl, CURLOPT_URL, $matches[0][$k]);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT,0);
    $data = curl_exec($curl);
    $ret = file_put_contents(dirname(__FILE__)."/usr/uploads/".$arr_date[0]."/".$arr_date[1]."/".$arr_date[2]."/".$k.".".$matches[1][$k], $data);
    //print_r(dirname(__FILE__)."/usr/uploads/".$arr_date[0]."/".$arr_date[1]."/".$arr_date[2]."/".$k.".".$matches[1][$k]." ret:$ret<br />");
    $old_img_path = $matche[0][$k];
    echo $old_img_path;
    $new_img_path = "http://cseroad.xyz/usr/uploads/".$arr_date[0]."/".$arr_date[1]."/".$arr_date[2]."/".$k.".".$matches[1][$k];
    $row['text'] = str_replace($old_img_path, $new_img_path, $row['text']);
}
//echo $row['text'];

$newtext = mysqli_real_escape_string($con,$row['text']);
$sql3 = "UPDATE `typecho_contents` SET  `text` =  '$newtext' where cid=".$row['cid'];
//echo $sql3;
mysqli_query($con,$sql3);
mysqli_close($con);
curl_close($curl);

Views插件

来统计浏览次数。
参考文章
Typecho 浏览统计插件Views使用方法
Typecho文章浏览次数统计设置
文章里包含下载地址。

只显示摘要

在修改外观处,找到archive.php和index.php页面。
content(...);替换为excerpt(300,'...');

image.png

修改typecho首页显示文章的数量
function.php页面末尾增加以下代码

function themeInit($archive) {
    if ($archive->is('index')) {
        $archive->parameter->pageSize = 6;
    }
}

加固

博客搭建也需要一些简单加固。

删除登录功能

如删除sidebar.php页面的登录功能。


image.png

修改后台地址

修改config.inc.php的后台路径,并对admin目录进行重命名。
删除index.html、install.php、install文件。

结尾

优化到这暂时可以浏览使用了,以后有需求再加吧。
blog地址为 http://cseroad.xyz

上一篇下一篇

猜你喜欢

热点阅读