VPS 部署记录

VPS 部署记录

最近发现了 VPS 越来越多的用处,于是想再搞一个VPS玩玩。

兴起

腾讯云原来 1 元 1 月的短期学生机太坑,于是先去看看国外的 VPS,看了看 Linode 和 Vultr,一个更比一个贵 23333,所以又去看了看阿里云的学生机,听 U 大说 100 Mbps 按量计费 15 G 用不完,然后开了一个发现无法按量计费…………又换回腾讯云了,长期优惠版,12 元 / 月,2 核 2 G 内存,还是挺爽。付费。装系统的时候看到只有 Debian 8.2,很是蛋疼,先装再说吧。

第一件事,改源

1
$ vim /etc/apt/sources.list

腾讯云的服务器当然还是用腾讯自家的镜像比较快,于是我把 jessie 改成了 stretch 试试有没有 Debian 9

1
$ apt update && apt upgrade && apt dist-upgrade

注:Debian 8 开始大多数时候可以直接用 apt 代替 apt-get,另一个选择是 aptitude

果然是有 Debian 9 镜像的,各种软件都升级了一波,非常愉快。升级以后看看内核版本

1
2
$ uname -a
Linux Luke-VPS 4.9.0-3-amd64 #1 SMP Debian 4.9.25-1 (2017-05-02) x86_64 GNU/Linux

4.9(滑稽),可以搞点事情?开启一波 BBR

1
2
3
4
5
6
7
8
$ echo "net.core.default_qdisc=fq" >> /etc/sysctl.conf
$ echo "net.ipv4.tcp_congestion_control=bbr" >> /etc/sysctl.conf
$ sysctl -p
net.core.default_qdisc = fq
net.ipv4.tcp_congestion_control = bbr
$ sysctl net.ipv4.tcp_available_congestion_control
net.ipv4.tcp_available_congestion_control = bbr cubic reno
$ lsmod | grep bbr

看到 tcp_bbr,搞定

看了看原本源里的 NGiNX,版本是 1.10,感觉还是不够新,加一波源

在 /etc/apt/sources.list 里加入如下部分

1
2
deb http://nginx.org/packages/debian/ stretch nginx
deb-src http://nginx.org/packages/debian/ stretch nginx

导入升级 key

1
$ wget http://nginx.org/keys/nginx_signing.key && apt-key add nginx_signing.key

更新源,安装 NGiNX

1
$ apt update && apt install nginx

查看 NGiNX 版本

1
2
$ nginx -v
nginx version: nginx/1.12.0

嗯,不错

中期准备

本地环境准备

Node.js

1
$ brew install node

创建 Hexo 目录

1
2
$ mkdir "hexo dir name"
$ cd "hexo dir name"

安装 Hexo

1
$ npm install -g hexo-cli

安装完成之后

1
2
3
4
$ hexo init
$ npm install
$ hexo d -fg
$ hexo serve

打开 http://localhost:4000 如果看到 Hexo 的初始页面证明安装成功。

生成 SSH 公钥密钥

1
2
$ cd ~/.ssh
$ ssh-keygen

它先要求你确认保存公钥的位置(.ssh / id_rsa),然后它会让你重复一个密码两次,如果不想在使用公钥的时候输入密码,可以留空。

如果你的机器重装了系统,或者是像我一样系统在炸了以后恢复备份了,可以通过 ssh-add 命令来加入私钥

服务器环境(以我的 Debian 9 为例)

安装 Git & Node.js

1
2
3
$ apt install git
$ curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -
$ apt-get install -y nodejs

创建 git 用户 & 禁用 git 用户的 shell 登录权限

1
2
$ adduser git
$ vim /etc/passwd

git 开头的那一行的 /bin/bash 部分改为 /usr/bin/git-shell

注:adduseruseradd 是不一样的,具体可以百度看看

添加证书登录

前面已经生成了公钥,现在把 .pub 公钥里的内容添加到服务器的 /home/git/.ssh/authorized_keys 文件中

初始化 Git 仓库

目录可自己选择

1
2
3
$ mkdir "git dir name"
$ cd "git dir name"
$ git init --bare hexo.git

使用 --bare 参数,Git 就会创建一个裸仓库,裸仓库没有工作区,我们不会在裸仓库上进行操作,它只为共享而存在。

配置 git hooks

hexo.git/hooks 目录下新建一个 post-receive 文件:

1
2
$ cd /var/repo/hexo.git/hooks
$ vim post-receive

post-receive 文件中写入如下内容:

1
2
#!/bin/sh
git --work-tree=/opt/hexo --git-dir=/var/hexo.git checkout -f

注:其中 /opt/hexo 为部署目录,/var/hexo.git 为该 git 仓库。

设置这个文件的可执行权限:

1
$ chmod +x post-receive

设置拥有者

1
2
$ chown -R git:git hexo.git
$ chown -R git:git /www/hexo

本地配置

现在配置 Hexo 的 deploy。

修改 hexo 目录下的 _config.yml 找到 deploy, 修改为:

repo 的地址为你自己的地址以及 git 仓库目录

1
2
3
4
deploy:
type: git
repo: git@www.example.com:/var/hexo.git
branch: master

在使用 git 部署之前,我们还得安装一个插件:

1
$ npm install hexo-deployer-git --save

开始使用

新建文章:

1
$ hexo new "post name"

生成 & 部署:

1
$ hexo clean && hexo g && hexo d

后续增加文章并部署:

1
$ hexo g && hexo d

后期及其他

  1. 将 NGiNX 的网页根目录指定到上面所说的位置,用 Let’s Encrypt 获取网站证书(使用 acme.sh 即可),并加入自动跳转 HTTPS 和 HTTP/2

    NGiNX 简要配置方法

  2. 使用 Hexo 的 Material 主题增加网页美观度

    Material主题文档

  3. 这台 VPS 也被我用来编译 LEDE,后面再来记录(挖坑)

参考

作者

Luke

发布于

2017-05-26

更新于

2023-05-29

许可协议

评论