模板引擎art-template记录

1. 关于

art-template 是一个简约、超快的模板引擎,相类似的还有百度模版引擎等。

它采用作用域预声明的技术来优化模板渲染速度,从而获得接近 JavaScript 极限的运行性能,并且同时支持 NodeJS 和浏览器。

2.安装

  • 通过 npm 安装: npm install art-template --save
  • 下载安装

2.1 编译

因为浏览器不支持文件系统,所以 template(filename, data) 不支持传入文件路径,它内部使用 document.getElementById(filename).innerHTML 来获取模板,例如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
<!-- 引入template-web.js -->
<script src="./node_modules/art-template/lib/template-web.js"></script>
</head>
<body>
<div id="container"></div>
<!-- 创建 script 标签创建模板,注意下面几点 -->
<!-- 1. type="text/该斜杠后可以是 html,template... 不是script即可)" -->
<!-- 2. 给 script 标签添加 id ,此 id 即为模板 id -->
<!-- 3.模板 script 标签必须在 template() 方法调用的 script 标签之前 -->
<script type="text/html" id="tpl">
{{if user}}
<h2>{{user.name}}</h2>
{{/if}}
</script>
<script>
var user = {
name: "Template username",
};
var html = template("tpl", { user: user });
var container = document.querySelector("#container");
container.innerHTML = html;
</script>
</body>
</html>

3. 语法

art-template 同时支持两种模板语法。标准语法可以让模板更容易读写;原始语法具有强大的逻辑处理能力。

art-template 支持标准语法与原始语法。标准语法可以让模板易读写,而原始语法拥有强大的逻辑表达能力。

标准语法支持基本模板语法以及基本 JavaScript 表达式;原始语法支持任意 JavaScript 语句,这和 EJS 一样。

3.1 模版绑定

1
2
3
4
5
// 标准语法 {{if user}}
<h2>{{user.name}}</h2>
{{/if}} // 原始语法 <% if (user) { %>
<h2><%= user.name %></h2>
<% } %>

3.2 自定义输出

1
2
3
// 标准语法 {{value}} {{data.key}} {{data['key']}} {{a ? b : c}} {{a || b}} {{a
+ b}} // 原始语法 <%= value %> <%= data.key %> <%= data['key'] %> <%= a ? b : c
%> <%= a || b %> <%= a + b %>

3.3 原文输出

1
// 标准语法 {{@ value}} // 原始语法 <%- value %>

3.4 条件输出

1
2
3
4
5
6
7
8
9
10
11
12
13
// 标准语法

<!-- 单 if 判断 -->
{{if value}} ... {{/if}}

<!-- if ... else ... 判断 -->
{{if v1}} ... {{else if v2}} ... {{/if}} // 原始输出

<!-- 单 if 判断 -->
<% if (value) { %> ... <% } %>

<!-- if ... else ... 判断 -->
<% if (v1) { %> ... <% else if (v2) { %> ... <% } %>

3.5 循环输出

target是一个数组,each用于对数组遍历,$index  是数组的下标, $value是数组的值,并且target  支持  array  与object  的迭代,其默认值为  $data

1
2
// 标准语法 {{each target}} {{$index}} {{$value}} {{/each}} // 原始语法 <% for
(var i = 0; i < target.length; i++) { %> <%= i %> <%= target[i] %> <% } %>

3.6 自定义变量

1
2
// 标准语法 {{set temp = data.sub.content}} // 原始语法 <% var temp =
data.sub.content %>

3.7 模板继承

模板继承允许你构建一个包含站点共同元素的基本“模板骨架”

1
2
3
// 标准语法 {{extend './layout.html'}} {{block 'head'}} ... {{/block}} //
原始语法 <% extend ('./layout.html') %> <% block('head', function () { %> ... <%
}) %>

3.8 子模版

1
2
// 标准语法 {{include './header.art'}} {{include './header.art' data}} //
原始语法 <% include('./header.art') %> <% include('./header.art', data) %>

4. 调试器

art-template 内建调试器,能够捕获到语法与运行错误,并且支持自定义的语法。在 NodeJS 中调试模式会根据环境变量自动开启:process.env.NODE_ENV !== 'production'

设置 template.defaults.debug=true 后,等同于:

1
{ "cache": false, "minimize": false, "compileDebug": true }

5. 模板变量

template.defaults.imports模板通过 $imports 可以访问到模板外部的全局变量和导入的变量。

5.1 变量使用

template.defaults.imports.log = console.log;

<% $imports.log('Hello, template.defaults.imports.log') %>

5.2 内置变量清单

  • $data      传入模板的数据;
  • $imports 外部导入的变量以及全局变量;
  • print      字符串输出函数;
  • include   子模板载入函数
  • extend     模板继承模板导入函数
  • block      模板块生命函数

引用:https://www.jianshu.com/p/d8d8e19157e0

Android读图进度条

HoverProcessView

半透明镂空、圆形、动画 的进度条,仿微信和微博

特征:

  • 仿微信和微博的镂空半透明圆形进度
  • 支持开启和关闭动画
  • 背景支持圆角矩形
  • layout
1
2
3
4
5
6
7
8
9
10
11
<com.wuadam.hoverprocess.HoverProcessView
android:id="@+id/process"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:duration="1000"
app:hoverColor="#80000000"
app:max="100"
app:progress="30"
app:radius="48dp"
app:ringWidth="4dp"
app:roundCornerRadius="8dp" />

JAVA

1
HoverProcessView process = findViewById(R.id.process);

动画模式

1
process.setProgress(50, true);

无动画模式

1
process.setProgress(50, false);

无限模式

1
2
process.startInfiniteProgress();
process.stopInfiniteProgress();

onDestroy:

1
process.stop();

转载来源:

https://github.com/hzw1199/HoverProcessView

Linux VPS 挂载磁盘

一、扩展 CentOS 系统磁盘大纲

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#查看当前硬盘和分区信息
lsblk

NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 100G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 99G 0 part
├─centos-root 253:0 0 50G 0 lvm /
├─centos-swap 253:1 0 3.9G 0 lvm [SWAP]
└─centos-home 253:2 0 45.1G 0 lvm /home
sdb 8:16 0 200G 0 disk
└─sdb1 8:17 0 200G 0 part
sr0 11:0 1 1024M 0 rom


#创建新的物理卷PV
pvcreate /dev/sdb1

#若提示格式盘符不符标准请使用fdisk
WARNING: ntfs signature detected on /dev/sdb1 at offset 3. Wipe it? [y/n]
fdisk /dev/sdb

#查看详细信息
p

磁盘标签类型:dos
磁盘标识符:0x0a904f37

设备 Boot Start End Blocks Id System
/dev/sdb1 2048 419430399 209714176 7 HPFS/NTFS/exFAT

#使用change a partition's system id更改System
t

#更改目的Hex选择Linux LVM
8e

#保存配置项
w

#继续创建物理卷PV,以下两个警告可以yes忽略
pvcreate /dev/sdb1
WARNING: ntfs signature detected on /dev/sdb1 at offset 3. Wipe it? [y/n]
y
WARNING: dos signature detected on /dev/sdb1 at offset 510. Wipe it? [y/n]
y
Physical volume "/dev/sdb1" successfully created.

#成功后查看PVS物理卷信息
pvs

#查看现扩展的VG卷组(vgdisplay/vgs)
vgdisplay

--- Volume group ---
VG Name centos
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 4
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 3
Open LV 3
Max PV 0
Cur PV 1
Act PV 1
VG Size <99.00 GiB
PE Size 4.00 MiB
Total PE 25343
Alloc PE / Size 25342 / 98.99 GiB
Free PE / Size 1 / 4.00 MiB
VG UUID 5GZJnr-E1gX-xxxx-xxxx-xxxx-Id1m-YoK1Jf

#使用输出的VG NAME扩展卷组
vgextend centos /dev/sdb1

#查看现LV逻辑卷
lvdisplay

--- Logical volume ---
LV Path /dev/centos/swap
LV Name swap
VG Name centos
LV UUID bxJPDc-TeYX-xxxx-xxxx-xxxx-ZRJH-at3eah
LV Write Access read/write
LV Creation host, time localhost, 2021-12-22 13:02:25 +0800
LV Status available
# open 2
LV Size <3.88 GiB
Current LE 992
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:1

--- Logical volume ---
LV Path /dev/centos/home
LV Name home
VG Name centos
LV UUID OptJBh-cD7A-xxxx-xxxx-xxxx-E5pn-saZCtr
LV Write Access read/write
LV Creation host, time localhost, 2021-12-22 13:02:25 +0800
LV Status available
# open 1
LV Size <45.12 GiB
Current LE 11550
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:2

--- Logical volume ---
LV Path /dev/centos/root
LV Name root
VG Name centos
LV UUID KnPGiV-1eJp-xxxx-xxxx-xxxx-V6Ps-bdQ7l7
LV Write Access read/write
LV Creation host, time localhost, 2021-12-22 13:02:25 +0800
LV Status available
# open 1
LV Size 50.00 GiB
Current LE 12800
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0

#通常root根目录对应的逻辑卷进行全额不分区扩展
lvextend -l +100%FREE /dev/centos/root

#查看文件系统
df -TH -h

devtmpfs devtmpfs 7.8G 0 7.8G 0% /dev
tmpfs tmpfs 7.8G 0 7.8G 0% /dev/shm
tmpfs tmpfs 7.8G 8.7M 7.8G 1% /run
tmpfs tmpfs 7.8G 0 7.8G 0% /sys/fs/cgroup
/dev/mapper/centos-root xfs 50G 3.5G 47G 7% /
/dev/sda1 xfs 1014M 171M 844M 17% /boot
/dev/mapper/centos-home xfs 46G 100M 45G 1% /home
tmpfs tmpfs 1.6G 0 1.6G 0% /run/user/0

#使用XFS文件系统命令扩展
xfs_growfs /

#验证结果
df -h

二、具体挂载 EXT3/4 格式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
[root@MyVPS ~]# fdisk -l (查看磁盘分区)
Disk /dev/xvdb: 7516 MB, 7516192768 bytes
255 heads, 63 sectors/track, 913 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk /dev/xvdb doesn't contain a valid partition table
Disk /dev/xvda: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/xvda1 * 1 13 104391 83 Linux
/dev/xvda2 14 1044 8281507+ 8e Linux LVM
/dev/xvda3 1045 1305 2096482+ 8e Linux LVM
[root@MyVPS ~]# df -hal //*查看已划分区空间使用情况*//
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
9.3G 1.2G 7.7G 13% /
proc 0 0 0 - /proc
sysfs 0 0 0 - /sys
devpts 0 0 0 - /dev/pts
/dev/xvda1 99M 29M 66M 31% /boot
tmpfs 151M 0 151M 0% /dev/shm
none 0 0 0 - /proc/sys/fs/binfmt_misc
sunrpc 0 0 0 - /var/lib/nfs/rpc_pipefs
[root@MyVPS ~]# fdisk /dev/xvdb //*对硬盘/dev/xvdb进行增加分区操作*//
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel. Changes will remain in memory only,
until you decide to write them. After that, of course, the previous
content won't be recoverable.
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
Command (m for help): n //*输入n新建分区*//
Command action
e extended
p primary partition (1-4)
p //*输入p主分区*//
Partition number (1-4): 1 //*输入1第一个分区*//
First cylinder (1-913, default 1): //*回车跳过*//
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-913, default 913): //*回车跳过*//
Using default value 913
Command (m for help): w //*输入W保存退出*//
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@MyVPS ~]# mkfs -t ext3 /dev/xvdb1 //*将新分区xvdb1(此名称因系统而异,后缀+1)格式化为ext3格式*//
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
917504 inodes, 1833410 blocks
91670 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1879048192
56 block groups
32768 blocks per group, 32768 fragments per group
16384 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 21 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@MyVPS ~]# mount /dev/xvdb1 /home //*将新分区xvdb1挂载到目录/home*//
[root@MyVPS ~]# df -hal
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
9.3G 1.2G 7.7G 13% /
proc 0 0 0 - /proc
sysfs 0 0 0 - /sys
devpts 0 0 0 - /dev/pts
/dev/xvda1 99M 29M 66M 31% /boot
tmpfs 151M 0 151M 0% /dev/shm
none 0 0 0 - /proc/sys/fs/binfmt_misc
sunrpc 0 0 0 - /var/lib/nfs/rpc_pipefs
/dev/xvdb1 6.9G 144M 6.4G 3% /home
[root@MyVPS ~]# echo "/dev/xvdb1 /home ext3 defaults 1 2" >> /etc/fstab //*添加开机自动挂载*//
//*至此硬盘挂载成功*//

