前言
喜爱分享是程序员的本能,所以大部分的程序员都会有一个本人的博客,外面的内容能够是一些工作中遇到的问题和解决思路,也能够是最近学习到的新技术的总结,也能够是对生存的思考和感悟。搭建集体博客的形式也有很多,能够间接在第三方博客平台上写作,如掘金、博客园、CSDN 等等,也能够应用 hexo 搭建博客部署到 github pages,当然如果领有公有云服务器的还能够在下面借助 wordpress 博客零碎搭建一个博客。本文要介绍的是应用 hexo 搭建博客,然而部署到公有云服务器。
筹备工作
本文重点介绍的是将博客部署到公有云服务器上,所以一些筹备工作默认是曾经做好了。具体包含以下几点:
- 一台曾经装置Nginx的公有云服务器,装置 Nginx 举荐应用军哥的LNMP一键安装包
- 一个博客域名:如果服务器是国外购买的,域名就不须要工信部备案,如果服务器是在阿里云/腾讯云等平台购买的,则域名须要工信部备案。并将域名解析到公有云服务器。
- 本地曾经装置node.js、hexo博客环境、git
部署步骤
搭建git仓库
新建git用户并设置明码
adduser git
passwd git
批改权限
chmod 740 /etc/sudoers
vim /etc/sudoers
找到root ALL=(ALL) ALL并在其上面增加
git ALL=(ALL) ALL
保留后改回sudoer权限:
chmod 400 /etc/sudoers
创立免密登陆证书
在服务器中关上RSA认证
vim /etc/ssh/sshd_config
找到以下三项并开启,若没有找到则增加
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
切换到git用户并开始配置ssh
su git
cd ~
mkdir .ssh && chmod 700 .ssh
接着,要为git用户里的.ssh增加开发者公钥,以便免密向git仓库推送数据。
在本地电脑通过 C盘用户目录/.ssh/id_rsa.pub找到ssh的公钥,在服务端.ssh里新建authorized_keys文件并将其复制到外面。
留神:公钥在authorized_keys文件中是一行增加一个
touch /home/git/.ssh/authorized_keys
sudo chmod 600 /home/git/.ssh/authorized_keys
vim /home/git/.ssh/authorized_keys
若之前未有上传文件至github等仓库的经验,本地客户端无ssh的keys,则要学生成一下。
批改git用户权限
给git用户设置权限,限度其只能应用git-shell向git仓库push或pull等,而不能登陆机器并获得一般shell命令控制系统。
应用which git-shell判断是否装置了git-shell。如果未装置,则yum install git
判断shells文件门路是否存在:cat /etc/shells,如果shells文件不存在或者文件中没有/usr/bin/git-shell,则
sudo vim /etc/shells
在最上面增加
/usr/bin/git-shell
创立仓库
在/var/repo创立空仓库(切换为root用户)
mkdir /var/repo
cd /var/repo
git init --bare blog.git
配置git hooks使得在仓库更新的时候,nginx配置文件中root指向的目录同步更新(这里root指向的是/home/wwwroot/www.blogdomain.com/)
vim /var/repo/blog.git/hooks/post-receive
增加
#!/bin/sh
git --work-tree=/home/wwwroot/www.blogdomain.com/ --git-dir=/var/repo/blog.git checkout -f
/home/wwwroot/www.blogdomain.com/
chmod -R 777 *
保留退出并设置权限
chmod +x /var/repo/blog.git/hooks/post-receive
更改blog.git拥有者
sudo chown -R git:git blog.git
应用chsh命令批改任意零碎用户的shell权限
sudo chsh git
在 Login Shell [/bin/bash]后输出:
/usr/bin/git-shell
批改完后验证cat /etc/passwd是否以git-shell结尾,例如:
git:x:1003:1003:,,,:/home/git:/usr/bin/git-shell
批改后,在本地客户端应用ssh git@serverip登陆将被回绝(第一次登陆该网址会提醒continue connecting,输出yes):
$ ssh git@gitserver
fatal: Interactive git shell is not enabled.
hint: ~/git-shell-commands should exist and have read and execute access.
Connection to gitserver closed.
git测试
实现上述步骤后,通过测试判断git服务器是否部署胜利,在本地客户端执行
git clone git@serverip:/var/repo/blog.git
如果近程服务器禁用22端口登陆,则能够应用如下语句:
git clone ssh://git@serverip:port/var/repo/blog.git
输出明码后(如果秘钥配置胜利则不必输出明码)若blog.git中没有内容将会提醒:
···
git@serverip`s password:
warning: You appear to have cloned an empty repository
Nginx 配置
本文假设你是应用了 lnmp 一键安装包,可应用以下命令将博客域名增加到虚拟主机:
lnmp vhost add
具体选项可按须要抉择配置:
最初回车,即可实现虚拟主机的增加。
hexo 本地配置
在本地hexo博客根目录批改_config.yml文件
# Deployment
## Docs: https://hexo.io/docs/deployment.html
deploy:
type: git
repo:
vps: git@serverip:/var/repo/blog.git
branch: master
将博客部署到公有云服务器
在 hexo 博客根目录顺次执行以下命令即可:
hexo clean
hexo g
hexo d
到这里就实现了hexo博客的配置和部署了,在浏览器输出域名(文章中示例用的是 www.blogdomain.com ) 即可拜访博客了。
发表回复