hexo部署
整体思路
本文使用Hexo框架,在阿里云服务器上搭建个人博客。整体思路为在本地进行Hexo安装以及平时帖子的编写,然后使用裸仓库以及Hexo的部署功能将博客部署到远程的服务器上进行访问。
本地环境
环境安装
文档
安装 Hexo 相当简单,只需要先安装下列应用程序即可:
- Node.js (Node.js 版本需不低于 10.13,建议使用 Node.js 12.0 及以上版本)
- Git
所有必备的应用程序安装完成后,即可使用 npm 安装 Hexo。
1
| $ npm install -g hexo-cli
|
服务器环境配置
Step1 、安装基础软件
1 2 3 4 5 6 7
| sudo apt install -y git nginx nodejs npm
git --version nginx -v node -v
|
Step2、Git裸仓库配置
创建裸仓库(bare repository)
在 Git 里,“裸仓库(bare repository)”指的是只有 Git 版本库数据,没有工作区的仓库。
1 2 3 4 5 6
| cd ~
mkdir hexo.git && cd hexo.git git init --bare
|
创建网站根目录
1 2
| sudo mkdir -p /var/www/hexo
|
配置自动部署钩子
1 2 3 4 5 6 7 8 9
| nano ~/hexo.git/hooks/post-receive
git --work-tree=/var/www/hexo --git-dir=/root/hexo.git checkout -f
按Ctrl+O → 回车确认 → Ctrl+X退出
|
Step3. Nginx配置
创建配置文件:
1
| sudo nano /etc/nginx/sites-available/hexo
|
文件内容:
1 2 3 4 5 6 7 8 9
| server { listen 80; server_name domain_name your_server_ip; root /var/www/hexo;
location / { try_files $uri $uri/ =404; } }
|
启用配置:
1 2 3 4 5 6 7 8
| sudo ln -s /etc/nginx/sites-available/hexo /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx
|
本地Hexo配置
安装 Hexo 完成后,请执行下列命令,Hexo 将会在指定文件夹中新建所需要的文件。
1 2 3
| $ hexo init <folder> $cd <folder> $ npm install
|
初始化后,您的项目文件夹将如下所示:
1 2 3 4 5 6 7 8
| . ├── _config.yml ├── package.json ├── scaffolds ├── source | ├── _drafts | └── _posts └── themes
|
修改_config.yml
文件:
1 2 3 4
| deploy: type: git repo: hexodeploy@your_server_ip:/home/hexodeploy/hexo.git branch: master
|
安装部署插件:
1
| npm install hexo-deployer-git --save
|
执行命令:
1 2 3
| hexo clean hexo g hexo d
|
最终验证
- 浏览器访问:
https://你的服务器IP
或域名
- 服务器查看日志:
1
| tail -f /var/log/nginx/access.log
|

主题更换
参考:
https://github.com/fluid-dev/hexo-theme-fluid
如何给网站添加免费数据统计 - webfem
参考文章
hexo部署到服务器上