hunkier

学习笔记,开源代码,技术分享

CentOS 7 安装 go-fastdfs

CentOS 7 安装go-fastdfs文件服务

参照官网描述(https://github.com/sjqzhang/go-fastdfs),linux下go-fastdfs安装步骤如下:

下载文件

下载相关文件并赋予执行权限

1
2
3
4
$ wget https://github.com/sjqzhang/go-fastdfs/releases/download/v1.3.0/fileserver
$ wget https://raw.githubusercontent.com/sjqzhang/go-fastdfs/master/control
$ chmod 755 fileserver
$ chmod 755 control

常用命令

启动/停止/重启/查看状态/查看日志 ./control start|stop|restart|status|tail

1
2
3
4
5
6
7
8
$ ./control start
fileserver started..., pid=20599
$ ./control status
fileserver now is running, pid=1080
$ ./control stop
fileserver stoped...
$ ./control tail
Listen on :28088

测试上传

启动后可以使用命令体验上传

1
curl -F file=@http-index-fs http://10.1.xx.60:8080/upload

web上传

使用浏览器打开http://yourserver ip:8080/upload.html,注意不要使用127.0.0.1上传

修改配置

fileserver同目录下会自动生成配置文件,存放在 conf/cfg.json,需要修改几个参数,其他参数按需求修改。

1
2
3
4
5
6
7
8
9
10
"绑定端号": "端口",
"addr": ":28088",
"本主机地址": "本机http地址,默认自动生成(注意端口必须与addr中的端口一致),必段为内网,自动生成不为内网请自行修改,下同",
"host": "http://172.20.8.31:28088",
"集群": "集群列表,注意为了高可用,IP必须不能是同一个,同一不会自动备份,且不能为127.0.0.1,且必须为内网IP,默认自动生成",
"peers": ["http://172.20.8.31:28088"],
"是否自动重命名": "默认不自动重命名,使用原文件名",
"rename_file": true,
"下载域名": "用于外网下载文件的域名,不包含http://",
"download_domain": "172.20.8.31:28088",

修改完后需要重启服务

1
2
3
$ ./control restart
fileserver stoped...
fileserver started..., pid=21038

测试文件上传

1
2
$ curl -F file=@test.jpeg http://172.20.8.31:28088/upload
http://172.20.8.31:28088/group1/default/20190604/16/22/6/7d6ef01d71af04dc61cb388955478121.jpeg#

加入系统服务

新建文件gofastdfs

1
vi  /etc/rc.d/init.d/gofastdfs

文件内容为

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
#!/bin/sh
# chkconfig: 2345 80 90
#
# Simple go-fastdfs init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.

### BEGIN INIT INFO
# Provides: gofastdfs_28088
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: go-fastdfs file server
# Description: go-fastdfs file server. See https://github.com/sjqzhang/go-fastdfs
### END INIT INFO


# WORKSPACE=$(cd $(dirname $0)/; pwd)
WORKSPACE=/data1/webserver/gofastdfs/
cd $WORKSPACE

mkdir -p log conf

module=
app=fileserver
conf=conf/cfg.json
pidfile=conf/app.pid
logfile=log/app.log

function check_pid() {
if [ -f $pidfile ];then
pid=`cat $pidfile`
if [ -n $pid ]; then
running=`ps -p $pid|grep -v "PID TTY" |wc -l`
return $running
fi
fi
return 0
}

function start() {
check_pid
running=$?
if [ $running -gt 0 ];then
echo -n "$app now is running already, pid="
cat $pidfile
return 1
fi

nohup ./$app &> $logfile &
echo $! > $pidfile
echo "$app started..., pid=$!"
}

function stop() {
pid=`cat $pidfile`
kill $pid
echo "$app stoped..."
}

function restart() {
stop
sleep 1
start
}

function status() {
check_pid
running=$?
if [ $running -gt 0 ];then
echo -n "$app now is running, pid="
cat $pidfile
else
echo "$app is stoped"
fi
}

function tailf() {
tail -f $logfile
}

function build() {
go build
if [ $? -ne 0 ]; then
exit $?
fi
mv $module $app
./$app -v | grep -v "config"
}

function pack() {
build
git log -1 --pretty=%h > gitversion
version=`./$app -v|grep -v config`
file_list="control cfg.example.json $app"
tar zcf $app-$version.tar.gz gitversion $file_list
}

function packbin() {
build
git log -1 --pretty=%h > gitversion
version=`./$app -v|grep -v config`
tar zcvf $app-bin-$version.tar.gz $app gitversion
}

function help() {
echo "$0 start|stop|restart|status|tail"
}

if [ "$1" == "" ]; then
help
elif [ "$1" == "stop" ];then
stop
elif [ "$1" == "start" ];then
start
elif [ "$1" == "restart" ];then
restart
elif [ "$1" == "status" ];then
status
elif [ "$1" == "tail" ];then
tailf
else
help
fi

修改文件权限

1
chmod 755 /etc/rc.d/init.d/gofastdfs

设置开机启动

1
chkconfig gofastdfs on

启动、停止、重启、查看状态等命令

1
service gofastdfs start/stop/restart/status/tail
谢谢你请我喝牛奶

欢迎关注我的其它发布渠道