在VPS上架设HEXO博客

趁活动入手了个1核1G腾讯云CVM,良心价64个月360元。第一时间安装nginx+git+hexo,过程坑颇多,Mark一下。

  1. 服务器安装nginx和git
  2. 本地安装hexo并部署网站到服务器

服务器操作系统为 Centos7.4,开启SELinux、Firewalld,配置SSH禁止密码登录、root登录并修改了默认22端口。(参见:《新购VPS SSH安全措施备忘》)

第一步 安装nginx

=== 安装nginx的rpm ===
[root@localhost ~]# rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

=== yum安装nginx ===
[root@localhost ~]# yum install -y nginx

=== 运行nginx ===
[root@localhost ~]# systemctl start nginx.service

=== 开机启动 ===
[root@localhost ~]# systemctl enable nginx.service

=== 配置防火墙 ===
[root@localhost ~]# firewall-cmd --add-service=http --permanent

=== 刷新防火墙配置 ===
[root@localhost ~]# firewall-cmd --reload

此时,通过浏览器访问服务器IP,应该会出现nginx欢迎信息。
默认网站根目录为/usr/share/nginx/html
默认网站的配置文件是/etc/nginx/conf.d/default.conf
如果打算直接用默认网站做HEXO部署的目录,可以跳过第二步。

第二步 配置nginx网站

自定义一个端口为8008,根目录为/var/www/hexo 的网站:

  1. 确认SELinux和Firewalld已经开放了8008端口
=== 查看SELinux的http端口 ===
[root@localhost ~]# semanage port -l | grep http
...
http_port_t tcp 80, 81, 443, 488, 8008, 8009, 8443, 9000
...

=== 如果希望SElinux开放其他http端口,如8007 ===
[root@localhost ~]# semanage port -a -t http_port_t -p tcp 8007

=== 查看Firewalld端口或服务是否有http ===
[root@localhost ~]# firewall-cmd --list-all
...
services:  ssh dhcpv6-client http
...

=== 将8008端口加入防火墙http服务 ===
[root@localhost ~]# firewall-cmd --service=http --add-port=8008/tcp --permanent

=== 刷新防火墙配置 ===
[root@localhost ~]# firewall-cmd --reload

=== 列出firewall http服务的端口
[root@localhost ~]# firewall-cmd --info-service=http
...
http
  ports: 80/tcp 8008/tcp
...
  1. 新建目录,确认目录权限。
=== 新建目录和测试文件 ===
[root@localhost ~]# mkdir /var/www
[root@localhost ~]# cd /var/www
[root@localhost www]# mkdir hexo
[root@localhost www]# cd hexo
[root@localhost hexo]# nano index.html
...
hello world!
...

=== 查看文件权限设置 ===
[root@localhost hexo]# ls -al
...
drwxr-xr-x. 7 root root 4096 Mar 8 16:04 .
drwxr-xr-x. 3 root root 4096 Mar 8 15:15 ..
-rw-r--r--. 1 root root 6557 Mar 8 16:04 index.html
...
# .表示当前文件夹hexo
# drwxr-xr-x 表示文件夹权限是 755
# -rw-r--r--表示文件权限是644
# 如非755 和 644 则需要修改一下

=== 更改文件权限 ===
[root@localhost hexo]# chmod -R 755 /var/www/hexo
[root@localhost hexo]# chmod -R 644 /var/www/hexo/index.html
  1. 配置文件
=== 新建网站配置hexo.conf ===
[root@localhost ~]# nano /etc/nginx/conf.d/hexo.conf
...
server {
    listen       8008;
    server_name  localhost;
    location / {
        root   /var/www/hexo;
        index  index.html index.htm;
    }
}
...

=== 重启nginx === 
[root@localhost ~]# nginx -s reload

此时,浏览器通过8008端口器访问服务器,应该会403错误。

  1. 修改SELinux访问限制
=== 将SELinux设置为Permissive宽容模式(可访问,提出警告) ===
[root@localhost ~]# setenforce 0
[root@localhost ~]# getenforce
Permissive

如此时,浏览器通过8008端口器访问服务器,如出现Hello World!,证明确实是SELinux导致网站出现403错误,下面我们来解决一下。

=== 安装SELinux策略文档 ===
[root@localhost ~]# yum install selinux-policy-doc

=== 查看SELinux日志 ===
[root@localhost ~]# grep nginx /var/log/audit/audit.log | audit2allow -m nginx

==== 出现以下提示 ===
......
module nginx 1.0;

require {
        type httpd_t;
        type var_t;
        class file { getattr open read };
}

#============= httpd_t ==============

#!!!! WARNING: 'var_t' is a base type.
#!!!! The file '/var/www/hexo/index.html' is mislabeled on your system.
#!!!! Fix with $ restorecon -R -v /var/www/hexo/index.html
allow httpd_t var_t:file { getattr open read };
......

==== 按提示操作 ===
# 提示需要开放 /var/www/hexo/index.html 的http_t读写权限
# 而我们选择开放/var/www/hexo 整个文件夹的 http_t读写权限
[root@localhost ~]# restorecon -R -v /var/www/hexo

