Hexo博客部署到远程服务器

部署博客到服务器

昨天把基于go的hugo框架的博客部署在github page上了,但是发现github page服务器在美国,国内访问很不流畅。所以现在想把博客部署在国内的服务器上。

远程服务器

买了个京东云2c4g的服务器,部署博客肯定不需要这么大的。不过后面还有其他用
使用XShell ssh连接到服务器,之前在实习的时候经常用的是MobaXterm。大同小异。
服务器要开启HTTP80端口用于web访问,还有22端口用于git连接。

1. 服务器环境配置

1.1 安装Nginx

Nginx 作为反向代理服务器,负责在用户访问域名时,将请求转发到存放博客文件的路径,以实现快速、高效的静态内容分发

plaintext
1
2
3
yum install nginx
systemctl start nginx
systemctl enable nginx

1.2 创建博客目录

将以后的博客文件都放在这个目录下面,nginx路由配置也需要指向这个目录

plaintext
1
mkdir /home/www/website

1.3 配置nginx路由

plaintext
1
rpm -ql ngnix #用于列出RPM包管理器安装的nginx所有文件

/etc/nginx/vhost下创建blog.conf wwwblog.conf. 不直接在nginx/nginx.conf里配置是因为后续有新的需求在添加新配置文件就可以,提高效率

shell
1
2
3
4
5
6
7
8
9
10
11
12
server{        listen    80;
               listen   [::]:80;
               root /home/www/website;
               server_name priska997.com;
               location /{        
               }
               error_page   500 502 503 504  /50x.html;
                location = /50x.html {
                    root   /usr/share/nginx/html;
                }

}

/etc/nginx/nginx.conf添加    include /etc/nginx/vhost/*.conf;

1.4 安装node.js

plaintext
1
2
curl -sL https://rpm.nodesource.com/setup_10.x | bash -
yum install -y nodejs

直接请求node.js的安装脚本无法成功。猜测应该是服务器的网络问题。最后选择的是wget获得zip包然后配置一下环境也是可行的

2. git配置

通过git从本地计算机推送博客到服务器

shell
1
2
3
4
5
6
7
8
yum install git
adduser git
chmod 740 /etc/sudoers
vim /etc/sudoers
加上git ALL = (ALL)ALL
chmod 400 /etc/sudoers
sudo passwd git

配置服务器git仓库

shell
1
2
3
4
5
cd ~
git init --bare blog.git
vi ~/blog.git/hooks/post-receive
git --work-tree=/home/www/website --git-dir=/home/git/blog.git checkout -f
chmod +x ~/blog.git/hooks/post-receive

3. 本地hexo配置

本地的npm 和hexo先配置好,在一个文件夹里先初始化一个hexo博客

3.1 安装hexo相关插件

shell
1
2
npm install hexo-deployer-git --save
npm install hexo-server

3.2 修改hexo配置

在hexo博客根目录下的_config下修改配置

yaml
1
2
3
4
deploy:
 type: git
 repo: git@云服务器公网IP:/home/git/blog.git
 branch: master

3.3 远程部署

本地博客写好之后清除本地缓存,在进行生成静态文件和部署到远程服务器

plaintext
1
2
hexo clean
hexo g -d