FastDFS

安装

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

# 第一步:安装gcc环境
yum install -y gcc-c++

# 第二步:安装libevent,FastDFS依赖libevent库

yum install -y libevent

# 第三步:安装libfastcommon,libfastcommon是FastDFS官方提供的包,包含了FastDFS运行所需要的一些基础库

wget -P ~/soft/ "http://ou0y29lb2.bkt.clouddn.com/libfastcommonV1.0.7.tar.gz"

# 下载fastdfs #最新版可从github上下载 https://github.com/happyfish100/fastdfs
wget -P ~/soft/ "http://ou0y29lb2.bkt.clouddn.com/fastdfs-5.05.tar.gz"

if [ ! -d "/usr/local/fastdfs" ]; then
mkdir /usr/local/fastdfs
fi


cp ~/soft/libfastcommonV1.0.7.tar.gz /usr/local/fastdfs
cp ~/soft/fastdfs-5.05.tar.gz /usr/local/fastdfs

cd /usr/local/fastdfs

tar -zxvf libfastcommonV1.0.7.tar.gz

tar -zxvf fastdfs-5.05.tar.gz

cd libfastcommon-1.0.7

./make.sh && ./make.sh install

# libfastcommon安装好后会自动将库文件拷贝至/usr/lib64下,由于FastDFS程序引用usr/lib目录,所以需要将/usr/lib64下的库文件拷贝至/usr/lib下
cp /usr/lib64/libfastcommon.so /usr/lib/


cd /usr/local/fastdfs/fastdfs-5.05

./make.sh && ./make.sh install

# --- 配置 ------
# 拷贝/usr/local/fastdfs/fastdfs-5.05/conf 目录下的文件到/etc/fdfs 目录下

if [ ! -d "/etc/fdfs" ]; then
mkdir /etc/fdfs
fi

