hunkier

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

CentOS 7 安装 go-fastdfs

CentOS 7 安装go-fastdfs文件服务

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

下载文件

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

$ 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

$ ./control start
fileserver started..., pid=20599
$ ./control status
fileserver now is running, pid=1080
$ ./control stop
fileserver stoped...
$ ./control tail
Listen on :28088

测试上传

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

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,需要修改几个参数,其他参数按需求修改。

"绑定端号": "端口",
"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",

修改完后需要重启服务

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

测试文件上传

$ 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

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

文件内容为

#!/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

修改文件权限

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

设置开机启动

chkconfig gofastdfs on

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

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

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