美文网首页
用Hexo部署博客到Ubuntu服务器

用Hexo部署博客到Ubuntu服务器

作者: MrGod | 来源:发表于2018-11-11 21:00 被阅读38次

    本地安装Hexo,node.js,git

    参看<a href="/note-hexo-and-github.html">hexo 和 Github 搭建个人博客记录</a>

    服务器端

    首先需要一台Ubuntu服务器,并且可以用ubuntu用户身份正常登陆.

    配置SSH

    配置服务器的登陆选项,添加SSH,SSH公钥就用之前安装git时生成的公钥即可.

    在本地下载Xshell等工具,登陆方式也可以选为本地的私钥.

    注意在使用Xshell等终端时,若不小心键入了ctrl+s, 则这个命令为暂时挂起终端,只需要按ctrl+q即可继续输入

    安装Git和Nginx

    Git 用于版本管理和部署,Nginx 用于静态博客托管。

    sudo apt-get update
    sudo apt-get install git nginx -y
    

    创建Git仓库

    /var/repo/下创建名为hexo_static的裸仓库。用如下命令

    sudo mkdir /var/repo/
    sudo chown -R $USER:$USER /var/repo/
    sudo chmod -R 755 /var/repo/
    
    cd /var/repo/
    git init --bare hexo_static.git
    

    配置Nginx托管文件目录

    创建/var/www/hexo目录,用于Nginx托管,修改目录所有权和权限。

    sudo mkdir -p /var/www/hexo
    
    sudo chown -R $USER:$USER /var/www/hexo
    sudo chmod -R 755 /var/www/hexo
    

    随后修改Nginx的default设置,使root指向hexo目录.

    sudo vim /etc/nginx/sites-available/default
    

    注意一定要加sudo,否则会提醒default是只读文件.

    修改文件中对应的项

    ...
    
    server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;
    
        root /var/www/hexo; # 需要修改的部分
        index index.html index.htm;
    ...
    

    Vim的操作方法比较特殊,可以在网上查查

    重启Nginx服务,使得改动生效。

    sudo service nginx restart
    

    创建Git钩子

    不清楚钩子是什么

    在自动生成的 hooks 目录下创建一个新的钩子文件:

    vim /var/repo/hexo_static.git/hooks/post-receive
    

    在该文件中添加两行代码,指定 Git 的工作树(源代码)和 Git 目录(配置文件等)。

    #!/bin/bash
    
    git --work-tree=/var/www/hexo --git-dir=/var/repo/hexo_static.git checkout -f
    

    保存并退出文件,并让该文件变为可执行文件。

    chmod +x /var/repo/hexo_static.git/hooks/post-receive
    

    回到本地配置

    修改Hexo的默认配置

    在站点config.yml中修改博客的地址url

    # URL
    ## If your site is put in a subdirectory, set url as 'http://yoursite.com/child' and root as '/child/'
    
    url: http://server-ip # 没有绑定域名时填写服务器的实际 IP 地址。
    root: /
    permalink: :year/:month/:day/:title/
    permalink_defaults:
    

    通过Git部署

    先在任意位置处打开powershell, 从服务器上把hexo_static仓库克隆下来, 以此来将服务器地址添加到受信任的站点中。

    git clone ubuntu@server_ip:/var/repo/hexo_static.git
    

    注意在第一次进行这一步时会提示是否继续,选yes即可。

    再编辑Hexo的config.yml文件,找到Deployment, 修改为

    deploy:
      type: git
       repo: ubuntu@server_ip:/var/repo/hexo_static.git
      branch: master
    

    最后记得安装Hexo部署到Git仓库的包.

    npm install hexo-deployer-git --save
    

    于是就可用hexo d命令来部署了。大功告成。

    相关文章

      网友评论

          本文标题:用Hexo部署博客到Ubuntu服务器

          本文链接:https://www.haomeiwen.com/subject/zheffqtx.html