个人网站转到云服务器上
- 2020 年 1 月 2 日
- 筆記
硬件
- 电脑一台,这个管够
- 域名一个,freeyouth.cn
- 服务器一台,原则:哪家便宜买哪家
所需环境
- 客户端
- Git
- Node
- hexo-cli
- 服务器
- Nginx
- Git
步骤
一、客户端Git、Node的安装以及hexo的搭建
二、服务器端Git、Nginx的安装
用命令yum install -y nginx git安装git,安装完后分别输入git –version、nginx -v,出现相应#版本号即为安装成功。### 三、服务器Nginx的配置 在根目录创建博客站点的文件,并更改文件权限
mkdir -p /data/www/hexo chmod -R 777 /data/www/hexo
添加 index.html 用于检测配置 Nginx 是否成功
vim /data/www/hexo/index.html
添加代码如下
<!DOCTYPE html><html> <head> <title></title> <meta charset="UTF-8"> </head> <body> <p>Hello world!</p> </body></html>
修改nginx.conf配置文件
vim /etc/nginx/nginx.conf
在http下添加server模块

server { listen 80; //nginx 默认80端口 server_name www.xxx; root /data/www/hexo; }
开启nginx服务
systemctl start nginx
浏览器访问域名,查看能否正常访问index.html内容 若每次更改nginx配置,可输入命令nginx -s reload,让配置生效 一定要在服务器的安全组规则中添加80端口,否则不会有任何输出
四、服务器Git的配置
增加git用户,并授予相应权限
adduser git chmod 740 /etc/sudoers vim /etc/sudoers
找到
root ALL=(ALL) ALL
在下面添加一行
git ALL=(ALL) ALL sudo passwd git
五、Git仓库设置
切换到git用户,然后再服务器上初始化一个git裸库
su git cd ~ git init --bare blog.git
接着新建一个post-receive文件
vim ~/blog.git/hooks/post-receive
在文件中输入
#!/bin/sh git --work-tree=/data/www/hexo --git-dir=/home/git/blog.git checkout -f
保存退出后再赋予该文件执行权限
chmod +x ~/blog.git/hooks/post-receive
六、设置SSH,实现客户端免密登陆Git
切换为git用户,创建 ~/.ssh 文件夹和 ~/.ssh/authorized_keys 文件,并赋予相应的权限
su git mkdir ~/.ssh vim ~/.ssh/authorized_keys
然后将客户端.ssh文件夹下的idrsa.pub文件里的内容复制到authorizedkeys中,接着赋予相应的权限
chmod 600 ~/.ssh/authorized_keys chmod 700 ~/.ssh
查看客户端.ssh文件夹下的id_rsa.pub 打开gitbash,执行以下命令,复制里面内容即可
cd ~/.ssh ls cat id_rsa.pub
七、hexo文件配置修改
到此只需要把hexo build后的文件发布到服务器就阔以啦~ 打开hexo的_config.yml文件,拉到最底处,补充deploy的内容
deploy: type: git repository: git@ip:/home/git/blog.git #Git仓库地址,:符号后为Git仓库服务器路径 branch: master #分支,由于我们只用Git进行发布,master即可。
然后我们在终端执行
hexo clean hexo g hexo d
如遇报错
ERROR Deployer not found: git
安装hexo-deployer-git
npm install hexo-deployer-git -–save
博客文件就会上传到我们在服务器上的git仓库,然后再部署到我们创建的博客根目录。在浏览器中访问服务器地址,已经可以看到网站。

八、域名解析

登陆我的腾讯云平台,更改记录值为云服务器的外网ip即可。输入freeyouth.cn即可跳转到自己的网站了,访问速度比在GitHub里提升了几个档次。参照 https://www.jianshu.com/p/86e80be14d8f