Hexo部署阿里云【Ubuntu22.04.1】

部署环境与准备

1.环境

本地 Windows10 操作系统

阿里云 Ubuntu22.04.1 服务器

2.准备
Hexo 本地博客

MobaXterm 链接到你的服务器

云服务器配置 Git

配置步骤

1.更新软件包列表
在安装 Git 之前,需要先更新 Ubuntu 软件包列表

1
sudo apt update

2.安装依赖库

1
sudo apt install dh-autoreconf libcurl4-gnutls-dev libexpat1-dev make gettext libz-dev libssl-dev libghc-zlib-dev

3.配置之前请确认是否安装ssh,如果没有安装可以在终端输入

1
sudo apt install openssh-server

4.查看 git 的版本

1
git version

5.删除 git

1
apt remove git -y

6.下载解压最新版,官方下载链接,找到对应版本下载即可

1
2
3
cd /usr/local/src #下载的目录
wget https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.41.0.tar.gz #下载最新版
tar -zxvf git-2.41.0.tar.gz #解压到当前文件夹

7.编辑并安装

1
2
3
cd git-2.41.0    #进入文件夹
make prefix=/usr/local/git all #编译源码
make prefix=/usr/local/git install #安装路径

8.查看版本号

1
git --version

9.创建 git 用户并且修改权限

1
2
3
4
5
6
7
8
adduser yulium
passwd yulium
chmod 740 /etc/sudoers
vim /etc/sudoers root

找到root ALL=(ALL) ALL,在它下方加入一行
root ALL=(ALL) ALL
yulium ALL=(ALL) ALL

10.本地 windows10 使用 Gitbash 创建密钥

1
ssh-keygen -t rsa

然后一直回车即可,别管要求输入啥就行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
User@yulium MINGW64 ~/Desktop
$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/User/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/User/.ssh/id_rsa
Your public key has been saved in /c/Users/User/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:EBUfJaetn8+5GIcal1e6zhcOdNiEkRRPzwj0ynGmcKA User@yulium
The key's randomart image is:
+---[RSA 3072]----+
| ..o.+o*+=. |
| . o B =o+.|
| . E + + O.o|
| . = O o |
| S . = . .|
| . = + |
| . B * .|
| + O +.|
| . ..Oo |
+----[SHA256]-----+

11.将本地创建 id_rsa.pub 中文件复制
切换到Ubuntn

1
2
3
su yulium
mkdir ~/.ssh
vim ~/.ssh/authorized_keys # 查看是否有authorized_keys文件,如果没有则用以下命令创建一个

12.本地测试

1
ssh -v yulium@服务器ip

出现 Welcome to Alibaba Cloud Elastic Compute Service !则代表部署成功

1
2
3
4
5
6
......
Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.


Welcome to Alibaba Cloud Elastic Compute Service !

云服务器网站配置

1.创建网站目录并且设置权限

1
2
3
su root
mkdir /home/hexo
chown yulium:yulium -R /home/hexo

2.安装 Nginx

1
2
apt install -y nginx
systemctl start nginx.service #启动服务

3.查看nginx状态

1
2
3
service nginx status
# 或者
sudo systemctl start nginx.service

4.停止nginx服务

1
2
3
service nginx stop
# 或者
sudo systemctl stop nginx.service

5.重启nginx服务

1
2
3
service nginx restart
# 或者
sudo systemctl restart nginx.service

6.修改 Nginx 配置文件

1
vim /etc/nginx/sites-enabled/default

原来的文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# https://www.nginx.com/resources/wiki/start/
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
# https://wiki.debian.org/Nginx/DirectoryStructure
#
# In most cases, administrators will remove this file from sites-enabled/ and
# leave it as reference inside of sites-available where it will continue to be
# updated by the nginx packaging team.
#
# This file will automatically load configuration files provided by other
# applications, such as Drupal or Wordpress. These applications will be made
# available underneath a path with that package name, such as /drupal8.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#
server {
listen 80 default_server;
listen [::]:80 default_server;

# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;

root /var/www/html;

# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;

server_name _;

location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}

# pass PHP scripts to FastCGI server
#
#location ~ \.php$ {
# include snippets/fastcgi-php.conf;
#
# # With php-fpm (or other unix sockets):
# fastcgi_pass unix:/run/php/php7.4-fpm.sock;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}


# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
# listen 80;
# listen [::]:80;
#
# server_name example.com;
#
# root /var/www/example.com;
# index index.html;
#
# location / {
# try_files $uri $uri/ =404;
# }
#}

修改后

1
2
root         /home/hexo;      #第41行修改, 网站目录
server_name xxxxx; #第46行修改, 域名或ip地址

7.重启服务器

1
2
3
service nginx restart
# 或者
sudo systemctl restart nginx.service

8.建立 git 仓库

1
2
3
4
su root
cd /home/yulium
git init --bare blog.git
chown yulium:yulium -R blog.git

9.同步网站根目录

1
2
3
4
5
vim blog.git/hooks/post-receive

#添加以下内容
#!/bin/sh
git --work-tree=/home/hexo --git-dir=/home/yulium/blog.git checkout -f

10.修改权限

1
chmod +x /home/yulium/blog.git/hooks/post-receive

11.在 windows10 本地 hexo 目录修改_config.yml 文件

1
2
3
4
deploy:
type: git
repository: yulium@xxxxx:/home/yulium/blog.git #用户名@服务器Ip:git仓库位置
branch: master

12.在本机 gitbash 部署

1
hexo cl && hexo g && hexo d

常见报错

  1. git-upload-pack: 未找到命令
    1
    2
    bash: git-upload-pack: command not found
    fatal: Could not read from remote repository.
    解决方法
    1
    sudo ln -s  /usr/local/git/bin/git-upload-pack  /usr/bin/git-upload-pack
  2. git-receive-pack: 未找到命令
    1
    2
    bash: git-receive-pack: command not found
    fatal: Could not read from remote repository.
    解决方法
    1
    sudo ln -s /usr/local/git/bin/git-receive-pack  /usr/bin/git-receive-pack
  3. 无法远程连接获取
    1
    fatal: Could not read from remote repository.
    解决方法
    1
    重试或者 删掉本地ssh公钥重新上传至服务器
  4. key 出错
    1
    Host key verification failed.
    解决方法
    1
    ssh-keygen -R 你要访问的IP地址
  5. 部署时找不到git
    1
    ERROR Deployer not found: git
    解决方法
    1
    2
    3
    npm install hexo-deployer-git --save

    然后在使用Hexo d命令就可以推送了