使用Git管理GitBook

对照着博文搭建Gitbook并通过Git推送部署,在自己服务器上操作一遍,本文根据自己的情况作了记录。

1. 使用Git管理GitBook

我使用的是阿里云ECS,操作系统装的是Ubuntu 20.04。

root@ubuntu:~# uname -a 
Linux ubuntu 5.4.0-92-generic #103-Ubuntu SMP Fri Nov 26 16:13:00 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

(1)安装git

运行apt-get install git遇到问题E: Package 'git' has no installation candidate

# apt-get install git
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Package git is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source

E: Package 'git' has no installation candidate

运行命令apt-get update更新下源(package lists),再安装。

# apt-get update
# apt-get install git

(2)创建git用户并赋予root权限

创建git用户,按提示输入密码

# adduser git
Adding user `git' ...
Adding new group `git' (1001) ...
Adding new user `git' (1001) with group `git' ...
Creating home directory `/home/git' ...
Copying files from `/etc/skel' ...
New password: 
Retype new password: 
passwd: password updated successfully
Changing the user information for git
Enter the new value, or press ENTER for the default
        Full Name []: 
        Room Number []: 
        Work Phone []: 
        Home Phone []: 
        Other []: 
Is the information correct? [Y/n] Y

修改/etc/sudoers文件,赋予git权限

使用Workbench远程连接,用vim打开/etc/sudoers文件,编辑后按ESC没反应。于是想着用Windows PowerShell终端登录,但遇到如下问题:

> ssh root@120.79.30.6
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the ECDSA key sent by the remote host is
SHA256:wwpJNhzKxhhyNLEOzT8uw9IBGKQwXZEUyKImmgljdZk.
Please contact your system administrator.
Add correct host key in C:\\Users\\SU/.ssh/known_hosts to get rid of this message.
Offending ECDSA key in C:\\Users\\SU/.ssh/known_hosts:9
ECDSA host key for 120.79.30.6 has changed and you have requested strict checking.
Host key verification failed.

解决方法,使用命令ssh-keygen删除无效的key,

> ssh-keygen -R 120.79.30.6
# Host 120.79.30.6 found: line 9
C:\Users\SU/.ssh/known_hosts updated.
Original contents retained as C:\Users\SU/.ssh/known_hosts.old

OK,现在可以通过SSH登录服务器了。

运行命令sudo vim /etc/sudoers,在sudoers文件添加一行git ALL=(ALL:ALL) ALL

# User privilege specification
root    ALL=(ALL:ALL) ALL
git     ALL=(ALL:ALL) ALL

现在git用户拥有root权限了,为了安全起见,禁止其Shell登录,将其bash改为/sbin/nologin,如下:

git:x:1001:1001:,,,:/home/git:/bin/bash

# 改为

git:x:1001:1001:,,,:/home/git:/sbin/nologin

这样的话,用git账户登录,将会提示This account is currently not available.

(3)初始化git仓库

# 创建仓库目录
root@ubuntu:~# cd /home/git
root@ubuntu:/home/git# mkdir gitbook.git
root@ubuntu:/home/git# cd gitbook.git/

# 初始化仓库,--bare将仓库初始为裸仓库,这样创建的仓库不包含工作区
root@ubuntu:/home/git/gitbook.git# git init --bare
Initialized empty Git repository in /home/git/gitbook.git/

(4)配置ssh

本地机器生成公钥。在/c/Users/SU目录下运行ssh-keygen -t rsa,提示输入文件名和passphrase,直接回车就行,生成的公钥存储在id_rsa.pub

$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/SU/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/SU/.ssh/id_rsa
Your public key has been saved in /c/Users/SU/.ssh/id_rsa.pub
...
| ..              |
|o.               |
+----[SHA256]-----+

将本地公钥上传到服务器。将本地的.ssh/id_rsa.pub内容拷贝到/home/git/.ssh/authorized_keys文件。

root@ubuntu:~# cd /home/git/
root@ubuntu:/home/git# mkdir .ssh

上传本地id_rsa.pub到服务器authorized_keys文件。

$ scp .ssh/id_rsa.pub root@dianyao.co:/home/git/.ssh/authorized_keys
root@dianyao.co's password:
id_rsa.pub                                                                                                                                        100%  566    11.7KB/s   00:00

(5)修改仓库的权限

gitbook.git用户和组设为git。 bash

sudo chown git:git -R /home/git/

root@ubuntu:/home/git# ls -ls
total 4
4 drwxr-xr-x 7 git git 4096 Feb  7 22:07 gitbook.git

(6)拷贝到本地

