目录

「Blog」从建站到放弃

记录建站过程。

WEB环境

LNMP配置

1. 下载并安装 LNMP
# Debian 12 (5.5.62+@MySQL;yes@InnoDB; 7.4.33@PHP;)
# 使用 `nginx -V` 检查并确认如下模块已安装,可参考`https://lnmp.org/faq/lnmp1-2-upgrade.html`
# --with-stream_ssl_preread_module
# --with-http_realip_module
# (lnmp2.1 均已安装)
# 1. cd lnmpx.x
# 2. vim lnmp.conf
# 3. Nginx_Modules_Options后补充需要的模块
# 4. ./upgrade.sh nginx
# 5. 选择同版本
# Debian 11 (5.5.62+@MySQL;yes@InnoDB; 7.4.30@PHP;)
# 需确保如下模块已经安装,可参考`https://lnmp.org/faq/lnmp1-2-upgrade.html`
# --with-http_realip_module
# 1. cd lnmpx.x
# 2. vim lnmp.conf
# 3. Nginx_Modules_Options后补充需要的模块
# 4. ./upgrade.sh nginx
# 5. 选择同版本
wget http://soft.vpser.net/lnmp/lnmp1.9.tar.gz -cO lnmp1.9.tar.gz && tar zxf lnmp1.9.tar.gz && cd lnmp1.9 && ./install.sh lnmp
# Debian 11 (5.5.62+@MySQL;yes@InnoDB; 7.4.21@PHP;)
# 需确保如下模块已经安装,可参考`https://lnmp.org/faq/lnmp1-2-upgrade.html`
# --with-stream_ssl_preread_module
# --with-http_realip_module
# 1. cd lnmpx.x
# 2. vim lnmp.conf
# 3. Nginx_Modules_Options后补充需要的模块
# 4. ./upgrade.sh nginx
# 5. 选择同版本
wget http://soft.vpser.net/lnmp/lnmp1.8.tar.gz -cO lnmp1.8.tar.gz && tar zxf lnmp1.8.tar.gz && cd lnmp1.8 && ./install.sh lnmp
# Debian 10 (PHP7.1+)
wget http://soft.vpser.net/lnmp/lnmp1.6.tar.gz -cO lnmp1.6.tar.gz && tar zxf lnmp1.6.tar.gz && cd lnmp1.6 && ./install.sh lnmp
# Centos 6
wget -c http://soft.vpser.net/lnmp/lnmp1.4.tar.gz && tar zxf lnmp1.4.tar.gz && cd lnmp1.4 && ./install.sh lnmp
2. 添加虚拟主机

Debian 10/11/12 PASSED

lnmp vhost add

CentOS 6 PASSED

# 升级python到2.7,否则证书申请失败
  # 下载
wget https://www.python.org/ftp/python/2.7.12/Python-2.7.12.tgz
  # 解压
tar -zxvf Python-2.7.12.tgz
  # 定位
cd Python-2.7.12
  # 编译安装
./configure  
make all
make install
make clean
make distclean
  # 查看版本
/usr/local/bin/python2.7 -V  
  # 建立软连接,使系统默认的 python指向 python2.7
mv /usr/bin/python /usr/bin/python2.6.6
ln -s /usr/local/bin/python2.7 /usr/bin/python
  # 查看版本
python -V
  # 解决系统 Python 软链接指向 Python2.7 版本后,因为yum是不兼容 Python 2.7的,所以yum不能正常工作,需要指定 yum 的Python版本,修改`#!/usr/bin/python`到`#!/usr/bin/python2.6.6`
vim /usr/bin/yum
  # 安装setuptools,以便安装virtualenv(下面注释命令失败时使用,Vultr优先采用)
wget --no-check-certificate https://pypi.python.org/packages/source/s/setuptools/setuptools-12.0.3.tar.gz#md5=f07e4b0f4c1c9368fcd980d888b29a65
tar xvf setuptools-12.0.3.tar.gz && cd setuptools-12.0.3 && python setup.py install
  # 安装pip,以便安装virtualenv
wget "https://pypi.python.org/packages/source/p/pip/pip-1.5.4.tar.gz#md5=834b2904f92d46aaa333267fb1c922bb" --no-check-certificate
tar -xzvf pip-1.5.4.tar.gz && cd pip-1.5.4 && python setup.py install
  # 验证pip正确安装
pip -V
  # 如验证不成功
vim /etc/profile
文档最后添加`export PATH="/usr/local/bin/python2.7/bin:$PATH"`
source /etc/profile
  # 安装virtualenv,否则证书申请失败
  # # yum install python-setuptools
  # # easy_install virtualenv
