本人之前的Hexo
博客一直都是部署在GitHub
上的,通过github.io
域名后缀来访问,但是国内访问GitHub
网络一直不太稳定,因此考虑自己购买一台服务器,将个人博客部署到自己的服务器。
主要步骤有:
- 购买服务器、域名
- 在服务器上创建
git
仓库 - 配置
nginx
- 修改本地
hexo
配置
1. 购买服务器、域名
这部分没啥技术含量,有钱就行!
购买服务器(各大云厂商都有免费试用活动,可以先免费试用一段时间再考虑续费),我是购买了阿里云的服务器,阿里云官网,刚开始购买最低配置的应该就够了,后面有需求可以升级;购买域名,我购买的域名是(ysfun.ink),用来部署博客的是(blogs.ysfun.ink)。
2. 在服务器上创建git仓库
安装git
和nginx
yum install -y nginx git
- 配置ssh免密
# 切换到root账号根目录,网上有很多教程创建新用户,后面部署的时候容易出现权限不足的情况,因此我直接用root账号
cd /root
# 将本地的~/id_rsa.pub公钥内容复制到/root/.ssh/authorized_keys文件中
vim /root/.ssh/authorized_keys
- 初始化
git
仓库
# 创建git仓库,我创建在/var/repo目录下
mkdir /var/repo
cd /var/repo
git init --bare myblog.git
# 新建钩子文件
vim /var/repo/myblog.git/hooks/post-receive
post-receive
文件加入如下内容,注意/home/blog
目录可自行定义,这个目录是后面网站部署的文件根目录
#!/bin/bash
git --work-tree=/home/blog --git-dir=/var/repo/myblog.git checkout -f
# 给post-receive文件添加可执行权限
chmod +x /var/repo/myblog.git/hooks/post-receive
3. 配置nginx
# 查看nginx配置文件位置
nginx -t
# 修改nginx配置文件,只需要修改server_name(域名或ip地址)和root(网站文件的根目录,对应上面的/home/blog)即可
vim /etc/nginx/nginx.conf
# 重启nginx
systemctl restart nginx.service
# 查看nginx状态,看到绿色active(running)即为正常运行状态
systemctl status nginx.service
访问ip或者域名,如果出现404
则表示配置失败,有问题,可以优先排查服务器是否安全组是否暴露相应端口,如80, 443
;如果出现403
则正常,因为目前root
目录还没有任何文件。
可以在上面配置的root
目录(对应/home/blog
)加如下index.html
文件:
<html>
<body>
<p>This is my Blog.</p>
</body>
</html>
出现这样,即正常。
4. 本地修改hexo配置
修改博客根目录下的_config.yml
,主要修改两处:url
和deploy
同时部署到两个仓库:
deploy:
type: git
repo:
github: git@github.com:Ysfun/Ysfun.github.io.git
aliyun: root@47.110.56.85:/var/repo/myblog.git
branch: master
至此所有配置工作都完成了!
# 推送至个人服务器
hexo g -d
使用Ip或者域名就访问网站了!
如果想要通过域名访问,需要给域名备案,备案成功后就可以了,至此大功告成!!!
5. 启用SSL并部署
- 阿里云购买ssl证书,可以选择购买一年免费版的阿里云ssl证书
- 按照提示申请证书,申请完成后下载证书到本地
- 上传证书到远程服务器
scp nginx-ssl/* root@ip:/usr/local/nginx/conf/cert/
- 修改nginx配置文件
## 添加443端口server
# Settings for a TLS enabled server.
server {
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
server_name www.ysfun.cn;
root /home/blog;
ssl_certificate /usr/local/nginx/conf/cert/7872860_www.ysfun.cn.pem;
ssl_certificate_key /usr/local/nginx/conf/cert/7872860_www.ysfun.cn.key;
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 10m;
ssl_ciphers PROFILE=SYSTEM;
ssl_prefer_server_ciphers on;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
## http 80端口重定向
server {
listen 80;
server_name www.ysfun.cn;
rewrite ^(.*)$ https://$host:443$1 permanent;
}
重启nginx即可
评论区