Skip to content

Commit 9f625b8

Browse files
committed
备份初始文件
0 parents  commit 9f625b8

File tree

23 files changed

+450
-0
lines changed

23 files changed

+450
-0
lines changed

404/index.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
title: 404 Not Found:该页无法显示
2+
comments: false
3+
permalink: /404
4+
fancybox: false
5+
---
6+
7+
> 对不起,您需要访问的页面不存在
8+
> [回主页][index]·[所有文章][archives]·[留言板][about]
9+
10+
![Reveal 调试界面][revealImage]
11+
12+
[index]:http://hansoncoder.com/
13+
[archives]:http://hansoncoder.com/archives/
14+
[about]:http://hansoncoder.com/about/
15+
[revealImage]:/resources/404-reveal.png

_posts/VPS-building-hexo.md

Lines changed: 380 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,380 @@
1+
title: VPS(CentOS)搭建Hexo博客与Git Hooks更新(小白篇)
2+
date: 2016-3-2 22:28:00
3+
categories:
4+
- 环境搭建
5+
tags:
6+
- CentOS
7+
- VPS
8+
- Hexo
9+
- Hooks
10+
permalink: VPS-building-Hexo
11+
---
12+
  本机使用Hexo生成静态文件,通过Git推送到VPS的Git仓库。VPS配置Git Hooks,将静态文件同步到站点目录,再配置Nginx静态环境解析站点目录文件,显示Blog页面。
