从Ghost转移到Typecho,不是说Ghost不好,主要是因为瞎捣鼓数据也没了。就重新弄一下,就选择更轻量的Typecho。

没找到直接转移数据的插件,文字就自己复制粘贴了一下。不过图片都丢失了。

还是备案信息的问题,搜索了一下,参考这位博主的方法,非常方便:

Typecho底部添加ICP备案号

主要内容:
一:进入“外观”——“编辑当前外观”

1.编辑function.php文档,在function themeConfig下添加可修改变量代码
$bei = new Typecho_Widget_Helper_Form_Element_Text('bei', NULL, NULL, _t('工信部备案号'), _t('留空则不显示'));
$form->addInput($bei->addRule('xssCheck', _t('工信部备案号不能使用特殊字符')));

2.编辑footer.php文档,在footer元素下添加显示代码
<div class="blog-text-center"><a href="https://beian.miit.gov.cn/" target="_blank" rel="nofollow"><?php $this->options->bei(); ?></a></div>

二:进入“外观”——“设置外观”
即可添加ICP备案号

// 捣鼓VPS把数据都整没了。只剩下文字,当做是简单的记录吧。

  1. 群晖安装Docker

直接从套件中心安装即可,有疑问可以直接搜索相关资料。

  1. 编辑Docker Composer文件

参考官方文档:https://docs.teslamate.org/docs/installation/docker

请注意,以下文件需要按实际修改,入秘钥,密码,IP地址;

version: "3"

services:
  teslamate:
    image: teslamate/teslamate:latest
    restart: always
    environment:
      - ENCRYPTION_KEY=#填写加密字符串,请自行生成,切勿加入空格
      - DATABASE_USER=teslamate
      - DATABASE_PASS=#填写字符串,请自行生成,切勿加入空格
      - DATABASE_NAME=teslamate
      - DATABASE_HOST=database
      - MQTT_HOST=mosquitto
      - VIRTUAL_HOST=192.168.xx.xx #此处为NAS地址
      - TZ=CN #Timezone
    ports:
      - 4000:4000
    volumes:
      - ./import:/opt/app/import
    cap_drop:
      - all

  database:
    image: postgres:14
    restart: always
    environment:
      - POSTGRES_USER=teslamate
      - POSTGRES_PASSWORD=#填写字符串,请自行生成,切勿加入空格
      - POSTGRES_DB=teslamate
    volumes:
      - teslamate-db:/var/lib/postgresql/data

  grafana:
    image: teslamate/grafana:latest
    restart: always
    environment:
      - DATABASE_USER=teslamate
      - DATABASE_PASS=#填写字符串,请自行生成,切勿加入空格
      - DATABASE_NAME=teslamate
      - DATABASE_HOST=database
    ports:
      - 3000:3000
    volumes:
      - teslamate-grafana-data:/var/lib/grafana

  mosquitto:
    image: eclipse-mosquitto:2
    restart: always
    command: mosquitto -c /mosquitto-no-auth.conf
    # ports:
    #   - 1883:1883
    volumes:
      - mosquitto-conf:/mosquitto/config
      - mosquitto-data:/mosquitto/data

volumes:
  teslamate-db:
  teslamate-grafana-data:
  mosquitto-conf:
  mosquitto-data:

把配置文件置于docker文件夹中,比如我放在docker/teslamate中

并且在同级目录下创建import文件夹

  1. 通过composer安装

首先确定已经开启群晖SSH

通过电脑终端ssh访问,不了解的请自行搜索“ssh访问群晖”

ssh admin@192.168.xx.xx
#进入之后切换到root
sudo su #密码与你登录群晖相同

切换到docker配置的目录

cd /volume1/docker/teslamate/

执行命令安装

docker-compose up -d

等待命令执行完毕,如无意外(如果你遇到可能要请教google),你会在Docker里面看到这4个容器。

  1. 授权使用

浏览器中输入

192.168.xx.xx:4000 (群晖地址+端口)即可访问TeslaMate;

此令牌需要通过工具获取:https://github.com/adriankumpf/tesla_auth

192.168.xx.xx:3000 为Grafana;

默认username/password: admin/admin

我当前使用的是Theme是一个简约风格的主题 – dawn

因为最近更换了主题没留意备案号没有写上,导致腾讯云电话和邮件通知要修复,不然网站就不能访问。

之前一直都是直接改主题文件,把备案号写主题源码里。但是这样的话很麻烦,包括主题更新或者更换都会直接影响到。

在官方文档查到Code Injection这个功能是可以直接插入执行JS代码的,所以在这里顺便做个记录,可供有同样需求的朋友食用。

// 图片已丢失

具体的执行代码需要根据你所用的主题来实际修改,比如我用的主题在footer部位有copyright的声明,我就直接在这后面插入备案信息,执行脚本如下:

<script>
// 获取copyright的DOM对象
var cr = document.getElementsByClassName('copyright');

