在虚拟机上配置FastDFS

下载文件

在用户目录leyou下创建fdfs目录,并传到该目录以下四个文件

  • FastDFS_v5.08.tar.gz
  • fastdfs-nginx-module_v1.16.tar.gz
  • libevent-2.0.22-stable.tar.gz
  • libfastcommon-master.zip

安装以下内容

安装gcc依赖

1
$ yum -y install gcc

安装unzip工具,用于对压缩包进行解压

1
$ yum install -y unzip zip

安装libevent

1
$ yum -y install libevent

安装Nginx所需依赖

1
$ yum -y install pcre pcre-devel zlib zlib-devel openssl openssl-devel

安装libfastcommon-master

解压

1
$ unzip libfastcommon-master.zip

libfastcommon-master目录下使用make命令安装

1
$ ./make.sh && ./make.sh install

安装FastDFS

/home/leyou目录下解压FastDFS_v5.08.tar.gz

1
tar xvf FastDFS_v5.08.tar.gz

/home/leyou/FastDFS_v5.08目录下安装(安装不分Traker和storage,启动谁就是谁)

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

复制样例配置文件

1
cp /etc/fdfs/tracker.conf.sample /etc/fdfs/tracker.conf

编辑

1
vim /etc/fdfs/tracker.conf

修改路径(运行数据和日志目录)base_path=/leyou/fdfs/tracker

启动tracker服务

1
/etc/init.d/fdfs_trackerd /etc/fdfs/tracker.conf

tracker服务加入开机启动

1
chkconfig /etc/init.d/fdfs_trackerd on

配置storage

1
cp /etc/fdfs/storage.conf.sample /etc/fdfs/storage.conf

修改base_path、store_path0和tracker_server

base_path=/leyou/fdfs/storage
store_path0=/leyou/fdfs/storage
tracker_server=192.168.116.128:22122

启动storage

1
/etc/init.d/fdfs_storaged /etc/fdfs/storage.conf

设置storage开机启动

1
chkconfig /etc/init.id/fdfs_storaged on

测试文件上传

1
cp /etc/fdfs/client.conf.sample /etc/fdfs/client.conf

执行上传脚本

1
2
/usr/bin/fdfs_upload_file client.conf /tmp/1.jpg
group1/M00/00/00/wKh0gF6fN4SAH0KcAACCo_0btQw605.jpg

返回值:group1:组名、M00:对应store_path0、/00/00:磁盘路径

Nginx将图片请求转发到FastDFS

安装FastDFS的Nginx模块

/home/leyou目录下解压fastdfs-nginx-module_v1.16.tar.gz

1
tar xvf fastdfs-nginx-module_v1.16.tar.gz

修改配置

1
2
vim /home/leyou/fastdfs-nginx-module/src/config
执行命令 :%s+/usr/local/+/usr/+g

配置mod_fastdfs.conf

1
2
cp /home/leyou/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/
vim /etc/fdfs/mod_fastdfs.conf

修改配置
connection_timeout=10
tracker_server=192.168.116.128:22122
url_have_group_name=true
store_path0=/leyou/fdfs/storage

http相关配置

1
cp /home/leyou/FastDFS/conf/http.conf mime.types /etc/fdfs
1
2
3
4
5
6
7
8
9
10
11
12
13
[root@localhost conf]# cd /etc/fdfs
[root@localhost fdfs]# ll
总用量 80
-rw-r--r--. 1 root root 1447 4月 22 02:10 client.conf
-rw-r--r--. 1 root root 1461 4月 22 00:35 client.conf.sample
-rw-r--r--. 1 root root 858 4月 22 09:50 http.conf
-rw-r--r--. 1 root root 31172 4月 22 09:50 mime.types
-rw-r--r--. 1 root root 3686 4月 22 09:48 mod_fastdfs.conf
-rw-r--r--. 1 root root 7827 4月 22 01:59 storage.conf
-rw-r--r--. 1 root root 7829 4月 22 01:07 storage.conf.sample
-rw-r--r--. 1 root root 7101 4月 22 01:22 tracker.conf
-rw-r--r--. 1 root root 7102 4月 22 01:07 tracker.conf.sample

配置Nginx:

1
/home/leyou/nginx-1.16.1/configure --prefix=/opt/nginx --sbin-path=/usr/bin/nginx --add-module=/home/leyou/fastdfs-nginx-module/src

在/home/leyou/nginx-1.16 目录使用make命令编译

备份

1
mv /usr/bin/nginx /usr/bin/nginx-back

编译后,在/home/leyou/nginx-1.16/objs目录下生成了nginx可执行文件

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@localhost nginx-1.16.1]# cd objs/
[root@localhost objs]# ll
总用量 5220
drwxr-xr-x. 3 root root 17 4月 22 10:11 addon
-rw-r--r--. 1 root root 17385 4月 22 10:11 autoconf.err
-rw-r--r--. 1 root root 40514 4月 22 10:11 Makefile
-rwxr-xr-x. 1 root root 5202032 4月 22 10:13 nginx
-rw-r--r--. 1 root root 5317 4月 22 10:13 nginx.8
-rw-r--r--. 1 root root 7042 4月 22 10:11 ngx_auto_config.h
-rw-r--r--. 1 root root 657 4月 22 10:11 ngx_auto_headers.h
-rw-r--r--. 1 root root 5963 4月 22 10:11 ngx_modules.c
-rw-r--r--. 1 root root 45184 4月 22 10:13 ngx_modules.o
drwxr-xr-x. 9 root root 91 4月 22 01:05 src

/usr/bin/nginx替换为/home/leyou/nginx-1.16.1/objs/nginx

1
cp /home/leyou/nginx-1.16.1/objs/nginx /usr/bin

修改host

1
192.168.116.128 image.leyou.com

修改Nginx配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
server{
listen 80;
server_name image.leyou.com;
location ~/group([0-9])/{
ngx_fastdfs_module;
}
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
root html;
}

输入nginx命令重启Nginx

出现第二行ngx_http_fastdfs_set pid=6174内容表示重启成功

Nginx开机启动

Save this file as /etc/init.d/nginx

1
$ vim /etc/init.d/nginx

添加以下脚本

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
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: NGINX is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid

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

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

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

nginx="/usr/bin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/opt/nginx/conf/nginx.conf"

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx

lockfile=/var/lock/subsys/nginx

make_dirs() {
# make required directories
user=`$nginx -V 2>&1 | grep "configure arguments:.*--user=" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
if [ -n "$user" ]; then
if [ -z "`grep $user /etc/passwd`" ]; then
useradd -M -s /bin/nologin $user
fi
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
fi
}

start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}

stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}

restart() {
configtest || return $?
stop
sleep 1
start
}

reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $prog -HUP
retval=$?
echo
}

force_reload() {
restart
}

configtest() {
$nginx -t -c $NGINX_CONF_FILE
}

rh_status() {
status $prog
}

rh_status_q() {
rh_status >/dev/null 2>&1
}

case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac

nginx=”/usr/sbin/nginx” 修改成nginx执行程序的路径。
NGINX_CONF_FILE=”/etc/nginx/nginx.conf” 修改成配置文件的路径。

修改文件权限

1
chmod 755 /etc/init.d/nginx

加入服务列表

1
chkconfig --add /etc/init.d/nginx

开机启动

1
chkconfig nginx on