13+
14+
<!-- more -->
15+
16+
## 写文章目的
17+
1. 疏通VPS搭建Hexo的流程,理解每个搭建环节的作用。
18+
2. 将过程记录下来,引导想搭建相同环境的朋友少走弯路。
19+
20+
## 搭建流程与环境介绍
21+
22+
- Hexo博客从生产到展示的过程
23+
![流程图](/resources/VPS-And-Hexo/1.png)
24+
- VPS环境简介:我用的是`搬瓦工` 的VPS,其Root-Shell-basic有限制
25+
- VPS配置信息:
26+
![VPS配置信息](/resources/VPS-And-Hexo/2.png)
27+
- 本地配置信息:Mac 10.10.5
28+
![本地配置信息](/resources/VPS-And-Hexo/3.png)
29+
30+
## 要点分类:
31+
1. 本机安装Node.js、Hexo用于生成静态文件
32+
2. 本机安装Git,将静态文件Push到VPS的Git仓库
33+
3. VPS上安装Git,建立Git仓库、配置Hooks来同步静态文件
34+
4. 购买域名,在VPS上安装及配置Nginx环境,解析站点
35+
36+
## 安装步骤
37+
38+
#### 本机安装软件
39+
40+
本机需要安装Node.js、Hexo、Git,还不会装软件的自行Google
41+
42+
#### 本地配置Git
43+
> 本次演示配置信息如下,各自配置时替换即可
44+
> 本地Git邮箱:hansoncoder@gmail.com
45+
> 本地Git用户名:Hanson
46+
> VPS上网站配置文件:hexo.conf
47+
> VPS上站点目录:/var/www/hansoncoder.com/public
48+
> VPS上网站管理用户:Hanson
49+
> 域名:hansoncoder.com
50+
51+
1 设置本地Git邮箱及用户名
52+
53+
> ```
54+
MacdeiMac-2:~ Mac$ git config --global user.email "hansoncoder@gmail.com” #邮箱
55+
MacdeiMac-2:~ Mac$ git config --global user.name "Hanson" #用户名
56+
```
57+
> 查看配置是否正确
58+
> ```
59+
MacdeiMac-2:~ Mac$ git config --list
60+
user.email=hansoncoder@gmail.com
61+
user.name=Hanson
62+
filter.lfs.clean=git-lfs clean %f
63+
filter.lfs.smudge=git-lfs smudge %f
64+
filter.lfs.required=true
65+
```
66+
67+
2 生成sshkey
68+
> ```
69+
MacdeiMac-2:~ Mac$ ssh-keygen -t rsa -C "hansoncoder@gmail.com" #一路回车
70+
MacdeiMac-2:~ Mac$ cat .ssh/id_rsa.pub #复制公钥内容,从AAA开始到结束
71+
ssh-rsa AAAA....hansoncoder@gmail.com #...代替秘钥中间内容
72+
```
73+
74+
#### 购买域名
75+
76+
![购买的域名](/resources/VPS-And-Hexo/4.png)
77+
78+
#### VPS上安装Nginx
79+
80+
补充命令知识:
81+
> cat 查看文件内容
82+
> ls -l 查看文件详细信息,包括权限
83+
> echo 输出信息 (-e 是识别转义字符,输出的内容中"\n"被转义为换行)
84+
> mkdir 创建目录(文件夹)
85+
> touch 创建文件
86+
87+
1 配置Nginx源文件
88+
> 在/etc/yum.repos.d目录下创建一个源配置文件nginx.repo,写入如下代码
89+
90+
> ```
91+
[nginx]
92+
name=nginx repo
93+
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
94+
gpgcheck=0
95+
enabled=1
96+
```
97+
> 执行命令: 以后写入文件默认用echo -e 指令,以后会忽略指令过程,在适当的地方需要反斜杠(\)作为转义字符,大家也可以用图形界面操作。
98+
> ```
99+
[root /]# cd /etc/yum.repos.d
100+
[root yum.repos.d]# echo -e "
101+
[nginx]\n
102+
name=nginx repo\n
103+
baseurl=http://nginx.org/packages/centos/\$releasever/\$basearch/\n
104+
gpgcheck=0\n
105+
enabled=1\n
106+
" > nginx.repo
107+
```
108+
> 查看是否配置正确(使用cat 命令查看内容,以下配置文件大家自行使用查看命令验证配置信息)
109+
> ```
110+
[root /]# cat /etc/yum.repos.d/nginx.repo
111+
[nginx]
112+
name=nginx repo
113+
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
114+
gpgcheck=0
115+
enabled=1
116+
```
117+
118+
2 安装Nginx
119+
120+
> ```
121+
[root /]# yum install nginx -y
122+
```
123+
> 查看Nginx版本号(安装完成验证)
124+
> ```
125+
[root /]# nginx -v
126+
nginx version: nginx/1.8.1
127+
```
128+
129+
#### VPS上部署Nginx
130+
131+
1 添加站点解析配置文件
132+
133+
> 在/etc/nginx/sites-available目录下创建一个配置文件XXX.conf(这里以hexo.conf为例),并在文件中输入一下内容
134+
> ```
135+
server {
136+
root /var/www/hansoncoder.com/public; #站点目录
137+
index index.html index.htm; #主页,不用改
138+
server_name www.hansoncoder.com hansoncoder.com *.hansoncoder.com; #4.03 申请的域名
139+
location / {
140+
try_files $uri $uri/ /index.html;
141+
}
142+
}
143+
```
144+
> 执行以下命令,然后通过图形化界面写入内容
145+
> ```
146+
[root /]# mkdir -p /etc/nginx/sites-available
147+
[root /]# touch /etc/nginx/sites-available/hexo.conf
148+
```
149+
> ![图形用户写入内容过程](/resources/VPS-And-Hexo/5.png)
150+
> 查看hexo.conf配置文件信息(配置验证)
151+
> ![站点及域名(hexo.conf文件)配置信息](/resources/VPS-And-Hexo/6.png)
152+
153+
2 配置Nginx配置文件
154+
155+
> 在/etc/nginx/nginx.conf文件中添加如下代码(添加包含`sites-available`目录及站点`/var/www`目录下的配置文件)
156+
> ```
157+
include /etc/nginx/sites-available/*.conf;
158+
include /var/www/*.conf;
159+
```
160+
> ![Nginx配置文件(nginx.conf)配置步骤](/resources/VPS-And-Hexo/9.png)
161+
> 检查配置是否成功
162+
> ![Nginx配置文件(nginx.conf)配置内容](/resources/VPS-And-Hexo/10.png)
163+
164+
3 配置防火墙
165+
166+
> ```
167+
[root /]# iptables -I INPUT -p tcp --dport 80 -j ACCEPT
168+
```
169+
> 防火墙配置验证:
170+
> ```
171+
[root /]# /etc/init.d/iptables status
172+
```
173+
> 显示结果如果有:`1 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80\`,说明配置成功。
174+
175+
4 启动Nginx服务
176+
> ```
177+
[root /]# service nginx reload
178+
[root /]# /etc/init.d/nginx start
179+
```
180+
> ![Nginx服务启动成功](/resources/VPS-And-Hexo/11.png)
181+
182+
#### 创建站点管理者
183+
184+
为了安全考虑,在VPS上创建一个普通用户(Hanson)专门来管理网站的,其用户组为sitesManagers。
185+
186+
1 创建用户组及用户
187+
> 并创建其家目录与分配用户到sitesManagers组
188+
> ```
189+
[root /]# groupadd sitesManagers #创建用户组
190+
[root /]# useradd Hanson -m -g sitesManagers #创建用户,
191+
```
192+
> 查看用户信息(验证创建用户是否成功)
193+
> ```
194+
[root /]# finger Hanson
195+
Login: Hanson Name:
196+
Directory: /home/Hanson Shell: /bin/bash
197+
Last login Wed Mar 2 05:39 (EST) on pts/0 from 113.108.199.76
198+
No mail.
199+
No Plan.
200+
[root /]# groups Hanson
201+
Hanson : sitesManagers
202+
```
203+
204+
2 设置用户密码
205+
206+
> ```
207+
passwd Hanson
208+
```
209+
> 设置密码成功如下
210+
> ![Hanson密码设置成功](/resources/VPS-And-Hexo/13.png)
211+
212+
3 设置家目录权限
213+
214+
> ```
215+
chmod 755 /home/Hanson
216+
```
217+
218+
4 给Hanson用户添加sudo权限
219+
220+
> 修改/etc/sudoers文件,找到如下指令添加一条
221+
> ```
222+
## Allow root to run any commands anywhere
223+
root ALL=(ALL) ALL
224+
Hanson ALL=(ALL) ALL #新增这条指令
225+
```
226+
> ![用户权限设置信息](/resources/VPS-And-Hexo/14.png)
227+
228+
#### 建站测试
229+
230+
1 创建测试页面
231+
> 进入`/var/www/hansoncoder.com/public`目录(目录由4.0.5的root值确定),这里涉及的切换用户,应该使用”Root shell -interactive“
232+
> ```
233+
[root@localhost /]# cd /var
234+
[root@localhost var]# mkdir www
235+
[root@localhost var]# chmod 777 www
236+
[root@localhost var]# su Hanson
237+
[Hanson@localhost var]# mkdir -p /var/www/hansoncoder.com/public
238+
[Hanson@localhost var]# cd /var/www/hansoncoder.com/public
239+
[Hanson@localhost public]# echo “This is test HTML” > index.html
240+
```
241+
242+
2 测试(打开浏览器,输入你的域名,看看能否看到:This is test HTML)
243+
3 删除测试文件index.html
244+
245+
> ```
246+
[Hanson@localhost public]# rm index.html
247+
```
248+
> ![站点目录信息](/resources/VPS-And-Hexo/15.png)
249+
250+
#### VPS上安装配置Git
251+
252+
1 安装并验证Git
253+
> ```
254+
[root@localhost /]# yum install
255+
[root@localhost /]# git --version
256+
git version 1.7.1
257+
```
258+
259+
2 使用普通用户配置Git
260+
261+
> 在网站管理者的家目录配置.ssh公钥,用于静态文件本地与VPS同步。复制本地公钥,粘贴到VPS的authorized_keys文件中
262+
> ```
263+
[root@localhost /]# su Hanson
264+
[Hanson@localhost /]# cd /Home/Hanson
265+
[Hanson@localhost ~]# mkdir .ssh
266+
[Hanson@localhost .ssh]# cd .ssh
267+
[Hanson@localhost .ssh]# echo "本地的公钥(AAA开头)" > authorized_keys
268+
[Hanson@localhost .ssh]# cat authorized_keys #查看是否添加成功
269+
AAA....hansoncoder@gmail.com
270+
```
271+
> 无法使用shell-basic切换用户的,通过如下操作
272+
> ![Hanson创建authorized_keys文件](/resources/VPS-And-Hexo/16.png)
273+
> ![界面添加内容](/resources/VPS-And-Hexo/17.png)
274+
275+
3 设置VPS上ssh的端口号
276+
277+
> 将/etc/ssh/sshd_config文件中的Port为22设置如下:
278+
> ```
279+
Port 22
280+
```
281+
282+
4 ssh远程登录验证
283+
284+
> `本地`终端登录VPS的Hanson用户(ssh 用户名@域名),成功登录如下:
285+
> ```
286+
MacdeiMac-2: Mac$ ssh Hanson@hansoncoder.com
287+
git@hansoncoder.com's password:
288+
Last login: Tue Mar 1 02:21:36 2016 from 113.108.199.76
289+
[Hanson@localhost ][6]$
290+
```
291+
292+
#### VPS上配置Git仓库
293+
294+
在VPS上初始化Git仓库,配置Hooks目录下的post-receive文件,实现本地静态文件发布时自动同步到站点目录
295+
296+
1 初始化Git仓库
297+
> ```
298+
[root@localhost /]# su Hanson
299+
[Hanson@localhost /]# cd /home/Hanson
300+
[Hanson@localhost ~]# mkdir HansonBlog.git
301+
[Hanson@localhost ~]# cd HansonBlog.git
302+
[Hanson@localhost HansonBlog.git]# git init —bare
303+
[Hanson@localhost HansonBlog.git]# ls -l
304+
```
305+
> ![初始化成功信息](/resources/VPS-And-Hexo/18.png)
306+
307+
2 配置VPS上Git仓库的Hooks
308+
309+
> 在/home/Hanson/HansonBlog.git/Hooks目录创建post-receive,并写入以下内容
310+
> ```
311+
#!/bin/bash -l
312+
GIT_REPO=/home/Hanson/HansonBlog.git #Git仓库 10.1确定
313+
TMP_GIT_CLONE=/tmp/HansonBlog
314+
PUBLIC_WWW=/var/www/hansoncoder.com/public #站点目录 5.2步确定
315+
rm -rf ${TMP_GIT_CLONE}
316+
git clone $GIT_REPO $TMP_GIT_CLONE
317+
rm -rf ${PUBLIC_WWW}/*
318+
cp -rf ${TMP_GIT_CLONE}/* ${PUBLIC_WWW}
319+
```
320+
> 查看 post-receive文件内容(验证Hooks配置信息)
321+
> ![post-receive文件内容](/resources/VPS-And-Hexo/19.png)
322+
323+
## 博客初始化与编译发布
324+
325+
1. 本地初始化Hexo
326+
```
327+
MacdeiMac-2: Mac$ cd /Users/Mac/Desktop
328+
MacdeiMac-2:Desktop Mac$ mkdir myBlog
329+
MacdeiMac-2:Desktop Mac$ cd myBlog
330+
MacdeiMac-2:myBlog Mac$ hexo init
331+
```
332+
333+
2. Hexo编译生成静态文件
334+
```
335+
MacdeiMac-2:myBlog Mac$ npm install
336+
MacdeiMac-2:myBlog Mac$ hexo g
337+
```
338+
339+
3. 修改配置文件_config.yml,在_config.yml文件末写入如下内容
340+
```
341+
deploy:
342+
type: git
343+
message: Hanson
344+
repo: Hanson@hansoncoder.com:HansonBlog.git
345+
branch: master
346+
```
347+
348+
4. 发布到VPS服务器
349+
```
350+
MacdeiMac-2:myBlog Mac$ npm install hexo-deployer-git --save
351+
MacdeiMac-2:myBlog Mac$ hexo d
352+
```
353+
354+
## 配置结束,发布文章
355+
356+
- 写markdown文件,然后保存到suource/_posts文件夹
357+
- hexo g
358+
- hexo d
359+
360+
## 问题与解决方案
361+
362+
1.`yum install git`安装Git出现问题:/bin/bash: git: command not found
363+
解决(修改安装命令为):yum install git git-svn git-email git-gui gitk -y
364+
365+
2. ssh配置后,ssh连接验证:ssh: connect to host localhost port 22: Connection refused
366+
错误原因:
367+
2.1. sshd 未安装:yum install openssh-server
368+
2.2. sshd 未启动:chkconfig sshd on
369+
2.3. 防火墙:chkconfig iptables off(永久性生效,重启后不会复原)
370+
2.4. 端口号没有设置为22(或者设置后没有重启:service sshd restart)
371+
372+
3. 使用hero d报错:ERROR Deployer not found: git
373+
解决:npm install hexo-deployer-git --save
374+
375+
## 参考连接
376+
377+
1. ***安装流程*** by **叶雨梧桐BLOG** on <code>2015/03/04</code>: <http://blog.gt520.com/vps/239.html>
378+
1. ***linux用户管理*** by **海底苍鹰(tank)博客** on <code>2010/11/26</code>: <http://blog.51yip.com/linux/1137.html>
379+
1. ***VPS搭建Hexo,用GIt更新*** by **WikiLibrary** on <code>2016/01/26</code>: <http://tiktoking.github.io/2016/01/26/hexo/>
380+
1. ***CentOS上配置Ngxin实践*** by **王皓** on <code>2015/01/29</code>: <http://ninghao.net/blog/2088>

0 commit comments

Comments
 (0)