cp /usr/local/fastdfs/fastdfs-5.05/conf/* /etc/fdfs/

cd /etc/fdfs


# ★ Tracker配置

# in mac os
#sed -i '' '/^base_path/s/^/#/' tracker.conf
#
#sed -i '' '/^#base_path/a\
#base_path=/usr/local/fastdfs/tracker
#' tracker.conf

# in linux

sed -i '/^base_path/s/^/#/' /etc/fdfs/tracker.conf

sed -i '/^#base_path/a\\base_path=\/usr\/local\/fastdfs\/tracker' /etc/fdfs/tracker.conf

# ★ Storage配置

#指定storage的组名
#group_name=group1
#base_path=/usr/local/fastdfs/storage
#store_path0=/usr/local/fastdfs/storage
#如果有多个挂载磁盘则定义多个store_path,如下
#store_path1=.....
#store_path2=......
#配置tracker服务器IP和端口
#tracker_server=192.168.242.140:22122
#如果有多个则配置多个tracker
#tracker_server=192.168.101.4:22122


filepath="/etc/fdfs/storage.conf"

ipaddr=`ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"`

names=("group_name" "base_path" "store_path0" "tracker_server")

replace_names=("group_name=group1" "base_path=\/usr\/local\/fastdfs\/storage" "store_path0=\/usr\/local\/fastdfs\/storage" "tracker_server=$ipaddr:22122")

count=${#names[*]}

for (( i=0; i<"$count"; i=i+1 ))

do

#sed -i "" "/^${names[$i]}/s/^/#/" $filepath
sed -i "/^${names[$i]}/s/^/#/" $filepath
#sed -i "" "/^#${names[$i]}/a\
#${replace_names[$i]}" $filepath
sed -i "/^#${names[$i]}/a\\${replace_names[$i]}" $filepath

done




# 创建目录
#mkdir /usr/local/fastdfs/tracker –p
#mkdir /usr/local/fastdfs/storage –p

if [ ! -d "/usr/local/fastdfs/tracker" ]; then
mkdir /usr/local/fastdfs/tracker –p
fi

if [ ! -d "/usr/local/fastdfs/storage" ]; then
mkdir /usr/local/fastdfs/storage –p
fi

ps -ef | grep tracker | awk -F " " '{if($8!="grep")print $2}' | xargs kill -9
ps -ef | grep storage | awk -F " " '{if($8!="grep")print $2}' | xargs kill -9
# --- 启动 ------
#Tracker启动命令:
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf
#Storage启动命令:
/usr/bin/fdfs_storaged /etc/fdfs/storage.conf

##############################################
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.
#设置开机自动启动:
#vim /etc/rc.d/rc.local

#将运行命令行添加进文件:
#/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf
#/usr/bin/fdfs_storaged /etc/fdfs/storage.conf
##############################################

上传图片测试

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

#第一步:进入/etc/fdfs/ 目录,拷贝一份client.conf文件
cd /etc/fdfs
cp client.conf.sample client.conf

# 第二步:修改client.conf

#base_path=/usr/local/fastdfs/client
#tracker_server=192.168.242.140:22122

ipaddr=`ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"`

sed -i '/^base_path/s/^/#/' /etc/fdfs/client.conf
sed -i '/^#base_path/a\\base_path=\/usr\/local\/fastdfs\/client' /etc/fdfs/client.conf

sed -i '/^tracker_server/s/^/#/' /etc/fdfs/client.conf
sed -i "/^#tracker_server/a\\tracker_server=$ipaddr:22122" /etc/fdfs/client.conf

mkdir -p /usr/local/fastdfs/client

###############################################

#home目录下得有tomcat.png图片...
/usr/bin/fdfs_test /etc/fdfs/client.conf upload /home/tomcat.jpg

# 上传成功的话 返回图片下载路径
# 由于现在还没有和nginx整合无法使用http下载

#http://192.168.101.3/group1/M00/00/00/wKhlBVVY2M-AM_9DAAAT7-0xdqM485_big.png就是文件的下载访问路径。
#对应storage服务器上的磁盘路径:
#/usr/local/fastdfs/storage/data/00/00/wKhlBVVY2M-AM_9DAAAT7-0xdqM485_big.png文件

FastDFS 和nginx整合

在tracker服务器上安装nginx

在每个tracker上安装nginx,的主要目的是做负载均衡及实现高可用。如果只有一台tracker服务器可以不配置nginx。

在storage服务器上安装nginx

安装fastdfs-nginx-module

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

wget -P ~/soft/ "http://ou0y29lb2.bkt.clouddn.com/fastdfs-nginx-module_v1.16.tar.gz"

cp ~/soft/fastdfs-nginx-module_v1.16.tar.gz /usr/local/fastdfs/

cd /usr/local/fastdfs

tar -zxvf fastdfs-nginx-module_v1.16.tar.gz

filepath="/usr/local/fastdfs/fastdfs-nginx-module/src/config"

sed -i "s/\/usr\/local\/include\/fastdfs/\/usr\/include\/fastdfs/g" $filepath
sed -i "s/\/usr\/local\/include\/fastcommon/\/usr\/include\/fastcommon/g" $filepath
sed -i "s/\/usr\/local\/lib/\/usr\/lib/g" $filepath

#将fastdfs-nginx-module/src下的mod_fastdfs.conf拷贝至/etc/fdfs/下
cp /usr/local/fastdfs/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/

# 修改mod_FastDFS.conf文件

#base_path=/usr/local/fastdfs/storage
#tracker_server=192.168.242.140:22122
#url中是否包含group名称
#url_have_group_name=true
#指定文件存储路径,访问时使用该路径
#store_path0=/usr/local/fastdfs/storage

modfilepath="mod_fastdfs.conf"

ipaddr=`ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"`

#ipaddr='192.168.1.101'

names=("base_path" "tracker_server" "url_have_group_name")

replace_names=("base_path=\/usr\/local\/fastdfs\/storage" "tracker_server=$ipaddr:22122" "url_have_group_name=true")

count=${#names[*]}

for (( i=0; i<"$count"; i=i+1 ))

do

sed -i "/^${names[$i]}/s/^/#/" $modfilepath

sed -i "/^#${names[$i]}/a\\${replace_names[$i]}" $modfilepath

done


#"#[group1]"
#
#"store_path0=\/usr\/local\/fastdfs\/storage"

## linux不能使用/r/n
#sed -i "s/^#\\[group1\\]/#\\[group1\\]`echo -e '\r\n'`store_path0=\/usr\/local\/fastdfs\/storage/g" $modfilepath

# 考虑到可能重复执行该脚本, 把之前设置的 注释掉
sed -ie "/^store_path0/s/^/#/" $modfilepath
sed -E -ie "/^#\[group1\]/a\\store_path0=\/usr\/local\/fastdfs\/storage" $modfilepath



# 将libfdfsclient.so拷贝至/usr/lib下
cp /usr/lib64/libfdfsclient.so /usr/lib/

# 创建nginx/client目录
if [ ! -d "/var/temp/nginx/client" ]; then
mkdir -p /var/temp/nginx/client
fi

安装nginx

参考 Linux 安装nginx

唯一不一样的地方是 configure配置

1
2
3
4
5
6
7
8
9
10
11
12
13
./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi \
--add-module=/usr/local/fastdfs/fastdfs-nginx-module/src

如果之前安装了nginx也没事, 重新编译会生成新的nginx可执行文件, 老的会自动修改成nginx.old

Alt text

编译安装

1
2
make
make install

修改nginx.conf配置文件(有坑)

/usr/local/nginx 有conf文件夹, 里面的nginx.conf 才起作用

/usr/local/nginx/nginx-1.8.0/ 也有conf文件夹, 修改这个不起作用

1
2
3
4
5
6
7
8
9
server {
listen 80;
server_name localhost;

location /group1/M00/{
#root /usr/local/fastdfs/storage/data;
ngx_fastdfs_module;
}
}