=== 将SELinux设置回严格模式 ===
[root@localhost ~]# setenforce 1
[root@localhost ~]# getenforce
Enforcing

如此时,浏览器通过8008端口器访问服务器,如出现Hello World!则表示已解决SELinux的nginx权限问题。

第三步 安装git服务器

=== 安装git ===
[root@localhost ~]# yum install git

=== 创建git用户 ===
[root@localhost ~]# adduser git

=== 创建 ~/.ssh/文件夹和下面的authorized_keys文件 ===
[root@localhost ~]# cd /home/git/
[root@localhost ~]# mkdir .ssh
[root@localhost ~]# touch /home/git/.ssh/authorized_keys

=== 设置正确权限和所有权 ===
[root@localhost ~]# chmod 700 /home/git/.ssh
[root@localhost ~]# chmod 600 /home/git/.ssh/authorized_keys
[root@localhost ~]# chown -R git:git /home/git/.ssh

=== 将公钥黏贴至authorized_keys ===
[root@localhost ~]# nano /home/git/.ssh/authorized_keys

=== 测试是否能使用git用户免密登录 ===
# 如果不行,检查/etc/ssh/sshd_config文件,以下三项必须打开
 1.RSAAuthentication yes  
 2.PubkeyAuthentication yes
 3.AuthorizedKeysFile  .ssh/authorized_keys
# 如果可以用git用户免密登录,禁用git用户的shell登录
[root@localhost ~]# nano /etc/passwd
# 找到git用户那行,把行尾的/bin/bash 改成 /usr/bin/git-shell

其他免密设置可以参考《新购VPS SSH安全措施备忘》第3点。

第四步 服务器建仓和配置git自动脚本

=== 建立git目录 ===
[root@localhost ~]# mkdir /home/git/repo
[root@localhost ~]# cd /home/git/repo

=== 初始化空版本库 ===
[root@localhost repo]# git init --bare hexo.git

=== 配置自动脚本 ===
[root@localhost repo]# cd hexo.git/hooks
[root@localhost hooks] nano post-receive
# post-receive 内容:
......
#!/bin/sh
git --work-tree=/var/www/hexo --git-dir=/home/git/repo/hexo.git checkout -f
......
# 注意,上面的/var/www/hexo 要换成你自己的部署目录,
# /home/git/repo/hexo.git 则是刚刚新建的空版本库目录,
# 整个命令是我们从本地push到/home/git/repo/hexo.git以后,自动部署到/var/www/hexo上
=== 为脚本添加执行权限 ===
[root@localhost hooks]# chmod +x post-receive

=== 改变hexo.git目录的拥有者为git用户: ===
[root@localhost hooks]# chown -R git:git /home/git/repo/hexo.git

=== 改变部署目录的拥有者为git用户: ===
[root@localhost hooks]# chown -R git:git /var/www/hexo

第五步 本地安装和配置HEXO

  1. 本地安装git
  2. 本地安装node.js
  3. 本地安装hexo
  4. 新建hexo网站(如在Windows,可以使用git-bash)
$ hexo init <folder>
$ cd <folder>
$ npm install
  1. 设置主机别名

为了让git push的时候可以使用私钥登录服务器,必须设置SSH主机别名。MAC/Linux 编辑~/.ssh/config 文件,Windows在<用户文件夹>/.ssh/config 中,内容都是一样的:

Host alias
  Hostname <你的服务器IP>
  User git
  Port <你的服务器SSH端口>
  IdentityFile ~/.ssh/<你的对应私钥文件>
  1. 配置部署选项

然后编辑hexo网站的配置文件_config.yaml 中的部署选项,repo一项,前面是上一步设的别名alias ,后面是服务器的git版本库。

deploy:
  type: git
  repo: alias:/home/git/repo/hexo.git
  branch: master
  1. 部署网站
$ hexo g -d

此时,浏览器通过8008端口器访问服务器,出现Hexo网站默认主页。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,457评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,837评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,696评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,183评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,057评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,105评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,520评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,211评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,482评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,574评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,353评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,897评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,489评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,683评论 2 335

推荐阅读更多精彩内容

  • 本文讲解我在搭建Hexo博客的完整过程,主要内容包括GitHub与SSH、FAQ等。 另外还需要注意的是: Hex...
    风清袖一阅读 934评论 0 4
  • 本文参考了四弦同学的文章:在Linux服务器上搭建Hexo:OS X、Windows与Linux本地环境,讲解得非...
    野狗子嗷嗷嗷阅读 11,574评论 3 18
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,497评论 18 139
  • 第一章 安装Git工具 下载GitHub for Windows,直接点击安装,安装完成后,可以看到“Git Sh...
    不圆的石头阅读 11,832评论 5 63
  • 偶然 异地相聚 片言只语 不过几句问候与关注 虽然空间间距 微笑里相互祝福 举手投足 似乎路边桂树 迷香满屋 街边...
    云山任野阅读 162评论 1 3