服务器端配置
1、创建文件目录, 用于博客站点文件存放, 并更改目录读写权限
mkdir -p /data/www/hexo
chmod -R 755 /data/www/hexo
2、安装并配置 Nginx
sudo yum -y install nginx # 安装 nginx
使用 yum 进行 Nginx 安装时,配置文件在 /etc/nginx 目录下。接下来配置 Nginx 服务器
vim /etc/nginx/nginx.conf
找到如下代码, 按如下模板修改
......
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name 服务器公网ip;
location / {
root /data/www/hexo;
index index.html index.htm;
}
}
......
listen监听80端口,这是nginx的默认端口;server_name可以写公网ip,也可以写域名。
例如,当在浏览器访问
server_name时,就会被这个location匹配,跳转到物理内存/data/www/hexo/index.html下寻找静态资源。拼接的方法是root+location+index。
重新加载配置并重启Nginx
sudo systemctl reload nginx # 重新加载配置,一般是在修改过 nginx 配置文件时使用。
sudo systemctl restart nginx # 重启 nginx 服务
3、安装git,nodejs
yum install git
yum install -y nodejs
4、创建文件目录, 用于私人 Git 仓库搭建, 并更改目录读写权限
mkdir -p /data/GitLibrary
chmod -R 755 /data/GitLibrary
5、Git 仓库配置
Git初始化裸库
cd /data/GitLibrary
git init --bare hexo.git
创建 Git 钩子文件(hook)
vim /data/GitLibrary/hexo.git/hooks/post-receive
添加如下内容,用于指定 Git 的源代码 和 Git 配置文件
#!/bin/bash
git --work-tree=/data/www/hexo --git-dir=/data/GitLibrary/hexo.git checkout -f
保存并退出后, 并给该文件添加可执行权限
chmod +x /data/GitLibrary/hexo.git/hooks/post-receive
本地电脑配置
找到 Hexo 博客站点配置文件 _config.yml , 修改其中的 url 和 deploy/repo
title: 页面标题
subtitle: 小标题
description: 描述
author: 作者
language: zh-Hans
timezone: 时区
# URL
url: 106.15.3.183 //服务器公网IP
......
# Deployment
deploy:
type: git
repo: root@106.15.3.183:/data/GitLibrary/hexo // 服务器用户名@服务器公网IP:文件目录
branch: master
将本地部署到服务器
hexo clean & generate & deploy
部署结果(密码是服务器的登录密码)
至此完成 Hexo 个人博客网站搭建, 再浏览器输入服务器 IP 即可访问。默认的端口号是80.
使用域名访问
1、将域名备案
因为阿里云是国内的服务器,想用域名访问它,就必须要备案,否则会被封停。
2、将域名解析到阿里云服务器
域名解析的写法:
3、修改服务器 Nginx 的配置
将上面的 nginx.conf 中 server_name 一项,由服务器公网ip改为域名。
重新加载配置并重启Nginx
sudo systemctl reload nginx # 重新加载配置,一般是在修改过 nginx 配置文件时使用。
sudo systemctl restart nginx # 重启 nginx 服务
4、修改本地电脑 Hexo 博客站点配置文件
将里面 url 和 deploy/repo 中的服务器公网ip也改为域名。重新部署。
hexo clean & generate & deploy
完成。
可能出现的问题
如果在浏览器输入域名无法展示博客内容,那就试一下在浏览器输入服务器公网ip看一下结果:
- 如果能正常访问,说明是域名问题,有可能是域名解析不对或者是服务器没有完成备案。
- 如果也无法访问,说明上面的操作有问题,或者是服务器的安全组策略中没有开放80端口。