vps利用率一直都不是很高,所以花点时间把博客搭在vps上,加快一下国内访问速度

前言

最近一直在玩docker,所以博客自然也是在docker下搭的~本来想用dockerfile,但是后来发现手动设置的东西太多了,就懒得自动化了,还是老老实实手动配置好了~~

辛酸史

结合了很多大佬的教程,自己总结出重(踩)要(坑)的地方……

如果是用docker搭,首先run的时候要开启容器防火墙,加上参数--privileged=true即可。

还有最好运行一下初始化命令

yum install initscripts.x86_64

0x01 建立裸库

mkdir /var/repo
git init --bare /var/repo/akkayin.github.io.git

0x02 建立网站文件夹

mkdir /var/www/
mkdir /var/www/blog # 放置博客文件
mkdir /var/www/akkayin.github.io

0x03 修改nginx配置文件

rm /etc/nginx/conf.d/default.conf # 删除默认配置文件
vi web.conf
# 复制下面代码进conf配置文件中,root指向放置博客的目录,server_name填自己的域名
server {
        listen 80;
        root /var/www/blog;
        index index.html index.htm index.nginx-debian.html;
        server_name blog.0akarma.me;
        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }
        location ~* ^.+\.(css|js|txt|xml|swf|wav)$ {
                root /var/www/blog;
                access_log   off;
                expires      10m;
        }
        location ~* ^.+\.(ico|gif|jpg|jpeg|png)$ {
                root /var/www/blog;
                access_log   off;
                expires      1d;
        }
}
# 最后还可以检查一下配置是否正确
nginx -t 
# 重启nginx
nginx -s reload

0x04 配置git钩子

vim /var/repo/akkayin.github.io.git/hooks/post-receive

# 将下面的两行代码粘贴进post-receive文件中,前面的是博客目录,后面是仓库目录
#!/bin/sh
git --work-tree=/var/www/blog --git-dir=/var/repo/akkayin.github.io.git checkout -f

# 保存之后,执行下面命令
chmod +x /var/repo/akkayin.github.io.git/hooks/post-receive

0x05 创建git用户

useradd -d /home/git -m git
chmod 740 /etc/sudoers
vim /etc/sudoers
# 在 root All=(ALL) ALL 后面追加
git     ALL=(ALL)       ALL
# 保存退出后
chmod 400 /etc/sudoers

sudo chown git:git /var/www/blog
sudo chown -R git:git /var/repo/akkayin.github.io.git

0x06 设置ssh-key

# 生成ssh-key
ssh-keygen -t rsa -f ~/.ssh/id_rsa_vps -C "yourmail@xxx.com" # 本机执行
cat ~/.ssh/id_rsa_vps.pub # 复制本机的公钥到服务器的authorized_keys中
vi ~/.ssh/authorized_keys

# 设置验证 (⬇️本机执行)
vim ~/.ssh/config
# 将下面5行复制进config中
# two
Host yourIp
HostName yourIp 
User git
IdentityFile ~/.ssh/id_rsa_vps

# 配置ssh设置 (⬇️服务器执行)
vim /etc/ssh/sshd_config
# 复制下面5行
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile      .ssh/authorized_keys
PermitRootLogin yes # 允许 root 用户 SSH 登陆
PasswordAuthentication yes # 禁用密码登陆

# 设置权限
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

# 重启服务 
service sshd restart
# 尝试手动ssh看看配置是否正常
# 如果出现connection refused,检查防火墙端口,以及是否安装sshd
ssh -v git@yourIp -p yourPort

0x07 更改博客配置文件_config.yml

deploy:
- type: git
  repo: https://github.com/akkayin/akkayin.github.io-.git # github仓库
  branch: master

- type: git
  repo: ssh://git@yourIp:yourPort/var/repo/akkayin.github.io-.git # 裸库目录
  branch: master                            
  message:

0x08 修改git权限

vi /etc/passwd

git:x:1003:1003:,,,:/home/git:/bin/bash # 将⬅️改成⬇️,只需复制/home后面的即可
git:x:1003:1003:,,,:/home/git:/usr/bin/git-shell #这样git就只能使用git-shell而不能使用bash。

Reference

使用 VPS 让 Hexo 博客快的飞起

hexo博客同时部署到github和VPS上

CentOS SSH提示:connect to host centos-py port 22: Connection refused

docker 容器防火墙设置