前言

搭建博客的方案有很多,比如静态博客(hugo、hexo)、WordPress和typecho等。

静态博客在本地使用支持MarkDown的工具撰写文章,然后使用hugo等工具生成html页面,后续托管在GitHub上,优点是不用担心后续的托管问题,比较省心;缺点是没有管理后台。

WordPress是很成熟的搭建网站的方案,有各种丰富的插件、主题可供使用,但是对于搭建个人博客来说,有点儿太重量级了,很多功能都用不上。

搭建个人博客,需要的功能比较简单:

  • 支持MarkDown撰写文章;
  • 支持评论;
  • 支持后台管理;
  • 支持上传图片附件;
  • 支持代码高亮;

在支持上述功能的前提下,越轻量越好,所以选择了typecho,typecho虽然很简单,但是完全够用,而且一点儿也没有多余的功能。
下面简述了使用typecho搭建个人博客的过程。

服务器、域名相关

  • 服务器:vps
  • 操作系统:Debian 11
  • 域名:liuguangxuan.org

此处假设读者熟悉常见的Linux命令,并拥有root用户权限,配置好了域名的dns,解析到该服务器对应的IP。

安装SQLite

apt install sqlite3

安装PHP

安装PHP:

apt install php php7.3-fpm php7.3-gd php7.3-mbstring php7.3-xml php7.3-sqlite3

运行PHP:

systemctl start php7.3-fpm
systemctl status php7.3-fpm
php -v

安装nginx

apt install nginx

nginx的配置文件在/etc/nginx/conf.d目录下,在该目录下新建blog.conf文件,并填入如下内容:

server {
    listen          80;
    server_name     liuguangxuan.org www.liuguangxuan.org;
    root            /var/www/liuguangxuan.org;
    index           index.html index.htm index.php;

    if (!-e $request_filename) {
        rewrite ^(.*)$ /index.php$1 last;
    }

    location ~ .*\.php(\/.*)*$ {
        include fastcgi.conf;
        fastcgi_pass  unix:/run/php/php7.3-fpm.sock;
    }

    access_log liuguangxuan.org.log combined;
}

注意将上面的server_nameaccess_log替换成自己的。

安装typecho

/var/www目录下新建liuguangxuan.org目录:

cd /var/www
mkdir liuguangxuan.org
cd liuguangxuan.org

下载typecho安装包,并解压:

wget https://github.com/typecho/typecho/releases/latest/download/typecho.zip
unzip typecho.zip
rm -rf typecho.zip

修改文件夹权限

cd /var/www
chown -R www-data:www-data liuguangxuan.org

重启nginx

nginx -t
systemctl stop nginx
systemctl start nginx
systemctl status nginx

在浏览器中输入域名liuguangxuan.org访问地址,并进行设置。

如果有任何步骤提示错误,请仔细检查上面的步骤是否正确。

设置、插件

摘要

在后台,使用markdown写完文章并发布后,发现博客首页把所有的文章默认展开了,无法大致的预览所有的文章摘要。

需要进行两步设置才能看摘要。

  1. 后台->设置->阅读->聚合全文输出,选择仅输出摘要。
  2. 在写文章的时候,在摘要和后续的段落中间,插入<!--more-->

这样打开博客首页的时候,就会显示摘要了。

归档

经过上面的设置,可以显示摘要,但是还是无法查到所有的文章,需要下载一个归档插件JustArchives

cd /var/www/liuguangxuan.org/usr/plugins
wget https://plugins.typecho.me/usr/uploads/2012/03/2017455765.zip
unzip 2017455765.zip

然后进入博客后台,控制台->插件->启用JustArchives插件。

依次点击管理->独立页面->新增,标题中填入归档,内容填入<justarchives>,页面顺序填入-1,发布页面即可在首页导航栏看到归档按钮。

归档页面的内容及样式自己均可以调整,代码就在刚刚解压的文件中。

代码高亮

代码高亮选用CodeHighlighter插件,也可以显示行号。

cd /var/www/liuguangxuan.org/usr/plugins
wget https://plugins.typecho.me/usr/uploads/2018/05/45974882.zip
unzip 45974882.zip
mv CodeHighlighter-for-Typecho-master CodeHighlighter

即可进入博客后台设置插件的样式。

备份、恢复

因为vps随时有可能挂掉,所以将博客数据备份至关重要,此处选择将博客数据备份至GitHub私有仓库中。

  1. 新建GitHub私有仓库

登录到GitHub,点击右上角的加号,选择”New Repository”,输入仓库的名称,将仓库选择为Private,其他选择默认,点击下方的创建按钮。

  1. 配置ssh

    配置用户名和邮箱:

git config --global user.name "guangxuan"
git config --global user.email guangxuanliu@gmail.com

使用远程终端连接工具,ssh登录到wordpress网站所在的服务器上,注意切换到root用户,输入”ssh-keygen”命令,一路回车,在~/.ssh目录下生成id_rsa.pub,使用cat id_rsa.pub显示文件内容。

在Github中,点击右上角个人头像,点击设置,进入”SSH and GPG Keys”设置,点击”New SSH key”,Title部分随便填,最好自己能记住,看一眼就知道是那台计算机,Key部分粘贴进去上一步id_pub.rsa显示的内容。 切换到备份所在的文件夹:/var/www/liuguangxuan.org,依次执行如下命令:

git init
git add .
git commit -m "update"
git remote add origin xxx
git branch -M main
git push -u origin main

注意:上面的xxx为自己的git仓库地址,注意替换成自己的

  1. 编写自动提交脚本 脚本内容如下:
echo "push_git.sh start..."
push()
{
  current_date=`date +%F-%T`
  cd $1
  git add .
  git commit -m "$current_date"
  git push -u origin main
}

project_array=(/var/www/liuguangxuan.org)
for data in ${project_array[@]}
do
  echo "project name:" +${data}
  push ${data}
done
echo "push_git.sh done..."

将脚本保存在指定的位置,如/root/script/push_git.sh,并赋予可执行权限:

chmod a+x /root/script/git_push.sh
  1. 添加定时任务 输入crontab -e,在弹出的编辑窗口中输入:
0 3 * * * /root/script/push_git.sh >> /root/script/push_git.log 2>&1

代表每日的凌晨3点执行push_git.sh命令,并且将日志保存到push_git.log中。

  1. 登录到GitHub网页端,进行验证即可。

配置SSL

  1. 安装certbot

     apt-get install certbot
     apt-get install python-certbot-nginx
  2. 获取证书

     certbot --nginx -d liuguangxuan.org -d www.liuguangxuan.org
     # 按照提示,分别输入自己的电子邮件、同意服务条款、不同意收集信息等
  3. 自动更新

     # 每个月的1号,自动续订
     crontab -e
     0 0 1 * * /usr/bin/certbot renew --quiet

参考:

  1. https://naiv.fun/Ops/lsnpt.html

标签: none

添加新评论