三、具体挂载 LVM 格式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
[root@MyVPS ~]# fdisk -l //*查看硬盘分区情况*//
Disk /dev/xvda: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/xvda1 * 1 13 104391 83 Linux
/dev/xvda2 14 1044 8281507+ 8e Linux LVM
/dev/xvda3 1045 1305 2096482+ 8e Linux LVM
Disk /dev/xvdb: 7516 MB, 7516192768 bytes
255 heads, 63 sectors/track, 913 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk /dev/xvdb doesn't contain a valid partition table
[root@MyVPS ~]#df -hal //*查看已划分区空间使用情况*//
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
9.3G 1.2G 7.7G 13% /
proc 0 0 0 - /proc
sysfs 0 0 0 - /sys
devpts 0 0 0 - /dev/pts
/dev/xvda1 99M 29M 66M 31% /boot
tmpfs 151M 0 151M 0% /dev/shm
none 0 0 0 - /proc/sys/fs/binfmt_misc
sunrpc 0 0 0 - /var/lib/nfs/rpc_pipefs
[root@MyVPS ~]#fdisk /dev/xvdb //*对硬盘/dev/xvdb进行增加分区操作*//
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel. Changes will remain in memory only,
until you decide to write them. After that, of course, the previous
content won't be recoverable.
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
Command (m for help):n //*输入n新建分区*//
Command action
e extended
p primary partition (1-4)
p //*输入p,主分区*//
Partition number (1-4): 1 //*由于xvdb还没有分区.我们这里输入1.第1个分区*//
First cylinder (1-913, default 1)://*回车跳过*//
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-913, default 913)://*回车跳过*//
Using default value 913
Command (m for help): t //*通过t来指定分区系统格式*//
Selected partition 1
Hex code (type L to list codes):8e //*输入8e LVM系统格式*//
Changed system type of partition 1 to 8e (Linux LVM)
Command (m for help):w //*输入w保存退出*//
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@MyVPS ~]#reboot //*重启使之生效*//
Broadcast message from root (pts/0) (Mon Aug 15 00:43:03 2011):
The system is going down for reboot NOW!
[root@MyVPS ~]#
Connection closed by foreign host.
Type `help' to learn how to use Xshell prompt.
Xshell:\>
Connecting to :22...
Connection established.
Escape character is '^@]'.
Last login: Mon Aug 15 00:41:06 2011 from
[root@MyVPS1280 ~]#fdisk -l //*查看硬盘分区情况*//
Disk /dev/xvda: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/xvda1 * 1 13 104391 83 Linux
/dev/xvda2 14 1044 8281507+ 8e Linux LVM
/dev/xvda3 1045 1305 2096482+ 8e Linux LVM
Disk /dev/xvdb: 7516 MB, 7516192768 bytes
255 heads, 63 sectors/track, 913 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/xvdb1 1 913 7333641 8e Linux LVM
[root@MyVPS1280 ~]#pvcreate /dev/xvdb1 //*创建物理卷*//
Physical volume "/dev/xvdb1" successfully created
[root@MyVPS1280 ~]#vgextend VolGroup00 /dev/xvdb1 //*将物理卷加入到组VolGroup00 *//
Volume group "VolGroup00" successfully extended
[root@MyVPS1280 ~]# vgdisplay //*查看物理卷组情况.可以看到我们有6.97G的容量可以扩展*//
--- Volume group ---
VG Name VolGroup00
System ID
Format lvm2
Metadata Areas 3
Metadata Sequence No 6
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 3
Act PV 3
VG Size 16.81 GB
PE Size 32.00 MB
Total PE 538
Alloc PE / Size 315 / 9.84 GB
Free PE / Size 223 / 6.97 GB
VG UUID ONGPxy-HBvY-xrrQ-IjEW-PIiO-2cX7-tg6tuI
[root@MyVPS1280 ~]# lvresize -L +6G /dev/VolGroup00/LogVol00 //*扩容6G(我们第一次加6G)*//
Extending logical volume LogVol00 to 15.59 GB
Logical volume LogVol00 successfully resized
[root@MyVPS1280 ~]#resize2fs /dev/VolGroup00/LogVol00 //*动态扩容分区大小*//
resize2fs 1.39 (29-May-2006)
Filesystem at /dev/VolGroup00/LogVol00 is mounted on /; on-line resizing required
Performing an on-line resize of /dev/VolGroup00/LogVol00 to 4087808 (4k) blocks.
The filesystem on /dev/VolGroup00/LogVol00 is now 4087808 blocks long.
[root@MyVPS1280 ~]# vgdisplay //*查看物理卷组情况.可以看到我们还有992M的容量可以扩展*//
--- Volume group ---
VG Name VolGroup00
System ID
Format lvm2
Metadata Areas 3
Metadata Sequence No 7
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 3
Act PV 3
VG Size 16.81 GB
PE Size 32.00 MB
Total PE 538
Alloc PE / Size 507 / 15.84 GB
Free PE / Size 31 / 992.00 MB
VG UUID ONGPxy-HBvY-xrrQ-IjEW-PIiO-2cX7-tg6tuI
[root@MyVPS1280 ~]# lvresize -L +992M /dev/VolGroup00/LogVol00 //*扩容992M(我们第2次加992M)*//
Extending logical volume LogVol00 to 16.56 GB
Logical volume LogVol00 successfully resized
[root@MyVPS1280 ~]# resize2fs /dev/VolGroup00/LogVol00 //*动态扩容分区大小*//
resize2fs 1.39 (29-May-2006)
Filesystem at /dev/VolGroup00/LogVol00 is mounted on /; on-line resizing required
Performing an on-line resize of /dev/VolGroup00/LogVol00 to 4341760 (4k) blocks.
The filesystem on /dev/VolGroup00/LogVol00 is now 4341760 blocks long.
[root@MyVPS1280 ~]#vgdisplay //*最后再查看扩容完没有 可以看到已经扩容完毕*//
--- Volume group ---
VG Name VolGroup00
System ID
Format lvm2
Metadata Areas 3
Metadata Sequence No 8
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 3
Act PV 3
VG Size 16.81 GB
PE Size 32.00 MB
Total PE 538
Alloc PE / Size 538 / 16.81 GB
Free PE / Size 0 / 0
VG UUID ONGPxy-HBvY-xrrQ-IjEW-PIiO-2cX7-tg6tuI
[root@MyVPS1280 ~]# df -hal //*查看已划分区空间使用情况 可以看到我们挂载成功*//
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
17G 1.2G 15G 8% /
proc 0 0 0 - /proc
sysfs 0 0 0 - /sys
devpts 0 0 0 - /dev/pts
/dev/xvda1 99M 29M 66M 31% /boot
tmpfs 151M 0 151M 0% /dev/shm
none 0 0 0 - /proc/sys/fs/binfmt_misc
sunrpc 0 0 0 - /var/lib/nfs/rpc_pipefs
//*至此硬盘挂载成功*//

常见问题:

Q:挂载过程中错误如何卸载

1
2
3
4
5
6
mount /dev/xvdb1 /mnt
df -h
Filesystem Size Used Avail Use% Mounted on
/dev/xvda1 40G 1.5G 36G 4% /
tmpfs 498M 0 498M 0% /dev/shm
/dev/xvdb1 5.0G 139M 4.6G 3% /mnt

使用命令卸载:umount /dev/xvdb1  假如提示设备忙碌,无法卸载,请尝试硬卸载  fuser -km /dev/xvdb1

CentOS离线环境解决安装与依赖方案

先前提要

内网无外网需要安装环境

整理准备

下载 CentOS 光盘完整版镜像

以 CentOS 6.x 为例,因为低版本 CentOS 的 nginx 是 epel,并且缺少 everything

下载地址:https://vault.centos.org/6.5/isos/x86_64/ 或 https://mirrors.aliyun.com/centos-vault/6.9/isos/x86_64/(6.x 都行)

以 CentOS 7 为例,目前是国内环境,我们使用国内 163 源的镜像 http://mirrors.163.com/centos/7/isos/x86_64/ ,当然阿里云、华中科大的源都行。

下载地址:http://mirrors.163.com/centos/7/isos/x86_64/CentOS-7-x86_64-Everything-2009.iso

然后将这个 iso 文件上传到服务器 /root 目录下:

<font style="color:#4D4D4C;">cd /root && wget http://mirrors.163.com/centos/7/isos/x86_64/CentOS-7-x86_64-Everything-2009.iso</font>

挂载 yum 本地库

于 CentOS 6.x

1.1 查看光驱

<font style="color:#4D4D4C;">cd </font><font style="color:#666600;">/</font><font style="color:#4D4D4C;">home</font>

1.2 挂载 iso 文件

mkdir /mnt/dvd && mount -o loop<font style="color:#4D4D4C;"> </font><font style="color:#4271AE;">CentOS</font><font style="color:#666600;">-</font><font style="color:#006666;">6.5</font><font style="color:#666600;">-</font><font style="color:#4D4D4C;">x86_64</font><font style="color:#666600;">-</font><font style="color:#4D4D4C;">bin</font><font style="color:#666600;">-</font><font style="color:#4D4D4C;">DVD1</font><font style="color:#666600;">.</font><font style="color:#4D4D4C;">iso </font>/mnt/dvd

mount iso 镜像文件,关键参数为-o loop,CentOS 6 之类的版本,可以 mkdir /mnt/dvd2,再参考前面的命令将第二张挂载到 /mnt/dvd2 上,更多请查看https://www.cnblogs.com/pumpkinhlk/p/14981770.html

这样就将光盘挂载 /mnt/dvd 目录了。

当然这个挂载命令只是一次性的,系统重启或者自己 umount 后就没了,需要使用本地源 yum 安装时需要线执行这个挂载命令。

1.3 修改安装源

将/etc/yum.repos.d 目录下,将里面存在的所有 repo 文件重新命名或者备份到其他目录下

cd /etc/yum.repos.d/ && rename .repo .repo.backup *.repo

1.4 修改配置文件

在/etc/yum.repos.d 目录下新建一个配置文件来设置本地源。

名称取为 local.repo 文件内容以及命令解释如下

1
2
3
4
5
6
7
[local-media]
name=CentOS-$releasever-Media
baseurl=file:///mnt/dvd/ #(yum源目录,源地址)
enabled=1 #(是否启用该yum源,0为禁用)
gpgcheck=1 #(检查GPG-KEY,0为不检查,1为检查)
#gpgkey=file:///mnt/dvd/RPM-GPG-KEY-CentOS-6 #(gpgcheck=0时无需配置,或可后期赋权)
#gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6

1.5 私钥权限赋值

<font style="color:#4D4D4C;">sudo rpm </font><font style="color:#666600;">--</font><font style="color:#8959A8;">import</font><font style="color:#4D4D4C;"> </font><font style="color:#666600;">/</font><font style="color:#4D4D4C;">etc</font><font style="color:#666600;">/</font><font style="color:#4D4D4C;">pki</font><font style="color:#666600;">/</font><font style="color:#4D4D4C;">rpm</font><font style="color:#666600;">-</font><font style="color:#4D4D4C;">gpg</font><font style="color:#666600;">/</font>RPM-GPG-KEY-CentOS-6

1.6 清理 yum 缓存

<font style="color:#4D4D4C;">yum clean all</font>

同时将服务器上的软件包信息先在本地缓存,以提高搜索安装软件的速度

yum makecache

1.7 测试 yum 源

<font style="color:#4D4D4C;">yum list</font>

于 CentOS 7.x

2.1 挂载

将iso文件拷贝到机器的某个目录下,比如  /home 目录下

<font style="color:#4D4D4C;">cd </font><font style="color:#666600;">/</font><font style="color:#4D4D4C;">home</font>

使用mount命令将iso文件挂载到某个目录下

mount -t auto /home/CentOS-7-x86_64-DVD-1611.iso /mnt

  • 这个时候使用命令 df -lh 可以看到挂载的目录。
  • 在实验环境中,命令执行完毕之后可以看到如下内容:

/dev/loop0 4.1G 4.1G 0 100% /mnt

2.2 修改安装源的配置文件

1
2
3
4
5
[centos7-local]
name=CentOS-7-x86_64
baseurl=file:///mnt/
enabled=1
gpgcheck=1

2.3 私钥权限赋值

sudo rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY*

2.4 清除 yum 缓存

<font style="color:#000000;">yum clean all</font>

2.5 测试 yum 源

<font style="color:#000000;">yum list</font>

恢复 yum 网络源

倘若希望恢复网络源,可以将备份的 yum 源配置文件覆盖掉刚才生成的本地源配置文件

cd /etc/yum.repos.d/ && rename .repo.backup .repo *.repo.backup

keepalived 安装

所需前置

Keepalived_2.0.20

注意太高版本在 CentOS 无法编译安装

安装依赖

yum install -y openssl openssl-devel gcc-c++

如之前没有挂在本地 yum 库,可以参考如何离线安装 openssl-devel等方法

解压编译安装

1
2
3
4
5
tar -zxvf keepalived-2.0.20.tar.gz
cd keepalived-2.0.20
./configure --prefix=/usr/local/keepalived
make
make install

注册服务

1
2
3
4
5
6
7
8
9
mkdir /etc/keepalived
cp /usr/local/keepalived-2.0.20/keepalived/etc/init.d/keepalived /etc/init.d/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
ln -s /usr/local/sbin/keepalived /usr/sbin/
ln -s /usr/local/keepalived/sbin/keepalived /sbin/

# 最后把配置文件和执行文件移到指定文件夹中
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/

修改配置文件

于/etc/keepalived/文件夹内

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
! Configuration File for keepalived

global_defs {
router_id bhz006
}

vrrp_script chk_nginx {
script "/etc/keepalived/nginx_check.sh" #运行脚本,脚本内容下面有,就是起到一个nginx宕机以后,自动开启服务
interval 2 #检测时间间隔
weight -20 #权重
}
# 定义虚拟路由,VI_1 为虚拟路由的标示符,自己定义名称
vrrp_instance VI_1 {
state MASTER #来决定主从
  interface eth0 # 绑定虚拟 IP 的网络接口,根据自己的机器填写,可通过ifconfig查询
virtual_router_id 222 # 虚拟路由的 ID 号
mcast_src_ip 68.174.25.208 #填写本机ip
priority 100 # 节点优先级,主要比从节点优先级高
nopreempt # 优先级高的设置 nopreempt 解决异常恢复后再次抢占的问题
advert_int 1 # 组播信息发送间隔,两个节点设置必须一样,默认 1s
authentication {
auth_type PASS
auth_pass 111
}
# 将 track_script 块加入 instance 配置块
track_script {
chk_nginx #执行 Nginx 监控的服务
}

virtual_ipaddress {
68.174.25.4 # 主从要一致,虚拟ip,也就是解决写死程序的ip怎么能切换的ip,也可扩展,用途广泛。可配置多个。
}
}

其中 nginx_check 脚本

1
2
3
4
5
6
7
8
9
#!/bin/bash
A=`ps -C nginx 每no-header |wc -l`
if [ $A -eq 0 ];then
/usr/local/nginx/sbin/nginx
sleep 2
if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
killall keepalived
fi
fi

成功后键入

启动服务:service keepalived start

停止服务:service keepalived stop

如无法启动,请确定service iptables status是否停止运行

正常情况下,tailf /var/log/messages可以成功查看 Sending gratuitous ARP 至虚拟 IP

注册开机启动

chkconfig keepalived on

测试 ip 漂移

关闭其中一个节点的服务器测试 ip 漂移,ip a | grep ip bash 会 return init ip <font style="color:rgb(56, 58, 66);background-color:rgb(250, 250, 250);">scope global eth0</font>

Nginx 安装

所需前置如下:

nginx-1.20.1

pcre-8.44

zlib-1.2.11

openssl-1.1.1l

安装 zlib

nginx 使用 zlib 对 http 包的内容进行 gzip

1
2
3
4
5
tar -zxvf zlib-1.2.11.tar.gz
cd zlib-1.2.11
./configure
make
make install

安装 pcre

nginx 的 http 模块使用 pcre 来解析正则表达式

1
2
3
4
5
tar -zxvf pcre-8.44.tar.gz
cd pcre-8.44
./configure
make
make install

安装 openssl

OpenSSL 是一个强大的安全套接字层密码库,包含主要的密码算法,常用的密钥证书封装管理功能和 SSL 协议。Nginx 不仅支持 http 协议,还支持 https

1
2
3
4
5
tar -zxvf openssl-1.1.1l.tar.gz
cd openssl-1.1.1l
./config
make
make install

编译 Nginx

1
2
3
4
5
6
7
8
9
10
tar -zxvf nginx-1.20.1.tar.gz
./configure --sbin-path=/usr/local/nginx/nginx \
--conf-path=/usr/local/nginx/nginx.conf \
--pid-path=/usr/local/nginx/nginx.pid \
--with-http_ssl_module \
--with-pcre=/home/pcre-8.44 \
--with-zlib=/home/zlib-1.2.11 \
--with-openssl=/home/openssl-1.1.1l
make
sudo make install

开启 nginx 进程,进入目录cd /usr/local/nginx/后,./nginx即启动,观察是否有[error]错误信息,启动后键入ps -ef | gref nginx查看是否有该进程,最后访问成功即启动完毕,最后可以输入./nginx -v可以浏览版本是否正确,最后带参数再启动./nginx -s reload -c nginx.conf

注意 nginx.conf 相关配置符合预期生产环境,如 keepalive_timout 等

注册服务

因为 CentOS6.x 使用的是 sysinit,CentOS7 使用的是 systemd,以下以 CentOS6.5 为例子。

于 CentOS 6.x

<font style="color:rgb(77, 77, 77);">vim /etc/init.d/nginx</font>

输入以下内容注册

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# this script create it by caffreyxin at 2007.10.15.
# it is v.0.0.1 version.
# if you find any errors on this scripts, please contact caffreyxin.
# and send mail to xinyflove at sina dot com.
#
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
# It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf

nginxd=/usr/local/nginx/nginx
nginx_config=/usr/local/nginx/nginx.conf

nginx_pid=/var/run/nginx.pid

RETVAL=0
prog="nginx"

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0

[ -x $nginxd ] || exit 0


# Start nginx daemons functions.
start() {

if [ -e $nginx_pid ];then
echo "nginx already running...."
exit 1
fi

echo -n $"Starting $prog: "
daemon $nginxd -c ${nginx_config}
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
return $RETVAL
}


# Stop nginx daemons functions.
stop() {
echo -n $"Stopping $prog: "
killproc $nginxd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid
}


# reload nginx service functions.
reload() {

echo -n $"Reloading $prog: "
#kill -HUP `cat ${nginx_pid}`
killproc $nginxd -HUP
RETVAL=$?
echo

}

# See how we were called.
case "$1" in
start)
start
;;

stop)
stop
;;

reload)
reload
;;

restart)
stop
start
;;

status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $prog {start|stop|restart|reload|status|help}"
exit 1
esac

exit $RETVAL

主要对应变量 nginxd 以及 nginx_config 根据自己实际填写,最后键入<font style="color:rgb(0, 0, 136) !important;">chmod </font><font style="color:rgb(0, 102, 102) !important;">755</font><font style="color:rgb(79, 79, 79);background-color:rgb(246, 248, 250);"> /etc/init.d/nginx</font>即可享用

启动服务:service nginx start

停止服务:service nginx stop

重启服务:service nginx reload

注册开机启动

略

JDK 安装

所需前置

jdk1.8

准备 jdk

创建 java 文件夹

mkdir /usr/local/java

将安装包转移到 java 文件夹中

mv jdk-8u202-linux-x64.tar.gz /usr/local/java/

解压安装包

tar zxvf jdk-8u202-linux-x64.tar.gz

配置 jdk

进入文件夹键入

vim /etc/profile

在末尾加上 JAVA_HOME 和 JRE_HOME,路径如下

1
2
3
4
5
JAVA_HOME=/usr/local/java/jdk1.8.0_66
JRE_HOME=/usr/local/java/jdk1.8.0_66/jre
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib
PATH=$JAVA_HOME/bin:$PATH
export PATH JAVA_HOME CLASSPATH

保存并退出,重新加载环境变量 source /etc/profile

校验是否安装成功

java -version

出现版本号即可成功

Tomcat 安装

解压即安装

Redis 安装

所需前置

Redis-service

解压编译安装

1
2
3
4
tar -zxvf redis-6.2.5.tar.gz
cd redis-6.2.5
make #当前目录下执行
make install --prefix=/usr/local/reids

安装成功后进入进入 src 目录启动./redis-serve看到 redis 的 logo 证明启动成功。

后台方式启动

根目录下vi redis.conf修改其中 daemonize no 为 daemonize yes

最后再次以配置文件启动即可 ./redis-server redis.conf

Q&A

在配置编译 pcre 时出现的问题 ivalid c++ compiler or c++ compiler flags

返回到根目录输入 yum install -y gcc gcc-c++即可

cannot open shared object file: No such fileordirectory

查看缺少哪些依赖包,补充编译

关于一键部署环境

可以前往https://lnmp.org/download.html(LNMP)或者https://oneinstack.com/auto/(LNMT)一键部署

简单调优最大进程数和最大文件打开数

查看<font style="color:rgb(51, 51, 51);background-color:rgb(254, 254, 254);">ulimit -n</font>默认 1024

临时修改的话<font style="color:rgb(51, 51, 51);background-color:rgb(254, 254, 254);">ulimit -</font>HSn<font style="color:rgb(51, 51, 51);background-color:rgb(254, 254, 254);"> 16384</font>即可,重启后失效

永久修改可以前往vim /etc/security/limits.conf 修改,最后reboot即可

1
2
3
4
5
*    soft    nofile    65536
* hard nofile 65536
* soft noproc 10240
* hard noproc 10240
# End of file

参数详解:

bash: ulimit

-H 设置硬件资源限制.

-S 设置软件资源限制.

-a 显示当前所有的资源限制.可以使用该命令查看进程占用资源的情况。

-c size:设置 core 文件的最大值.单位:blocks

-d size:设置数据段的最大值.单位:kbytes

-f size:设置创建文件的最大值.单位:blocks

-l size:设置在内存中锁定进程的最大值.单位:kbytes

-m size:设置可以使用的常驻内存的最大值.单位:kbytes

-n size:设置内核可以同时打开的文件描述符的最大值.单位:n

-p size:设置管道缓冲区的最大值.单位:kbytes

-s size:设置堆栈的最大值.单位:kbytes

-t size:设置 CPU 使用时间的最大上限.单位:seconds

-v size:设置虚拟内存的最大值.单位:kbytes 5

  • 代表针对所有用户

soft hard 软硬

noproc 是代表最大进程数

nofile 是代表最大文件打开数

configure: error: C compiler cannot create executables

gcc 没有安装好

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Making all in lib
make[1]: Entering directory `/usr/local/src/keepalived-1.2.24/lib'
make all-am
make[2]: Entering directory `/usr/local/src/keepalived-1.2.24/lib'
make[2]: Leaving directory `/usr/local/src/keepalived-1.2.24/lib'
make[1]: Leaving directory `/usr/local/src/keepalived-1.2.24/lib'
Making all in keepalived
make[1]: Entering directory `/usr/local/src/keepalived-1.2.24/keepalived'
Making all in core
make[2]: Entering directory `/usr/local/src/keepalived-1.2.24/keepalived/core'
CC namespaces.o
namespaces.c: In function ‘setns’:
namespaces.c:184: error: ‘__NR_setns’ undeclared (first use in this function)
namespaces.c:184: error: (Each undeclared identifier is reported only once
namespaces.c:184: error: for each function it appears in.)
make[2]: *** [namespaces.o] Error 1
make[2]: Leaving directory `/usr/local/src/keepalived-1.2.24/keepalived/core'
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory `/usr/local/src/keepalived-1.2.24/keepalived'
make: *** [all-recursive] Error 1

yum update glib*

离线更新 glib

http://copr-be.cloud.fedoraproject.org/results/mosquito/myrepo-el6/epel-6-x86_64/glibc-2.17-55.fc20/glibc-2.17-55.el6.x86_64.rpm

http://copr-be.cloud.fedoraproject.org/results/mosquito/myrepo-el6/epel-6-x86_64/glibc-2.17-55.fc20/glibc-common-2.17-55.el6.x86_64.rpm

http://copr-be.cloud.fedoraproject.org/results/mosquito/myrepo-el6/epel-6-x86_64/glibc-2.17-55.fc20/glibc-devel-2.17-55.el6.x86_64.rpm

http://copr-be.cloud.fedoraproject.org/results/mosquito/myrepo-el6/epel-6-x86_64/glibc-2.17-55.fc20/glibc-headers-2.17-55.el6.x86_64.rpm

<font style="color:rgb(77, 77, 77);">rpm -Uvh xx.rpm</font> 升级指定包

<font style="color:rgb(77, 77, 77);">rpm -e xx.rpm</font> 卸载由 <font style="color:rgb(77, 77, 77);">rpm -qa | grep xxx</font> 指定的包

免费Https证书Let Encrypt续期

1、写在前面

关于 Let’s Encrypt  免费 SSL 证书

Let’s Encrypt 作为一个公共且免费 SSL 的项目逐渐被广大用户传播和使用,是由 Mozilla、Cisco、Akamai、IdenTrust、EFF 等组织人员发起,主要的目的也是为了推进网站从 HTTP 向 HTTPS 过度的进程,目前已经有越来越多的商家加入和赞助支持。

Let’s Encrypt 免费 SSL 证书的出现,也会对传统提供付费 SSL 证书服务的商家有不小的打击。到目前为止,Let’s Encrypt 获得 IdenTrust 交叉签名,这就是说可以应用且支持包括 FireFox、Chrome 在内的主流浏览器的兼容和支持,虽然目前是公测阶段,但是也有不少的用户在自有网站项目中正式使用起来。

Let’s Encrypt  的最大贡献是它的  ACME  协议,第一份全自动服务器身份验证协议,以及配套的基础设施和客户端。这是为了解决一直以来  HTTPS TLS X.509 PKI  信任模型,即证书权威(Certificate Authority, CA)模型缺陷的一个起步。

在客户端-服务器数据传输中,公私钥加密使得公钥可以明文传输而依然保密数据,但公钥本身是否属于服务器,或公钥与服务器是否同属一个身份,是无法简单验证的。证书权威模型通过引入事先信任的第三方,由第三方去验证这一点,并通过在服务器公钥上签名的方式来认证服务器。第三方的公钥则在事先就约定并离线准备好,以备访问时验证签名之用。这个第三方就称为证书权威,简称 CA。相应的,CA 验证过的公钥被称为证书。

问题是,如果服务器私钥泄露,CA 无法离线使对应的证书无效化,只能另外发布无效记录供客户端查询。也就是说,在私钥泄露到 CA 发布无效记录的窗口内,中间人可以肆意监控服-客之间的传输。如果中间人设法屏蔽了客户端对无效记录的访问,那么直到证书过期,中间人都可以进行监控。而由于当前 CA 验证和签发证书大多手动,证书有效期往往在一年到三年。

Let’s Encrypt  签发的证书有效期只有 90 天,甚至希望缩短到 60 天。有效期越短,泄密后可供监控的窗口就越短。为了支撑这么短的有效期,就必须自动化验证和签发。因为自动化了,长远而言,维护反而比手动申请再安装要简单。

证书的有效期,我坚持认为这是合理的。Let’s Encrypt  的证书是自动签发的,对  Let’s Encrypt  的目标用户即个人站点来说,90 天已经是一个很长的时间了。特别是个人网站,域名所有权的变动是非常快的。可能前几天域名还属于张三,后几天就属于李四了。因此自动签发一个长有效期的证书是很容易产生问题的。如果证书的有效期是两年,那么经常交易域名的人就可以长时间持有已经不属于他们的域名的证书。并且这种持有不受证书吊销列表的控制,因为签发的时候,所有权是没有问题的,即使后来所有权变更了,Let’s Encrypt  也不会知道。所以设的有效期短一点,可以减少这个问题的影响。然后是易用性,现在已经有不少的  Let’s Encrypt  自动续期脚本了,配好后就不用管了,非常方便。


2、需求环境

1.主要面向续期 Let’s Encrypt 免费证书的个人网站

2.部署环境为

  • CentOS 系统
  • Nginx

当上述了解清楚后就可以开始续期我们的 Let’s Encrypt 免费证书,亦或者重新部署 Let’s Encrypt 免费证书替代你的不佳 Https


3、安装及配置

3.1 准备 acme.sh 协议

curl [https://get.acme.sh](https://get.acme.sh) | sh

若提示无权限,请使用 sudo 运行,因为 acme 生成的路径为~(root)/.acme.sh 目录下 并可以创建一个  bash  的  alias,  方便你的使用:

alias acme.sh=~/.acme.sh/acme.sh

3.2 生成安全证书

acme.sh  实现了  acme  协议支持的所有验证协议.  一般有两种方式验证: http  和  dns  验证.

这里仅提供三种方式面向用户生成证书文件 【www.mydomain.com】是指自己的域名,若使用Nginx配置,请对应相应的conf配置文件下的网站域名

一、寻址根目录

需要在你的网站根目录下放置一个文件,  来验证你的域名所有权,完成验证.  然后就可以生成证书

1
acme.sh  --issue  -d mydomain.com -d www.mydomain.com  --webroot   /home/wwwroot/mydomain.com/

按照官方的话来说,只需要指定域名, 并指定域名所在的网站根目录. acme.sh 会全自动的生成验证文件, 并放到网站的根目录, 然后自动完成验证. 最后会聪明的删除验证文件. 整个过程没有任何副作用。

二、链接 Apache 服务器
1
acme.sh --issue  -d mydomain.com   --apache
三、链接 Nginx 服务器
1
acme.sh --issue  -d mydomain.com   --nginx

值得我们注意的是,无论是 Apache 还是 Nginx 生成证书模式, acme.sh 在完成验证之后, 会恢复到之前的状态, 都不会私自更改你本身的配置. 好处是你不用担心配置被搞坏, 也有一个缺点, 你还需要自己手动配置 SSL 的配置否则只能成功生成证书, 你的网站还是无法访问 Https

3.3 部署安全证书

刚刚我们用 acme.sh 命令生成了安全证书,都均存放在~/.acme.sh 目录下,但是请不要直接使用此目录下的文件,  例如:  不要直接让  nginx/apache  的配置文件使用这下面的文件.  这里面的文件都是内部使用,  而且目录结构可能会变化的。

正确方法如下:

1
2
3
4
acme.sh  --installcert  -d  <domain>.com   \
--key-file /etc/nginx/ssl/<domain>.key \
--fullchain-file /etc/nginx/ssl/fullchain.cer \
--reloadcmd "service nginx force-reload"
  • 注意,分步执行,同时看看官方的说明:

这里用的是 service nginx force-reload, 不是 service nginx reload, 据测试, reload 并不会重新加载证书, 所以用的 force-reload

Nginx 的配置 ssl_certificate 使用 /etc/nginx/ssl/fullchain.cer ,而非 /etc/nginx/ssl/.cer ,否则 SSL Labs 的测试会报 Chain issues Incomplete 错误

3.4 更新 acme.sh

手动更新命令:

<font style="color:#F5222D;">acme.sh --upgrade</font>

自动更新命令:

<font style="color:#F5222D;">acme.sh --upgrade --auto-upgrade</font>

自动更新命令:


4、写在后面

<font style="color:#F5222D;">acme.sh --list</font>

使用该命令,可以快速查看绑定安全证书的网站

更多的参数,可以敲击acme.sh --help来查看帮助

笔者开题缘由

因为我也在续期证书的时候遇到瓶颈,当初这个证书的确是会自动续签的,但是居然在某一天停止了,所以我就找出问题,后来发现是几年来没更新  acme  协议和  letsencrypt CA  造成的,所以一口气下来就重新部署了。由于我是 Nginx 服务器上的网站,所以这件有段小插曲也是发生在 Nginx 下,最重要还是要善用nginx -t命令和systemctl status nginx.service来判定错误位置,同时在使用生成安全证书于网站根目录方式的话,记得没有权限判断这个网站是否为你时候,记得要如此操作。

尝试生成**.well-known**

在你的网站根目录下新建一个目录.well-known 即可

mkdir .well-known

更改配置问题

于 Nginx 配置文件 nginx.conf 做出修改

1
2
3
location ~ /\.ht {
deny all;
}

以上删除

1
2
3
location /.well-known/ {
root /XXX/XXX.com;
}

增加上述即可


2018.11.16

今天再一次续签 https  续签 A 域名不小心覆盖了 B 域名由此大家注意 nginx 问题

贴上自检命令

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

同时需要善用几个高级的 acme.sh 命令

1.高级 debug

acme.sh --issue ..... --debug 2

2.高级 Nginx

acme.sh --issue -d domain.com -w nginx:$NGINX_HOME/conf/nginx.conf

FastDFS教程及解决方案

1、准备

本篇攻略教程基于: CentOS6.5 **外网云服务器 单机版**FastDFS

在配置 FastDFS 前需要准备:

gcc 的安装

安装  nginx  需要先将官网下载的源码进行编译,编译依赖  gcc  环境,如果没有  gcc  环境,则需要安装:

yum install gcc-c++


1.1 安装 Nginx

**1. **直接**官网**下载**.tar.gz****安装包**

**2. **使用**wget****命令下载**

wget -c [https://nginx.org/download/nginx-1.14.0.tar.gz](https://nginx.org/download/nginx-1.14.0.tar.gz)

解压后即可

1
2
tar -zxvf nginx-1.14.0.tar.gz
cd nginx-1.14.0

1.2 Nginx 相关配置及指令

仅需安装成功,毋需以下操作

**1.****运行测试报告指令**

systemctl status nginx.service


**2.****添加模块指令**

1
2
3
4
5
6
./configure \
> --with-http_stub_status_module \
> --with-http_ssl_module \
> --with-http_gzip_static_module \
make
make install

3.验证模块指令模版

/usr/local/nginx/sbin/nginx -V  需要对应安装地址,默认目录指定为/var/temp/nginx  否则为/usr/local/nginx/

**4.****查找安装路径**

whereis nginx

5.**启停**nginx

1
2
3
4
5
cd /usr/local/nginx/sbin/
./nginx
./nginx -s stop
./nginx -s quit
./nginx -s reload

或者

1
2
3
service nginx start
service nginx restart
service nginx reload

6.查询**nginx**进程

ps aux|grep nginx

1.2.1 配置 Nginx

1. **下载并解压**fastdfs-nginx-module

Clone or Download > https://github.com/happyfish100/fastdfs-nginx-module/

解压  tar -xf fastdfs-nginx-module.tar

**2. **添加**fastdfs-nginx-module****模块**

1
2
3
4
5
6
7
./configure \
> --with-http_stub_status_module \
> --with-http_ssl_module \
> --with-http_gzip_static_module \
> --add-module=/data/wwwroot/ftp/fastdfs-nginx-module/src
make
make install
  • 注意:add-module=/data/wwwroot/ftp/XXX 是当前 fastdfs-nginx-module 解压出来的路径,由于我是 FTP 上传所以为此。请按部就班执行命令(本处使用 V1.16)

__

这里需要 http_stub_status_module 、 http_ssl_module 、 http_gzip_static_module 作为支撑,不然部分访问会 404 验证是否添加模块成功:

/usr/local/nginx/sbin/nginx -V

本Nginx安装在**/usr/local/ **请按部就班执行命令

1
2
3
4
5
nginx version: nginx/1.14.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --add-module=/data/wwwroot/ftp/fastdfs-nginx-module/src

看到以上提示即添加成功。


3、浅入理解 FastDFS

3.1.  什么是 FastDFS

FastDFS 是用 C 语言编写的一款开源的分布式文件系统。FastDFS 为互联网量身定制,充分考虑了冗余备份、负载均衡、线性扩容等机制,并注重高可用、高性能等指标,使用 FastDFS 很容易搭建一套高性能的文件服务器集群提供文件上传、下载等服务。

3.2 文件上传流程

2.3 文件下载流程

2.3 文件访问流程

客户端上传文件后存储服务器将文件 ID 返回给客户端,此文件 ID 用于以后访问该文件的索引信息。文件索引信息包括:组名,虚拟磁盘路径,数据两级目录,文件名。

  • **组名:**文件上传后所在的 storage 组名称,在文件上传成功后有 storage 服务器返回,需要客户端自行保存。
  • **虚拟磁盘路径:**storage 配置的虚拟路径,与磁盘选项 store_path*对应。如果配置了 store_path0 则是 M00,如果配置了 store_path1 则是 M01,以此类推。
  • **数据两级目录:**storage 服务器在每个虚拟磁盘路径下创建的两级目录,用于存储数据文件。
  • **文件名:**与文件上传时不同。是由存储服务器根据特定信息生成,文件名包含:源存储服务器 IP 地址、文件创建时间戳、文件大小、随机数和文件拓展名等信息。

2.4 FastDFS 图片服务器搭建

其拓扑图如下:


4、FastDFS 文件准备

4.1 文件准备

前面已经提及并加载了 fastdfs-nginx-module  这里是一个。

另外需要:

FastDFS_v5.08.tar

libfastcommon-1.0.7.tar.gz

还有先安装 libevent 工具

yum -y install libevent


5、安装 FastDFS

5.1 安装 libfastcommonV1.0.7 工具包`

