787 字
4 分钟
博客迁移

白嫖的阿里云服务器今年七月份就要到期了,又白嫖了两年的腾讯云,于是想着把自己的博客迁移到腾讯云上。

之前在阿里云上部署hexo博客的时候什么都不懂,是一篇篇教程试出来的。这次在部署写一份详细一点的教程记录一下部署过程。

原理#

我们写博客用的是markdown格式,使用nodejs管理hexo,通过hexo渲染成html格式的静态网页,然后通过git将静态网页推送到linux服务器上,并用nginx驱动网页,然后就可以通过IP地址访问我们的博客啦,如果需要的话可以做一下域名解析。

需要的部件#

客户端#

Terminal window
$ node -v
v13.14.0
$ hexo version
hexo: 5.4.0
hexo-cli: 4.2.0

服务器端#

Terminal window
$ nginx -v
nginx version: nginx/1.14.0

配置#

客户端#

hexo博客的__config.yml中添加以下内容:

deploy:
type: git
repo: 'yourUsername@yourIP:yourRepoPath'
branch: master
message: '爷爷奶奶你关注的博主更新啦!'

服务器端#

创建git用户#

git用户专门用于管理hexo博客。

Terminal window
$ useradd -m git # -m 参数可以为git用户创建一个/home/git 文件夹

创建git私有repo#

用于与客户端连接。

Terminal window
$ git init --bare hexo_blog # hexo_blog 可以替换成任何名称,但是注意⚠️__config.yml文件中的yourRepoPath

创建post-receive#

⚠️该文件用于在上传时更新文件。

Terminal window
$ mkdir /home/git/hexo_blog/hooks/post-receive

写入如下内容:

Terminal window
#!/bin/sh
git --work-tree=/usr/share/nginx/html/blog --git-dir=/home/git/hexo_blog checkout -f
# /usr/share/nginx/html/blog 可以修改为你想要存放静态网页的文件夹
# /home/git/hexo_blog 注意要和你的上面👆的文件名同步

post-receive添加执行权限,否则在执行hexo d指令时,post-receive无法执行。

另外需要将--work-tree文件的拥有者修改为git

Terminal window
$ chmod +x /home/git/hexo_blog/hooks/post-receive
$ chown -R git:git /usr/share/nginx/html/blog

修改nginx配置信息#

修改/etc/nginx/nginx.conf

Terminal window
$ sudo vim /etc/nginx/nginx.conf

写入如下内容:

# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name yourDomainName;
# 修改为你对应的文件名
root /usr/share/nginx/html/blog;
return 301 https://$server_name$request_uri;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
} # Settings for a TLS enabled server.
#
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name yourDomainName;
# 修改为你对应的文件名
root /usr/share/nginx/html/blog;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}

验证nginx.conf配置是否有误:

Terminal window
$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

启动nginx服务#

Terminal window
$ sudo service nginx start
$ sudo service nginx restart # 重启命令

推送博客#

在客户端的hexo文件夹内:

Terminal window
$ hexo clean && hexo g && hexo d
# 静态网页文件生成之后,输入服务器密码

然后在yourIP:80即可访问你的博客啦。

博客迁移
https://dicer-zz.github.io/posts/博客迁移/
作者
Dicer
发布于
2021-04-25
许可协议
CC BY-NC 4.0