// getElementsByClassName 获取到的对象是数组,默认直接取用首位即可
cr[0].insertAdjacentHTML('afterend', '<div style="color: var(--secondary-text-color); font-size: 1.2rem; letter-spacing: -.02rem;"><a style="color:var(--secondary-text-color);" target="_blank" href="https://beian.miit.gov.cn/">您的备案号</a></div>');
</script>

参考:

0x1 理论基础

  1. 以DNS的方式向Let's Encrypt申请校验码;
  2. 在域名解析中增加TXT记录,用于校验;
  3. 校验通过后生成证书;
  4. 自动renew问题的应对方法;

0x2 详细操作 (如果嫌麻烦请看0x3)

1. 申请校验码

certbot certonly -d *.domain.net --manual --preferred-challenges dns --server https://acme-v02.api.letsencrypt.org/directory\n

这个过程第一步会询问你是否同意记录你的IP

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NOTE: The IP of this machine will be publicly logged as having requested this
certificate. If you're running certbot in manual mode on a machine that is not
your server, please ensure you're okay with that.\n\nAre you OK with your IP being logged?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o:

Y, 进入下一步, 出现认证的校验码

Please deploy a DNS TXT record under the name
_acme-challenge.domain.net with the following value:

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Before continuing, verify the record is deployed.

在这里需要等待,不要按下回车键,复制那一长串校验码,进入到你域名解析的页面

2. 添加解析记录

添加记录

主机记录为_acme-challenge

记录类型为TXT

记录值为xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

如图

// .jpg fly.

保存之后要先通过测试,看DNS的解析是否已经生效

3. 查看解析记录是否生效

Linux中可用

watch -d -n 2 dig -t txt _acme-challenge.domain.net @223.5.5.5

macOS好像没有watch, 手动隔几秒执行一下好了。腾讯云的一般1分钟内就会解析生效;

dig -t txt _acme-challenge.domain.net @223.5.5.5

出现如下结果

Every 2.0s: dig -t txt _acme-challenge.domain.net @223.5.5.5                                       htpc: Thu Oct 10 22:06:46 2019


; <<>> DiG 9.11.3-1ubuntu1.9-Ubuntu <<>> -t txt _acme-challenge.domain.net @223.5.5.5
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 58041
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1452
;; QUESTION SECTION:
;_acme-challenge.domain.net.   IN      TXT

# 重点在这里,出现answer section显示刚刚添加的记录值就是已经正常了。
;; ANSWER SECTION:
_acme-challenge.domain.net. 564 IN   TXT   "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

;; Query time: 287 msec
;; SERVER: 223.5.5.5#53(223.5.5.5)
;; WHEN: Thu Oct 10 22:06:46 CST 2019
;; MSG SIZE  rcvd: 108

4. 继续执行申请证书

回到第一步中等待的终端,按下回车后,等待校验通过就会生成证书;如果出错可能是解析没成功就按了回车。

0x3 使用脚本申请证书和自动renew

我是通过申请证书之后发现renew并不正常,找解决方法的时候才发现这个脚本。

脚本地址:https://github.com/ywdblog/certbot-letencrypt-wildcardcertificates-alydns-au

这个脚本的说明已经很详细了,我就不复制过来了;

如果你打算使用这个脚本做renew,而对

Are you OK with your IP being logged?

这个询问有疑问,其实这个询问只会在申请证书的时候才会有,renew的时候是不会出现的。

所以这个脚本在自动renew的时候不会因为这个问题而卡住。

x-1

一般情况下Let's encrypt加上crontab自动renew证书就好了。但是前段时间,一直收到邮件说证书即将过期,今天抽空就来看看到底是什么情况。

x-2

在服务器上输入

certbot renew --dry-run

得到的结果是

Detail: remote error: tls: handshake failure

摸不着头脑,只能求助Google了。找到一位仁兄的解决方式
Renewing LetsEncrypt Certificate When Using Cloudflare

文章里面有提到因为Cloudflare做了反代,所以真实的IP与域名解析的IP不一致导致无法验证。
尝试了文中提到的解决方式,确实可以验证通过,并且更新了证书。

(原作者在文章最后提到如果有其它的解决方式,请留言,但是这位仁兄的Ghost并没有留言模块。希望他找到了更好的方法。)

不过由于不方便自动更新证书,找到了Cloudflare官方给出的解决方法
How to Validate a Let’s Encrypt Certificate on a Site Already Active on Cloudflare

这个方法相对来说更简单,并且可以自动更新证书。

简单总结一下

// 原文使用的是自己从github拉下来的letsencrypt脚步,我用的是certbot,不过是一样的。

certbot certonly --webroot --webroot-path /var/www/html/website --renew-by-default --email email@email.com --text --agree-tos -d domain.com -d www.domain.com

--webroot-path 网站根目录的路径
--renew-by-default 设置自动更新为默认
--email 用于注册和恢复的邮箱
--text 显示输出
--agree-tos 同意LE的协议
-d 对应的域名

正常的返回结果就会看到 #Congratulations!#

之后的renew直接用

cerbot renew

即可。用crontab定时运行更佳!