`

  • 解压缩

tar zxf libfastcommonV1.0.7.tar.gz

  • 安装
1
2
3
cd libfastcommonV1.0.7/
./make.sh
./make.sh install

Install 后把/usr/lib64/libfastcommon.so 文件向/usr/lib/下复制一份

可以使用下列该指令完成寻找

ll libfastcommon*

5.2 安装 Tracker 服务

  • 解压缩

tar zxf FastDFS_V5.08.tar.gz

  • 安装
1
2
3
cd libfastcommonV1.0.7/
./make.sh
./make.sh install

Install 后在/usr/bin/目录下有以 fdfs 开头的文件都是编译出来的

Install 后的配置文件都放到/etc/fdfs 文件夹

可以使用下列该指令完成新建文件夹

mkdir fdfs/

把/data/wwwroot/ftp/FastDFS/conf 目录下的所有的配置文件都复制到/etc/fdfs 下

1
2
cd /data/wwwroot/ftp/FastDFS/conf
cp * /etc/fdfs

5.3 配置并启动 Tracker 服务

修改/etc/fdfs/tracker.conf 文件  vi tracker.conf  编辑代码 键盘按下“i”键入,开始修改

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# is this config file disabled
# false for enabled
# true for disabled
disabled=false
# bind an address of this host
# empty for bind all addresses of this host
bind_addr=
# the tracker server port
port=22122
# connect timeout in seconds
# default value is 30s
connect_timeout=30
# network timeout in seconds
# default value is 30s
network_timeout=60
# the base path to store data and log files
base_path=/data/wwwroot/fastdfs/tracker
  • 值得注意,base_path=换成你需要的存放服务路径,实际存在的

修改完毕后

键盘按下“ESC”键入,结束修改

并且输入

:wq保存并退出

或者

:q!强制关闭并退出

5.4 启动 Tracker 服务

/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart

5.5 安装 Storage 服务

单机版无需另外安装 Storage 服务

5.6 配置并启动 Storage 服务

修改/etc/fdfs/storage.conf 文件  vi storage.conf  编辑代码 键盘按下“i”键入,开始修改

值得注意几个地方:

1
2
# the port of the web server on this storage server
http.server_port=8888
  • 注意,Server 的 port 端口为 8888
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# true for binding the address configed by above parameter: "bind_addr"
# false for binding any address of this host
client_bind=true
# the storage server port
port=23000
# connect timeout in seconds
# default value is 30s
connect_timeout=30
# network timeout in seconds
# default value is 30s
network_timeout=60
# heart beat interval in seconds
heart_beat_interval=30
# disk usage report interval in seconds
stat_report_interval=60
# the base path to store data and log files
base_path=/data/wwwroot/fastdfs/storage

base_path 依旧换成你需要的存放图片路径,实际存在的

1
2
3
# store_path#, based 0, if store_path0 not exists, it's value is base_path
# the paths must be exist
store_path0=/data/wwwroot/fastdfs/storage

base_path0 换成你需要的首要存放图片路径,于上面相对应,实际存在的

1
2
3
# tracker_server can ocur more than once, and tracker_server format is
# "host:port", host can be hostname or ip address
tracker_server=139.129.30.222:22122
  • 注意 Tracker 服务器 IP 地址填写外网地址,注意端口 22122

以上结束修改。

5.7 启动 Storage 服务

/usr/bin/fdfs_trackerd /etc/fdfs/Storage.conf restart


5.8 配置 Client 连接服务

修改/etc/fdfs/client.conf 文件  vi client.conf  编辑代码 键盘按下“i”键入,开始修改

1
2
3
4
5
6
7
8
9
10
11
# connect timeout in seconds
# default value is 30s
connect_timeout=30
# network timeout in seconds
# default value is 30s
network_timeout=60
# the base path to store log files
base_path=/data/wwwroot/fastdfs/client
# tracker_server can ocur more than once, and tracker_server format is
# "host:port", host can be hostname or ip address
tracker_server=139.129.30.222:22122

base_path 依旧换成你需要的存放连接池的路径,实际存在的

  • 注意 Tracker 服务器 IP 地址填写外网地址,注意端口 22122

以上结束修改。

5.9 fastdfs-nginx-module 再配置

回看第二章步骤,我们已经成功编译到**NGINX**中并且验证添加模块成功,可以进行以下操作:  将/data/wwwroot/ftp/fastdfs-nginx-module/src/mod_fastdfs.conf 文件复制到/etc/fdfs 目录下再修改  vi mod_fastdfs.conf  编辑代码 键盘按下“i”键入,开始修改 值得注意几个地方:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# specify storage ids filename, can use relative or absolute path
# same as tracker.conf
# valid only when load_fdfs_parameters_from_tracker is false
# since V1.13
storage_ids_filename = storage_ids.conf
# FastDFS tracker_server can ocur more than once, and tracker_server format is
# "host:port", host can be hostname or ip address
# valid only when load_fdfs_parameters_from_tracker is true
tracker_server=139.129.30.222:22122
# the port of the local storage server
# the default value is 23000
storage_server_port=23000
# the group name of the local storage server
group_name=group1
# if the url / uri including the group name
# set to false when uri like /M00/00/00/xxx
# set to true when uri like ${group_name}/M00/00/00/xxx, such as group1/M00/xxx
# default value is false
url_have_group_name = true
  • 注意的是,Tracker 服务器 IP 地址填写外网地址,注意端口 22122,而后 url_have_group_name 需要开启不然 HTTP 访问 404
1
2
# the base path to store log files
base_path=/tmp

base_path 换成你需要的存放日志路径,实际存在的

1
2
3
4
5
# store_path#, based 0, if store_path0 not exists, it's value is base_path
# the paths must be exist
# must same as storage.conf
store_path0=/data/wwwroot/fastdfs/storage
#store_path1=/home/yuqing/fastdfs1

base_path 还是换成你需要的首要存放图片路径,于上面相对应,实际存在的

以上结束修改。

5.10 配置 Nginx

进入存放 Nginx 配置根目录  cd /usr/local/nginx/conf  编辑配置文件  vi nginx.conf  值得注意几个地方

1
2
user root;
worker_processes auto;

开头添加 ROOT 权限

1
2
3
4
server {
listen 80;
listen 8888;
server_name _;

添加一个 listen

1
2
3
location /group1/M00/{
ngx_fastdfs_module;
}

最后添加一个 Server,以上操作均无报错或者异常请执行复制:

/usr/lib64/libfastcommon.so 文件向/usr/lib/下复制一份

cp /usr/lib64/libfdfsclient.so /usr/lib/

然后请记得 Nginx 服务重启!


6、测试 FastDFS

1.  测试 Client 连接服务

上传一个图片(.JPG)和文字(.TXT)到 FTP 文件夹

/usr/bin/fdfs_test /etc/fdfs/client.conf upload XXX.jpg

/usr/bin/fdfs_test /etc/fdfs/client.conf upload XXX.txt

执行命令如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
This is FastDFS client test program v5.08
Copyright (C) 2008, Happy Fish / YuQing
FastDFS may be copied only under the terms of the GNU General
Public License V3, which may be found in the FastDFS source kit.
Please visit the FastDFS Home Page http://www.csource.org/
for more detail.
[2018-06-04 14:47:01] DEBUG - base_path=/data/wwwroot/fastdfs/client, connect_timeout=30, network_timeout=60, tracker_server_count=1, anti_steal_token=0, anti_steal_secret_key length=0, use_connection_pool=0, g_connection_pool_max_idle_time=3600s, use_storage_id=0, storage server id count: 0
tracker_query_storage_store_list_without_group:
server 1. group_name=, ip_addr=139.129.109.222, port=23000
group_name=group1, ip_addr=139.129.109.222, port=23000
storage_upload_by_filename
group_name=group1, remote_filename=M00/00/00/i4Ee3lsU4GWAbt8wABBiM_6UMxA671.jpg
source ip address: 139.129.109.222
file timestamp=2018-06-04 14:47:01
file size=1073715
file crc32=4271125264
example file url: http://139.129.109.222/group1/M00/00/00/i4Ee3lsU4GWAbt8wABBiM_6UMxA671.jpg
storage_upload_slave_by_filename
group_name=group1, remote_filename=M00/00/00/i4Ee3lsU4GWAbt8wABBiM_6UMxA671_big.jpg
source ip address: 139.129.109.222
file timestamp=2018-06-04 14:47:01
file size=1073715
file crc32=4271125264
example file url: http://139.129.109.222/group1/M00/00/00/i4Ee3lsU4GWAbt8wABBiM_6UMxA671_big.jpg

若看到以上的滚动执行,现在可以跳到浏览器,键入下面的 URL

http://139.129.109.222/group1/M00/00/00/i4Ee3lsU4GWAbt8wABBiM_6UMxA671.jpg

看得到图片即表示搭建完毕。


7、结语  —  问题解决方案

Q1 **访问图片报**404**,而文字却没有**

详细请查看 Nginx 错误和执行文件,解决问题的根本所在 切入点 Nginx 配置文件 conf

1
2
3
4
location ~ .*\.(gif|jpeg|png|bmp|swf|flv|mp4|ico|jpg)$ {
expires 30d;
access_log off;
}

查看是否有此延迟死亡配置,将 JPG 删除掉,即可成功执行。


更多问题咨询请留言,有幸看到即回复 :-)

Quartz初浅使用

1、Quartz 简介

本教程建立在**SSM**整合点**Maven**项目的商城网站上 实现概览:通过提交商品订单,限制用户在规定时间付款,若仍未付款达到规定时间取消订单动作。


  • 官方介绍

Quartz 是 OpenSymphony 开源组织在 Job scheduling 领域又一个开源项目,它可以与 J2EE 与 J2SE 应用程序相结合也可以单独使用。Quartz 可以用来创建简单或为运行十个,百个,甚至是好几万个 Jobs 这样复杂的程序。Jobs 可以做成标准的 Java 组件或  EJBs。 Quartz 用一个小 Java 库发布文件(.jar 文件),这个库文件包含了所有 Quartz 核心功能。这些功能的主要接口(API)是 Scheduler 接口。它提供了简单的操作,例如:将任务纳入日程或者从日程中取消,开始/停止/暂停日程进度。

2、下载及其安装

2.1 下载

官方 Site: http://www.quartz-scheduler.org

Click 下 Download 即可下载最新 这里显示最新为 2.2.3

2.2 添加 Maven 依赖

一般使用 Quartz 以及 Spring4 框架整合使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.3</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-jobs</artifactId>
<version>2.2.3</version>
</dependency>

和 Spring 整合,需要 spring-context-support 的 jar 包

3、简单剖析

Quartz 框架简单架构

  1. Schedule ——  核心调度器
  2. Job ——  任务
  3. JobDetail ——  执行任务器
  4. Tigger ——  触发器

使用方法:

  1. 制作一个任务 Job java 类 需要一个执行方法
  2. 将类放到 spring 容器内,注意需要注解
  3. 配置 jobDetail,指定相应的任务 job
  4. 配置一个 Tigger,编写一个由 corn 表达式的触发方法的触发器
  5. 配置一个配置调度工厂

执行流程图:

4、使用教程

4.1 创建一个 Job Java 类

值得注意需要声明 execute()

1
2
3
4
5
6
7
8
public class Scheduler1 {
public void execute()
{
System.out.println("The task has been completed...");
System.out.println("Tasks to complete!");
System.out.println(new Date());
}
}

4.2 配置 Spring

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<!-- 配置job类 -->
<bean id="scheduler1" class="com.hmall.order.schedule.Scheduler1"></bean>
<!-- 配置JobDetail执行任务器 -->
<bean id="SpringQtzJobMethod"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<ref bean="scheduler1"></ref> <!-- 要执行的job名称 -->
</property>
<property name="targetMethod"> <!-- 要执行的方法名称 -->
<value>execute</value>
</property>
</bean>
<!-- 配置Trigger调度触发器 -->
<bean id="CronTriggerBean"
class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="SpringQtzJobMethod"></property>
<property name="cronExpression" value="0/5 * * * * ?"></property>
</bean>
<!-- 配置调度工厂 -->
<bean id="SpringJobSchedulerFactoryBean"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="CronTriggerBean"></ref>
</list>
</property>
</bean>

值得注意:

1. job:scheduler1 – jobDetail:scheduler1 job:SpringQtzJobMethod – Trigger:SpringQtzJobMethod Trigger:CronTriggerBean – FactoryBean:CronTriggerBean

2.需要使用 corn 表达式

4.3 运行调试

运行 Maven 项目  clean tomcat7:run

1
2
3
4
5
DEBUG - Calling execute on job DEFAULT.SpringQtzJobMethod
DEBUG - batch acquisition of 1 triggers
The task has been completed...
Tasks to complete!
Wed Jun 20 11:46:20 CST 2018

看到以上 success 即成功

5、Corn 表达式

Cron 表达式的说明

字段名 允许的值 允许的特殊字符
秒 0-59 , - * /
分 0-59 , - * /
时 0-23 , - * /
日 1-31 , - * ? / L W C
月 1-12 or JAN-DEC , - * /
周 1-7 or SUN-SAT , - * ? / L C #
年 empty, 1970-2099 , - * /

Cron字符的说明

字符名 字符说明
“?”字符 表示不确定的值
“,”字符 指定数个值
“-”字符 指定一个值的范围
“/”字符 指定一个值的增加幅度。n/m 表示从 n 开始,每次增加 m
“L”字符 用在日表示一个月中的最后一天,用在周表示该月最后一个星期 X
“W”字符 指定离给定日期最近的工作日(周一到周五)
“#”字符 表示该月第几个周 X。6#3 表示该月第 3 个周五

Cron 常用表达式案例说明

表达式 含义
0 0 12 ? 每天中午 12 点触发
0 15 10 ? 每天上午 10:15 触发
0 15 10 ? 每天上午 10:15 触发
0 15 10 ? * 每天上午 10:15 触发
0 15 10 ? 2005 年的每天上午 10:15 触发
0 *14 ** ? 在每天下午 2 点到下午 2:59 期间的每 1 分钟触发
0 0/5 14 ? 在每天下午 2 点到下午 2:55 期间的每 5 分钟触发
0 0/5 14,18 ? 在每天下午 2 点到 2:55 期间和下午 6 点到 6:55 期间的每 5 分钟触发
0 0-5 14 ? 在每天下午 2 点到下午 2:05 期间的每 1 分钟触发
0 10,44 14 ? 3 WED 每年三月的星期三的下午 2:10 和 2:44 触发
0 15 10 ? * MON-FRI 周一至周五的上午 10:15 触发
0 15 10 15 * ? 每月 15 日上午 10:15 触发
0 15 10 L * ? 每月最后一日的上午 10:15 触发
0 15 10 ? * 6L 每月的最后一个星期五上午 10:15 触发
0 15 10 ? * 6L 2002-2005 2002 年至 2005 年的每月的最后一个星期五上午 10:15 触发
0 15 10 ? * 6#3 每月的第三个星期五上午 10:15 触发

Cron 表达式构造器

Site: http://cron.qqe2.com

Jar 软件: CronExpBuilder-1.0

在Icarus主题下开启评论模块

Valine

Valine评论插件的漏洞会导致此评论服务暴露所有评论者的IP地址 ([xCss/Valine#336](https://github.com/xCss/Valine/issues/336)) 请更换其他评论服务

何为 valine 评论?

Valine 诞生于 2017 年 8 月 7 日,是一款基于 LeanCloud 的快速、简洁且高效的无后端评论系统。 理论上支持但不限于静态博客,目前已有 Hexo、Jekyll、Typecho、Hugo、Ghost 等博客程序在使用 Valine。

和其他评论模块的优势

  • 属于国内的评论模块,在网站加载速度和人性化方面略优
  • LeanCloud 背书,有一定保障性
  • 无意义上后端,接入便捷
  • MarkDown 全语法支持,并支持Emoji

注册 LeanCloud 并创建应用

注册 LeanCloud 成功后创建应用

点击设置-应用 Key 获得 App key 即创建成功,由于 LeanCloud 对免费用户开发版进行一系列限制,接下来开始对该应用进行设置。

设置 - API 访问域名

绑定自己已有在国内备案过的域名,在设置-域名绑定-API 访问域名设置。

  • 注意不可以填入自己的 GitHub page 的 URL,github.io 也不行会引起 403 ERROR
  • 国际版可以使用免费的二级域名,这里不推荐使用,因为体验过后不太佳,尽量还是使用自有域名

设置 - 云引擎、ClientEngine 域名

同上,绑定域名,显示已绑定即成功绑定。

配置 Icarus 的 config

打开部署的 Hexo 根目录,在 theme 下的 Icarus 文件夹里vi _config.yml开始配置配置项,示例如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Comment plugin configurations
# https://ppoffice.github.io/hexo-theme-icarus/categories/Plugins/Comment/
comment:
type: valine
app_id: W3zVbpO*****gzGzoHsz
app_key: DUKL************Lb1hNyD5q3
placeholder: "欢迎留言…"
notify: false
verify: false
avatar: 'retro'
meta: ["nick", "mail"]
page_size: 10
highlight: true
record_ip: false

附带 - 参数解释

参数 配置 说明
type changyan,disqus,disqusjs,facebook,gitalk,gitment,isso,livere,utterances,valine [必填] 这里默认 valine
app_id W3zVbpO*****gzGzoHsz [必填] 为 LeanCloud 应用的 AppID
app_key DUKL************Lb1hNyD5q3 [必填] 为 LeanCloud 应用的 AppKey
placeholder 欢迎留言… [可选] 评论前提示语
notify false [可选]notify 邮件提醒无论 true 和 false 都是不生效的,因为 valine 评论自带的邮件提醒功能将在 v1.4.0 发布时下线
verify false [可选]评论时是否有验证码,如为 true 需要在 Leancloud 设置->安全中心 中打开图形验证码服务
avatar mp,identicon,monsterid,wavatar,retro,robohash,hide [可选]评论发表时,不填入邮箱 email 或无Gravatar 头像显示的头像类型,详细见Valine 针对 avatar 头像的说明
meta [“nick”, “mail”, “link”] [可选]分别对应评论栏所供填入的昵称、邮箱地址和个人网站
page_size 10 [可选]分页配置
highlight true [可选]是否开启高亮代码
record_ip false [可选]是否评论后显示 IP

至此,键入hexo clen & g重启服务即可看到评论模块生效成功。

申请推送流服务

QMsg

因某些原因,Qmsg 酱将要停业,并关闭新用户登录通道,原有用户可以继续使用到完全停业。感谢您一直以来对 Qmsg 酱的支持!

由于 QMsg 已经关闭注册入口,现在新增国内另外一家提供推送消息服务 ServerChan

ServerChan

「Server 酱」,英文名「ServerChan」,是一款「程序员」和「服务器」之间的通信软件。

说人话?就是从服务器推报警和日志到手机的工具。

进入ServerChan官网,按流程注册获取到SCKEY,在下方测试正常推送到关注好到微信公众号收到测试消息,即可成功。

安装 Valine-admin

首先确保上面流程走完且评论正常,方可展开 valine 后台部署,然后进入 LeanCloud 对应的应用,点击 云引擎 -> 部署 -> 部署项目 -> Git 部署 -> Git 配置 填写 GIt 代码库并保存:[https://github.com/sviptzk/Valine-Admin-Server](https://github.com/sviptzk/Valine-Admin-Server) 然后再次点击 Git 部署 第一次部署请勾选不使用缓存,最后点击部署,查看日志无报错且部署完成就此部署第一阶段完成。

配置部署后台的环境变量

点击 云引擎 -> 设置 -> 添加新变量:

变量名 示例 说明
SITE_NAME My_blog [必填] 网站名称
SITE_URL https:/bk.catooilg.com [必填] 网站地址,最后不要加 /
SMTP_USER postmaster@catooilg.com [必填] SMTP 服务用户名,一般为邮箱地址。
SMTP_PASS password [必填] SMTP 密码,一般为授权码或者是邮箱的登陆密码,请自行查询对应邮件服务商的获取方式
SMTP_SERVICE qiye.aliyun 邮件服务提供商,请查询对应到邮件服务商参数
填入
SENDER_NAME Yuki [必填] 寄件人名称。
TEMPLATE_NAME default [必填] 设置提醒邮件的主题,默认为
default,可以选择填入 default,rainbow,custom1,custom2
DISABLE_EMAIL true [可选],填写则代表停止发送邮件
SCKEY xxx [可选] 填入 ServerChan 的 SCKEY
FAVICON https://img.catooilg.com/catooilg/2016/07/valine_favicon.ico [可选] 网页 favicon 图标

添加完成后,点击保存,并且重新部署实例即可。

初始化后台管理

第一次打开绑定的域名会提示需要登录,在域名后补上 /sign-up,先注册你的登录信息,即可进入后台进行管理操作。

设置防休眠

点击 云引擎 -> 定时任务,新增定时器,配置如图所示:

保存后运行即可。

常见问题

Q:提示 undefined is not a valid value

自定义环境变量没有配置好,请再次检查

Q:提示 Invalid status code: { favicon: ‘https://cdn.jsdelivr.net/gh/sviptzk/StaticFile_HEXO@v3.2.3/butterfly/img/favicon.ico‘ }

自定义环境变量网页 favicon 图标没有配置好,注意是否 ico 图标(尺寸为 48 × 48)

Waline

✖ Project framework failed to execute:

ERROR:

Please provide existed ossBucket under your account when code size is greater than 50M.

致谢与引用声明

Hexo 优化 — Valine 扩展之邮件通知

Valine 评论之 Valine-admin 配置攻略

Valine 官方文档

Qmsg 酱-您的专属 QQ 消息推送服务小姐姐

Server 酱 | 网站小帮手