15

ubuntu上部署hexo博客

 3 years ago
source link: https://www.yangyanxing.com/article/hexo-deployment-in-ubuntu.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

之前这个个人博客一直挂在github pages或者coding pages下面,之前在百度云上买了一台云服务器,闲着也是闲着,就将个人博客转到这上面来吧。

本文从一个纯净的ubuntu系统开始,到最后的上线,主要涉及以下内容

  1. 云服务器上git环境的搭建
  2. 云服务器上nginx的设置
  3. 云服务器上https证书的设置
  4. 本地hexo提交到云服务器
  5. 云服务器git 钩子设置
  6. 设置图片防盗链

一步一步来,我买的是ubuntu 18.04 x64

一、云服务器上git环境的搭建

1.1 安装git

1
2
# apt-get update
# apt-get install git

1.2 初始化git仓库

1
2
3
4
5
6
root@instance-tgmmsl5q:~# mkdir myblog
root@instance-tgmmsl5q:~# chown -R $USER:$USER myblog/
root@instance-tgmmsl5q:~# chmod -R 755 myblog/
root@instance-tgmmsl5q:~# cd myblog/
root@instance-tgmmsl5q:~/myblog# git init --bare hexo_blog.git
Initialized empty Git repository in /root/myblog/hexo_blog.git/

这时我们就创建了一个空的git仓库

二、设置nginx环境

2.1 安装nginx

1
2
# apt-get update
# apt-get install nginx

2.2 设置nginx环境

我们将hexo的index目录设置为 /var/www/myblog

1
2
3
4
root@instance-tgmmsl5q:~/myblog# cd /var/www/
root@instance-tgmmsl5q:/var/www# mkdir myblog
root@instance-tgmmsl5q:/var/www# chown -R $USER:$USER myblog/
root@instance-tgmmsl5q:/var/www# chmod -R 755 myblog/

2.3 绑定域名

需要将www.yangyanxing.com 的域名指向/var/www/myblog

/etc/nginx/sites-available创建yangyanxing.com文件,写入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
server {
listen 80;
listen [::]:80;

server_name www.yangyanxing.com;

root /var/www/myblog;
index index.html;

location / {
try_files $uri $uri/ =404;
}

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
access_log off;
expires 1d;
}
location ~ \.(js|css) {
access_log off;
expires 1d;
}
}

然后创建一个链接文件 ln -s /etc/nginx/sites-available/yangyanxing.com /etc/nginx/sites-enabled/

重启nignx sytemctl restart nginx , 在/var/www/myblog 目录下写一个index.html文件,然后再访问一下www.yangyanxing.com 查看nginx域名设置是否生效,如果是你设置的index.html则说明生效了.

三、云服务器上https证书的设置

添加 certbot 源

1
add-apt-repository ppa:certbot/certbot

安装certbot

1
apt-get update && apt-get install python-certbot-nginx -y

生成证书,会有几个步骤

  1. 输入邮箱,随意填
  2. 是否同意许可,Y
  3. 是否分享你的电子邮件 N
  4. 选择哪个域名需要添加证书,这里输入列出的编号
  5. 是否将http强制跳转到https,我选是 2

执行完脚本以后,我们再浏览器中刷新一下,之前的http就自动跳转到了https则说明设置https成功。

Let’s Encrypt 签发的 SSL 证书有效期只有 90 天,所以在过期之前,需要自动更新 SSL 证书。最新版 certbot 会自动添加更新脚本到 /etc/cron.d 里,此脚本每隔 12 小时运行一次,并将续订任何在30天内到期的证书。输入以下命令检测自动续签是否工作:

certbot renew --dry-run

命令运行后如果没有报错,即说明自动续签可以正常工作。

四、本地hexo提交到云服务器

在第一步中初始化git仓库以后,服务器上的地址为/root/myblog/hexo_blog.git

我们是可以得到git的地址为你的服务器用户名@云服务器的IP地址:/root/myblog/hexo_blog.git

先本地clone一下这个项目,前提是需要将本机的公钥文件id_rsa.pub 复制到云服务器中的authorized_keys 文件里。

设置本地hexo的_config.yml 文件

1
2
3
4
deploy:
- type: git
repo: `你的服务器用户名@云服务器的IP地址:/root/myblog/hexo_blog.git`
branch: master

然后就可以使用hexo d 来提交部署,如果没有报错,则继续往下走。

五、云服务器git 钩子设置

云服务器上的git的钩子设置是为了将提交上来的文件,将其拷贝到第二步中nginx设置的域名目录

创建 /root/myblog/hexo_blog.git/hooks/post-receive

1
2
#!/bin/bash
git --work-tree=/var/www/myblog --git-dir=/root/myblog/hexo_blog.git checkout -f

保存退出,并添加执行权限,之后再hexo d 提交一下,看看是否可以正常自动部署了!

六、设置图片防盗链

其实经过上面几步的操作以后,基本上已经完成了hexo向云服务器自动部署了,但是可以再做一些优化,比如图片防盗链.

1
2
3
4
5
6
7
8
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
access_log off;
expires 1d;
valid_referers none server_names *.yangyanxing.com;
if ($invalid_referer){
rewrite ^/ http://ww1.sinaimg.cn/large/795ab47fly1g4a6llk7wjj20c808174s.jpg;
}
}

valid_referers 这行的作用是配置可以识别 refer,即可以正常获取资源文件的请求,在这里配置加入白名单的请求 refer 域名
参数说明:

  • none 代表请求的 refer 为空,也就是直接访问,比如在浏览器中直接访问图片 www.yangyanxing.com/test1.png,直接访问时,refer 会为空。
  • blocked refer 来源不为空,但是里面的值被代理或者防火墙删除了
  • server_names refer 来源包含当前的 server_nameslocation 的父节点 server 节点的 server_name 的值。
  • 字符串 定义服务器名称,比如 *.test1.com,配置后,来源是从 test1.com 就会被认为是合法的请求。
  • 正则表达式 匹配合法请求来源, 如 ~\.test2\.

当请求的 refer 是合法的,即可以被后面任一参数所匹配, $invalid_referer 的值为0, 若不匹配则值为 1, 进入 if 的代码中。我这里的设置是,如果是不合法的请求,就统一返回一张图片,也可以直接返回 403

在Ubuntu18.04服务器上部署Hexo博客

图片防盗链


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK