提供了三个虚拟机:
一、 下载安装包
FastDFS 5.0.1
nginx 1.7.0
fastdfs-nginx-module_v1.16
由于使用Nginx提供文件的浏览访问功能,同时也需要安装Nginx服务。
二、安装服务
1. 增加编译环境
# yum -y install gcc gcc+ gcc-c++ openssl openssl-devel pcre pcre-deve # yum -y install pcre-devel openssl openssl-devel
2. 创建系统用户
# useradd fastdfs -M -s /sbin/nologin # useradd nginx -M -s /sbin/nologin
3. 安装FastDFS
tar -xvf FastDFS_v5.01.tar.gz cd FastDFS ./make.sh ./make.sh install
安装成功后,FastDFS会安装在/etc/fdfs目录下
4. 安装Nginx
# cd ../ # tar -xvf fastdfs-nginx-module_v1.16.tar.gz # tar -xvf nginx-1.14.0.tar.gz # cd nginx-1.14.0
# ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --add-module=../fastdfs-nginx-module/src # make # make install
注意:在Tracker Server上安装Nginx,不需要 --add-module=../fastdfs-nginx-module/src 这个配置项,而两个Storage Server的安装必须加上这个配置项。
三、Tracker Server配置
1. 创建数据及日志存放目录
mkdir -p /data/fastdfs/tracker
2. 修改tracker.conf配置
# vim /etc/fdfs/tracker.conf
更改下列两行配置:
base_path=/data/fastdfs/tracker group_name=group1
3. 修改nginx.conf配置
# vim /usr/local/nginx/conf/nginx.conf
修改完后代码如下,这是Tracker Server的一个负载均衡配置。注意这里的Tracker Server开放访问端口为84(非必须,只因为80端口已经作为其他用途)
user nginx nginx; worker_processes 4; pid /usr/local/nginx/nginx.pid; worker_rlimit_nofile 51200; events { use epoll; worker_connections 20480; } http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for" "$request_time"'; access_log /usr/local/nginx/logs/access.log main; upstream server_group1{ server 192.168.247.129; server 192.168.247.130; } server { listen 84; server_name 192.168.247.128; location /group1 { # include proxy.conf; proxy_pass http://server_group1; } } }
4. 配置Tracker Server启动程序
配置Tracker Server启动程序 ,并将其设置为随系统启动自动启动
# cp /usr/fastdfs/FastDFS/init.d/fdfs_trackerd /etc/init.d/ # chkconfig --add fdfs_trackerd # chkconfig fdfs_trackerd on
5. 配置Nginx的启动程序
使用下面指令创建一个文件
# vim /etc/init.d/nginx
然后编辑:
#!/bin/bash # nginx Startup script for the Nginx HTTP Server # it is v.0.0.2 version. # 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/sbin/nginx nginx_config=/usr/local/nginx/conf/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
保存后使用下列指令,修改为任何人可执行,并将其设定为随系统启动自动启动:
# chmod 755 /etc/init.d/nginx # chkconfig --add nginx # chkconfig nginx on
四、Storage Server配置
两台Storage都要进行下列相关的配置
1. 创建数据及日志保存目录
mkdir -p /data/fastdfs/storage/data
2. 修改storage.conf配置
vim /etc/fdfs/storage.conf
修改下列配置项,其他保持默认设置:
group_name=group1 base_path=/data/fastdfs store_path0=/data/fastdfs/storage tracker_server=192.168.247.128:22122 run_by_group=fastdfs run_by_user=fastdfs file_distribute_path_mode=1 rotate_error_log = true
3. 修改mod_fastdfs.conf配置
# cp fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/ # vim /etc/fdfs/mod_fastdfs.conf
修改下列各项配置:
connect_timeout=30 tracker_server=192.168.247.128:22122 group_name=group1 url_have_group_name = true store_path_count=1 store_path0=/data/fastdfs/storage
4. 修改nginx.conf配置
Storage Server的Nginx配置如下,监听80端口,并使用fastdfs-nginx-module模块。
vim /usr/local/nginx/conf/nginx.conf
修改内容为下:
user nginx nginx; worker_processes 4; pid /usr/local/nginx/logs/nginx.pid; worker_rlimit_nofile 1024; events { use epoll; worker_connections 1024; } http { include mime.types; server_names_hash_bucket_size 128; client_header_buffer_size 32k; large_client_header_buffers 4 32k; client_max_body_size 20m; limit_rate 1024k; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /usr/local/nginx/logs/access.log main; server { listen 80; server_name localhost; location /group1/M00{ root /data/fastdfs/storage/data; ngx_fastdfs_module; } } }
使用下列指令创建一个M00软连接,让配置中的M00同样指向data目录
ln -s /data/fastdfs/storage/data /data/fastdfs/storage/data/M00
5. 配置Storage Server的启动程序
# cp /usr/fastdfs/FastDFS/init.d/fdfs_storaged /etc/init.d/ # chkconfig --add fdfs_storaged # chkconfig fdfs_storaged on
6. 配置Nginx的启动程序
参考Tracker Server的Nginx启动配置
五、启动服务
1. 启动Tracker Server
# service fdfs_trackerd start # service nginx start
2. 启动Storage Server
# service fdfs_storaged start # service nginx start
如果报错:nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use),则使用killall -9 nginx,结束当前占用线程。
启动后,可以使用下列指令来查看各个服务的进程
# ps -ef | grep fdfs # ps -ef | grep nginx
如果可以查看到服务器进程,一般就说明已经启动成功了。
六、客户端测试
开放端口:20880、22122、23000、84、80
1. 在Tracker Server中配置一个客户端
# vim /etc/fdfs/client.conf
修改下列配置项:
# base_path=/data/fastdfs # tracker_server=192.168.247.128:22122
2. 查看服务的运行的情况
在Tracker Server使用下列指令可以查看服务的运行情况:
# fdfs_monitor /etc/fdfs/client.conf
3. 测试文件上传
# fdfs_upload_file /etc/fdfs/client.conf 01.jpg
上传成功后将返回已经保存的文件标识,它包含组名、路径和文件名:
group1/M00/76/7D/wKj3gls08kOAE5LWAAFY1Aipxfo086.jpg
4. 使用浏览器访问文件
使用上面配置的Tracker Server的Web服务端口,就可以使用下面完整的URL访问上面上传的文件:
http://192.168.247.128:84/group1/M00/76/7D/wKj3gls08kOAE5LWAAFY1Aipxfo086.jpg