搭建个人博客


一、本地搭建博客:(以下代码在CMD上运行)

1.在Windows本地下载Node.js
Node.js官网

2.安装cnpm

npm install -g cnpm --registry=https://registry.npm.taobao.org

3.使用cnpm在Windows本地安装hexo博客

cnpm install -g hexo-cli

4.在Windows本地新建blog文件夹:
CMD:

md blog

5.初始化hexo

hexo init

初始化成功后可以打开 http://localhost:4000 验证效果

6.在package.json文件中添加npm脚本:

"scripts": {
  "deploy": "hexo clean && hexo g -d",
  "start": "hexo clean && hexo g && hexo s"
},

(方便部署到服务器)
添加后就可以直接在第四个步骤做完以后上传到服务器了:

npm run deploy

二、git环境搭建:

1.在官网下载安装git
git官网

2.生成ssh认证:

git config --global user.name "yourname"
git config --global user.email youremail@example.com
ssh-keygen -t rsa -C "youremail@example.com"
git config --global core.autocrlf false  // 禁用自动转换,这个不设置后面上传时会出现警告

注:
最后获取到的ssh认证在C:Usersyourname.ssh

3.安装git

git --version // 如无,则安装
yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-devel
yum install -y git

4.在服务器新建用户并配置其仓库:

useradd git
passwd git // 设置密码
su git // 这步很重要,不切换用户后面会很麻烦
cd /home/git/
mkdir -p projects/blog // 项目存在的真实目录
mkdir repos && cd repos
git init --bare blog.git // 创建一个裸露的仓库
cd blog.git/hooks
vi post-receive // 创建 hook 钩子函数,输入了内容如下
#!/bin/sh
git --work-tree=/home/git/projects/blog --git-dir=/home/git/repos/blog.git checkout -f

添加完成后修改权限:

chmod +x post-receive
exit // 退出到 root 登录
chown -R git:git /home/git/repos/blog.git // 添加权限

5.测试git仓库是否可用,在Windows本地另找文件夹

git clone git@server_ip:/home/git/repos/blog.git

如果可以git下来空的仓库,就说明git仓库搭建成功了:

三、建立本地与服务器的连接:

1.在Windows本地建立ssh信任关系:
(Windows使用git Bash执行)

ssh-copy-id -i C:/Users/yourname/.ssh/id_rsa.pub git@server_ip
ssh git@server_ip // 测试能否登录

2.(可选)为了安全起见,要禁用git用户的shell登录权限,从而使其只能使用git clonegit push等登录:

cat /etc/shells // 查看 git-shell 是否在登录方式里面
which git-shell // 查看是否安装
vi /etc/shells
添加上2步显示出来的路劲,通常在 /usr/bin/git-shell

修改/etc/passwd中的权限:

// 将原来的
git:x:1000:1000::/home/git:/bin/bash

// 修改为
git:x:1000:1000:,,,:/home/git:/usr/bin/git-shell

四、部署到服务器(ngnix的使用):

1.下载并安装nginx

cd /usr/local/src
wget http://nginx.org/download/nginx-1.15.2.tar.gz
tar xzvf nginx-1.15.2.tar.gz
cd nginx-1.15.2
./configure // 如果后面还想要配置 SSL 协议,就执行后面一句!
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-file-aio --with-http_realip_module
yum -y install gcc gcc-c++ autoconf automake make // 安装C++编译环境
make && make install
alias nginx='/usr/local/nginx/sbin/nginx' // 为 nginx 取别名,后面可直接用

如果Nginx不能访问:

1:查看防火状态
systemctl status firewalld
service  iptables status
2:暂时关闭防火墙
systemctl stop firewalld
service  iptables stop
3:永久关闭防火墙
systemctl disable firewalld
chkconfig iptables off
4:重启防火墙
systemctl enable firewalld
service iptables restart  
5:永久关闭后重启
//暂时还没有试过
chkconfig iptables on

2.测试是否安装成功:

nginx

浏览器直接访问服务器的公网ip
如果可以访问,则说明安装成功,继续执行下面的操作。

停止nginx

nginx -s stop // 先停止nginx

3.修改nginx配置文件:

cd /usr/local/nginx/conf
vi nginx.conf
修改 root 解析路径,如下图
同时将 user 改为 root 如下图,不然nginx无法访问 /home/git/projects/blog


然后更新nginx配置:

sudo /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nginx -s reload

4.配置_config.yml文件里的deploy属性:

5.上传本地博客到服务器:

npm run deploy

6.(可选)将http网站设置为https

需要获取SSL证书
阿里云官方文档

参考文献:

https://blog.csdn.net/qq_35561857/article/details/81590953
https://www.jianshu.com/p/ea78bdd0551f/
https://blog.csdn.net/afei__/article/details/80717153


  目录