pip install virtualenv
# 添加主机
lnmp vhost add
3. 修改 Nginx 主机配置文件
  1. 修改读写权限

    # 解决权限问题
    chattr -i /home/wwwroot/yirami.xyz/.user.ini
    # 修改权限,755应该不行
    chmod -R 777 /home/wwwroot/yirami.xyz
  2. 修改 pathinfo,否则出现 404 【1.4版安装脚本需要该步】

    1. 修改对应虚拟主机的配置文件/usr/local/nginx/conf/vhost/yirami.xyz.conf,将include enable-php.conf;替换为include enable-php-pathinfo.conf; 【共 2 处,分别在 http 和 https 】
  3. 强制开启https

    # 编辑文件
    vim /usr/local/nginx/conf/vhost/yirami.xyz.conf
    # http 部分,新增如下
      if ($scheme = http ) {
          return 301 https://$host$request_uri;
      }
     或:
      if ($server_port = 80 ) {
          return 301 https://$host$request_uri;
      }
  4. 地址重写,忽略 Typecho 开启地址重写时的警告

    # 编辑文件
    vim /usr/local/nginx/conf/vhost/yirami.xyz.conf
    # https 部分,添加内容
      if (-f $request_filename/index.html){
          rewrite (.*) $1/index.html break;
      }
      if (-f $request_filename/index.php){
          rewrite (.*) $1/index.php;
      }
      if (!-f $request_filename){
          rewrite (.*) /index.php;
      }
  5. 重启 Nginx,修改 nginx 配置都得重启

    service nginx restart
    
    or
    
    /etc/init.d/nginx restart

Git配置

# 查看版本
git --version
# 初始化用户
git config --global user.name "yirami"
git config --global user.email "yirami@qq.com"
# 创建SSH
ssh-keygen -t rsa -C "yirami@qq.com"
# 去Git网站添加公钥

网站恢复

1. 备份原网站目录
  1. 主要是其下的.user.ini
2. 从远程端恢复网站
git clone git@gitee.com:yirami/yirami.xyz.git
3. 拷入备份的.user.ini
4. 恢复网站数据库,并测试
  1. 注意赋予文件及文件夹对应权限,尤其是数据库文件(对于Typecho的sqlite数据库)
  2. 创建数据库表(对于Typecho的Mysql数据库)
# 5.5.62
#set global innodb_file_format = BARRACUDA;
#set global innodb_large_prefix = ON;
# use this but why!!!
set global innodb_file_format = Antelope;
set global innodb_large_prefix = OFF;

create database typecho default character set utf8mb4 collate utf8mb4_unicode_ci;
create user 'yirami'@'%' identified by 'root';
grant all on root.* to 'yirami'@'%';
flush privileges;
  1. 导出数据库
mysql -u root -p
SHOW DATABASES;
exit
mysqldump -u root -p typecho > /root/yirami/miss.yirami.xyz.sql
  1. 导入数据库
mysql -u root -p
create database typecho default character set utf8mb4 collate utf8mb4_unicode_ci;
use typecho;
source /root/yirami/miss.yirami.xyz.sql
show tables;
exit
# mysqldump -u root -p typecho < /root/yirami/miss.yirami.xyz.sql  # NOT WORK!

Typecho

构建

维护

开启MathJax支持

参考《前端整合MathjaxJS的配置笔记》

「通用」法一

修改主题文件,在底部footer之后、body之前,插入如下JS代码

  • 版本一:可定制避开公式识别的类标签

MathJax V2

<script type="text/x-mathjax-config">
MathJax.Hub.Config({
    showProcessingMessages: false, // 关闭js加载过程信息
    messageStyle: "none", // 不显示信息
    extensions: ["tex2jax.js"],
    jax: ["input/TeX", "output/HTML-CSS"],
    tex2jax: {
        inlineMath: [ ['$','$'], ["\\(","\\)"] ], // 行内公式选择符
        displayMath: [ ['$$','$$'], ["\\[","\\]"] ], // 段内公式选择符
        skipTags: ['script', 'noscript', 'style', 'textarea', 'pre','code','a'], // 避开某些标签
        ignoreClass:"comment-content" // 避开含该Class的标签
    },
    "HTML-CSS": {
        availableFonts: ["STIX","TeX"], // 可选字体
        showMathMenu: false // 关闭右击菜单显示
    }
});
MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
</script>
<script src="//cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>

MathJax V3 注:V2 到 V3 的配置转换可以使用这里提供的在线工具

<script>
window.MathJax = {
  tex: {
    inlineMath: [ ['$','$'], ["\\(","\\)"] ],   // 行内公式选择符
    displayMath: [ ['$$','$$'], ["\\[","\\]"] ],   // 段内公式选择符
    autoload: {
      color: [],
      colorv2: ['color']
    },
    packages: {'[+]': ['noerrors']}
  },
  options: {
    skipHtmlTags: ['script', 'noscript', 'style', 'textarea', 'pre','code','a'],   // 避开某些标签
    ignoreHtmlClass: "comment-content",   // 避开含该Class的标签
    processHtmlClass: 'tex2jax_process'
  },
  loader: {
    load: ['[tex]/noerrors']
  }
};
</script>
<script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js" id="MathJax-script"></script>
  • 版本二:可额外定制公式识别范围(如排除评论区)
<script type="text/x-mathjax-config">
var mathId = document.getElementById("post-content"); // 选择公式识别范围
MathJax.Hub.Config({
    showProcessingMessages: false, // 关闭js加载过程信息
    messageStyle: "none", // 不显示信息
    extensions: ["tex2jax.js"],
    jax: ["input/TeX", "output/HTML-CSS"],
    tex2jax: {
        inlineMath: [ ['$','$'], ["\\(","\\)"] ], // 行内公式选择符
        displayMath: [ ['$$','$$'], ["\\[","\\]"] ], // 段内公式选择符
        skipTags: ['script', 'noscript', 'style', 'textarea', 'pre','code','a'] // 避开某些标签
    },
    "HTML-CSS": {
        availableFonts: ["STIX","TeX"], // 可选字体
        showMathMenu: false // 关闭右击菜单显示
    }
});
MathJax.Hub.Queue(["Typeset",MathJax.Hub,mathId]);
</script>
<script src="//cdn.bootcss.com/mathjax/2.7.0/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
「Initial主题」法二|推荐
  1. 进入网站管理后台
  2. 设置外观
  3. 在当前主题(Initial)下进入设置外观标签页
  4. 滑动页面到最下方的底部自定义内容字段,并结合上述法一中的任意配置填入。
显示受保护文章的标题
  1. 打开Typecho工程下var/Widget/Abstract/Contents.php文件;
  2. 第744行(当前为1.1 (17.10.30),不同版本可能稍有区别),用//注释掉$value['title'] = _t('此内容被密码保护');即可;

评论

清空垃圾评论
# install sqlite3
sudo apt update
sudo apt install -y sqlite3
# connect database
sqlite3 xxx.db
pragma key=******;
.tables  # show table
.schema  # show schema
SELECT COUNT(*) FROM typecho_comments WHERE length(text) > 30;
DELETE FROM typecho_comments WHERE length(text) > 30;
.quit
# set read-write permission
chmod 777 xxx.db

Hexo

构建

  1. 根据官方文档安装 nodejs 和 hexo 环境

常用命令

更多命令见官方文档

# 在 folder 目录新建网站
hexo init [folder]
# 新建文章,含空格的标题需要用双引号。常见布局有`post`、`page`、`draft`
hexo new [layout] <title>
# 生成静态文件
hexo generate | hexo g
# 发布草稿
hexo publish [layout] <filename>
# 启动服务器,服务默认启动在`http://localhost:4000/`
hexo server -p 4000 | hexo s
# 部署网站
hexo deploy | hexo d
# 渲染文件
hexo render <file1> [file2] ...
# 从其它博客系统迁移
hexo migrate <type>
# 清除缓存(db.json)和生成的静态文件(./public/*)
hexo clean
# 列出信息
hexo list <type>
# 版本信息
hexo version

自动化部署

Git Hook

思路是本地push到服务器,服务器通过git hook在接收完更新后拷贝文件到网站目录

adduser git  # passwd:=git
su git
chown -R git /home/wwwroot/review.yirami.xyz
mkdir ~/.ssh
nano ~/.ssh/authorized_keys  # 此步将本机git公钥拷贝进该文件
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
git:x:1001:1001:deploy.git,,,:/home/git:/bin/bash => git:x:1001:1001:,,,:/home/git:/usr/bin/git-shell  # 安全考虑,禁止git通过shell登录
service ssh restart  # root下重启ssh
cd ~
mkdir review.git
cd review.git
git init --bare
nano hook/post-receive  # 新建文件,并插入命令`git --work-tree=/home/wwwroot/review.yirami.xyz --git-dir=/home/git/review.git checkout -f`
chmod +x hook/post-receive 
git remote add deploy ssh://git@yirami.xyz:32722/home/git/review.git  # 将本远程地址放到hexo部署部分

参考