运行git clone遇到错误fatal: protocol error: bad line length character: This

$ git clone ssh://git@dianyao.co/home/git/gitbook.git
Cloning into 'gitbook'...
fatal: protocol error: bad line length character: This

原因是git用户已经被禁止Shell登录了。

git:x:1001:1001:,,,:/home/git:/sbin/nologin

解决方法,将git用户bash改为/bin/git-shell

git:x:1001:1001:,,,:/home/git:/bin/git-shell

现在可以了。

$ git clone ssh://git@dianyao.co/home/git/gitbook.git
Cloning into 'gitbook'...
warning: You appear to have cloned an empty repository.

存储用户名和密码:

git config --global credential.helper store

2. 创建站点

(1)安装宝塔运维管理面板

教程见宝塔Linux面板安装教程

进入到阿里云控制台,放行8888端口:进入控制台 --> 云服务器ECS --> 安全组 --> 配置规则 --> 手动添加,端口范围为8888/8888,授权对象为0.0.0.0/0

SSH登录服务器,运行如下命令安装宝塔面板:

wget -O install.sh http://download.bt.cn/install/install-ubuntu_6.0.sh && sudo bash install.sh

安装结束后,会给出面板访问地址、用户名和密码。

如果你也打算用宝塔面板,通过下列链接,我可以获得佣金返利。

(2)安装Nginx

在宝塔面板中的软件商店,找到Nginx,点击安装。

(3)创建站点

在宝塔面板中,网站 --> 添加站点,输入域名,设置备注和根域名,点击提交。

www.dianyao.co    80    
dianyao.co        80

放行80端口,进入到阿里云控制台:进入控制台 --> 云服务器ECS --> 安全组 --> 配置规则 --> 手动添加,端口范围为80,授权对象为0.0.0.0/0

(4)修改网站根目录的用户组权限

sudo chown git:git -R /www/wwwroot/dianyao.co

3. 自动部署

在本地编辑Markdown文章,使用Git提交到云服务器Git仓库。Git仓库收到推送,使用Git Hooks,执行post-receive将GitBook编译好的_book目录拷贝到网站目录。

(1)设置将_book目录拷贝到网站目录

/home/git/gitbook.git/hooks/目录下创建文件post-receive,写一小段Python脚本(用命令apt-get install python安装Python),将_book目录拷贝到网站目录,源码如下:

#!/bin/env python

import os

def main():
    # Step 1: git clone to /tmp
    git_repo = '/home/git/gitbook.git'
    tmp_git_repo = '/tmp/gitbook'
    if os.path.exists(tmp_git_repo):
        os.system('rm -rf {}'.format(tmp_git_repo))

    os.system('git clone {} {}'.format(git_repo, tmp_git_repo))

    # Step 2: copy _build to wwwroot
    www_source = '/tmp/gitbook/gitbook-notes/_book'
    www_target = '/www/wwwroot/dianyao.co/gitbook-notes'
    if os.path.exists(www_target):
        os.system('rm -rf {}'.format(www_target))

    os.system('cp -rf {} {}'.format(www_source, www_target))

post-receive加上可执行的权限。

chmod +x post-receive

(2)使用Git将GitBook提交到云服务器Git仓库

写了一小段Python脚本,将GitBook提交到云服务器Git仓库:

import os

def main():
    commit_message = "gitbook-notes first submit"
    dir_gitbook = "gitbook-notes"

    # Change the current working directory
    os.chdir('gitbook')

    # Build gitbook
    os.system('gitbook build {}'.format(dir_gitbook))

    # 解决使用git add命令时报错LF will be replaced by CRLF的问题
    os.system('git config auto.crlf true')

    # 保存所有的修改
    os.system('git add -A')

    # 把修改的文件提交
    os.system('git commit -m "{}"'.format(commit_message))

    # 强制推送到远程仓库
    os.system('git push -f origin master')

执行上述代码,将gitbook提交到服务器,Git收到提交,执行(1)中的shell脚本,将_book目录拷贝到网站目录。

(3)遇到问题及解决

执行上述代码遇到Author identity unknown问题:

Author identity unknown

*** Please tell me who you are.

Run

  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"

按提示,运行如下命令:

git config --global user.email "contact@dianyao.co"
git config --global user.name "git"

命令git config --list可以输出刚才设置的名字和邮件。

参考资料:

[1] 搭建Gitbook并通过Git推送部署 | Eulaの小破站

本文系Spark & Shine原创,转载需注明出处本文最近一次修改时间 2022-02-27 16:11

results matching ""

    No results matching ""