Hexo部署到服务器


一、服务器配置

1.1 新建用户作为git登陆用户

[root@VM-12-10-centos superzqbo]# sudo adduser git
[root@VM-12-10-centos superzqbo]# sudo passwd git
更改用户 git 的密码 。
新的 密码:
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。

1.2 修改新建用户的权限为git权限

现在这个用户是具备 SSH 权限的,我们需将其切换为仅具有 git 权限。

$ vi /etc/passwd

# git:x:1001:1001::/home/git:/bin/bash 修改为下面的
$ git:x:1001:1001::/home/git:/bin/git-shell

image-20220707235803442

1.3 安装git

[root@VM-12-10-centos superzqbo]# sudo yum install -y git

image-20220707210450899

1.4 生成git密钥

ssh,简单来讲,就是一个秘钥,其中,id_rsa是你这台电脑的私人秘钥,不能给别人看的,id_rsa.pub是公共秘钥,可以随便给别人看。把这个公钥放在git仓库上,这样当你链接git仓库自己的账户时,它就会根据公钥匹配你的私钥,当能够相互匹配时,才能够顺利的通过git上传你的文件到git仓库上。

如果有生成过git密钥,直接去~/.ssh/目录下查找即可,该目录下有两个文件

公钥:id_rsa.pub
私钥:id_rsa

如果之前没有生成过,就需要使用下面指令生成一个

ssh-keygen -t rsa -C "superzqbo@163.com"

1.5 将密钥保存在服务器

1、服务器运行下面命令,创建.ssh文件夹
$ su git
$ mkdir ~/.ssh

2、创建.ssh/authorized_keys文件,打开authorized_keys文件并将id_rsa.pub的内容复制拷贝其中并保存
$ vim ~/.ssh/authorized_keys

3、修改权限
$ chmod 755 ~
$ chmod 700 ~/.ssh
$ chmod 600 ~/.ssh/authorized_keys

4、测试本地连接服务器(git bash here),连接成功表示本地和服务器连接时ok的
$ ssh -v git@42.194.221.71

image-20220707214208446

1.6 创建git仓库

1、切换到root用户,创建一个目录用于存储网站的根目录
$ su root

2、创建网站的根目录
$ mkdir /home/superzqbo/git/hexo
$ cd /home/superzqbo/git

3、修改用户所有权和用户权限(将当前目录下,所有文件的拥有者修改为git,用户组改为 git)
$ chown -R  git:git  hexo
$ chown -R 755 hexo
$ cd hexo

4、创建一个git项目(这里有一个细节,就是.git目录必须要有可读写权限,因为当我们在push的时候,是使用git用户推送到服务器上面去,会有一个写入的过程,如果不赋予可写权限,push就会失败。git目录,用于存储记录版本信息)
$ git init --bare hexo.git

5、新建git 钩子 post-receive,方便博客推送自动部署(相当于拷贝一份到Ngnix的html当中)
$ vim hexo.git/hooks/post-receive
# 输入下面的数据
#!/bin/sh
git --work-tree=/home/superzqbo/hexo --git-dir=/home/superzqbo/git/hexo/hexo.git checkout -f

6、赋予这个文件可执行权限
chmod +x /home/superzqbo/git/hexo/hexo.git/hooks/post-receive

钩子函数解析

git --work-tree=/home/superzqbo/hexo --git-dir=/home/superzqbo/git/hexo/hexo.git checkout -f
# --work-tree=/home/superzqbo/hexo 指定hexo文件的位置(ngnix的根目录)  
  
# --git-dir=/home/superzqbo/git/hexo/hexo.git git仓库的地址

此时,使用客户端上传代码后,除了在仓库下还会在/home/superzqbo/hexo目录下生成上传的代码,Nginx当中指定地址是这个即可。

二、客户端配置

2.1 配置

博客根目录_config下增加

root: /
deploy:
  type: git   
  # 用户@服务器地址:git仓库地址
  repository: git@42.194.221.71:/home/superzqbo/git/hexo/hexo.git
  branch: master

2.2 部署

hexo clean
hexo g
hexo d

2.3 Nginx配置

参考地址:https://www.yyyzyyyz.cn/posts/45dafe31d273/

# 安装
$ yum install -y nginx

# 查看Nginx配置文件的位置
$ whereis nginx
  
# 设置开机自启
$ systemctl enable nginx.service
  
# 检查配置文件
$ nginx -t

配置nginx为服务(Systemd服务)

vim /lib/systemd/system/nginx.service
--------------------------------------------------------
  
[Unit]
Description=nginx
After=network.target
  
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp= true
  
[Install]
WantedBy=multi-user.target
  
--------------------------------------------------------  
[Unit]:服务的说明
Description:描述服务
After:描述服务类别
[Service]服务运行参数的设置
Type=forking是后台运行的形式
ExecStart为服务的具体运行命令
ExecReload为重启命令
ExecStop为停止命令
PrivateTmp=True表示给服务分配独立的临时空间
注意:[Service]的启动、重启、停止命令全部要求使用绝对路径
[Install]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3

配置如下

server {
  listen       80 default_server;
  listen       [::]:80 default_server;
  server_name  42.194.221.71;
  root         /home/superzqbo/hexo;// 代码地址

  charset koi8-r;

  # access_log  /var/log/nginx/host.access.log  main;

  location / {
  }

  error_page  404              /404.html;
  location = /40x.html {
  }

  # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
  location = /50x.html {
    root   html;
  }

  # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

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

三、Https配置

参考地址: https://www.freesion.com/article/16501347598/

步奏如下

1、首先我们要拥有一个域名。本文使用的是腾讯云注册的域名。接着申请免费的SSL域名证书。
  
2、腾讯云首页登录后搜索SSL(其他网站类似),申请SSL证书,一般申请过程会在4小时以内给出结果
  
3、SSL证书申请成功之后,将其文件点击下载到本地上。这里我们使用的是Nginx证书
  
4、在服务器Nginx配置文件同级目录创建一个conf目录,将Nginx证书上传到该目录下
  
5、修改Nginx配置文件
# HTTPS server
#
server {
    #SSL 访问端口号为 443
    listen       443 ssl;
    #填写绑定证书的域名
    server_name  www.superbo.cloud;
    #证书文件名称
    ssl_certificate      /usr/local/nginx/conf/conf/superbo.cloud_bundle.pem;
    #私钥文件名称
    ssl_certificate_key  /usr/local/nginx/conf/conf/superbo.cloud.key;

    ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout  5m;

    #请按照以下协议配置
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    #请按照以下套件配置,配置加密套件,写法遵循 openssl 标准。
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
    ssl_prefer_server_ciphers  on;

    root         /home/superzqbo/hexo;
    location / {

    }
}
server {
    listen 80;

    #填写绑定证书的域名
    server_name www.superbo.cloud;

    #把http的域名请求转成https
    return 301 https://$host$request_uri;
}

注意,如果网站想要正常访问,还需要进行备案!


文章作者: superzqbo
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 superzqbo !
评论
  目录