FastDFS环境配置及php FastDFS扩展安装
FastDFS是一个开源的轻量级分布式文件系统,它对文件进行管理,功能包括:文件存储、文件同步、文件访问(文件上传、文件下载)等,解决了大容量存储和负载均衡的问题。特别适合以文件为载体的在线服务,如相册网站、视频网站等等。详细介绍请自行百度(www.baidu.com)。
1
接下来我们就来具体配置和安装,我的服务器是LNMP
在此之前首先要确定LAMP或LNMP环境已经搭建好
下载Fast DFS
下载libfastcommon(后面会用到)
libfastcommon安装
root@test:~# wget https://github.com/happyfish100/libfastcommon/archive/master.zip
root@test:~# unzip master.zip
root@test:~# cd libfastcommon-master
root@test:~/libfastcommon-master# ./make.sh
…
root@test:~/libfastcommon-master# ./make.sh install
…
FastFDS安装
root@test:~# wget https://github.com/happyfish100/fastdfs/archive/master.zip
root@test:~# unzip fastdfs-master.zip
root@test:~# cd fastdfs-master.zip
root@test:~/fastdfs-master# ./make.sh
…
root@test:~/fastdfs-master# ./make.sh install
…
至此FastFDS已经成功安装到服务器上了,接下来就需要做一些配置
FastDFS配置
root@test:~# cd /etc/fdfs/
root@test:/etc/fdfs/# ls
client.conf.sample storage.conf.sample tracker.conf.sample
root@test:/etc/fdfs/# cp client.conf.sample client.conf
root@test:/etc/fdfs/# cp storage.conf.sample storage.conf
root@test:/etc/fdfs/# cp tracker.conf.sample tracker.conf
root@test:/etc/fdfs/# find ~/fastdfs-master/ -name http.conf
/root/fastdfs-master/conf/http.conf
root@test:/etc/fdfs/# cp /root/fastdfs-master/conf/http.conf /etc/fdfs/http.conf
root@test:/etc/fdfs/# vim tracker.conf
在tracker.conf中找到
the base path to store data and log files
base_path=/home/yuqing/fastdfs
…
http.server_port=80
the base path to store data and log files
base_path=/data/fastdfs
…
http.server_port=8088
保存退出,如果80端口不被占用可以不做更改
并在根目录下新建data文件夹和data文件夹下的字文件夹fastdfs
root@test:/etc/fdfs/# mkdir /data
root@test:/etc/fdfs/# mkdir /data/fastdfs
root@test:/etc/fdfs/# vim storage.conf
在storage.conf中找到
the base path to store data and log files
base_path=/home/yuqing/fastdfs
tracker_server can ocur more than once, and tracker_server format is
“host:port”, host can be hostname or ip address
tracker_server=192.168.1.36:22122
store_path#, based 0, if store_path0 not exists, it’s value is base_path
the paths must be exist
store_path0=/home/yuqing/fastdfs
…
http.server_port=80
改为
the base path to store data and log files
base_path=/data/fastdfs
tracker_server can ocur more than once, and tracker_server format is
“host:port”, host can be hostname or ip address
tracker_server=192.168.1.36:22122
store_path#, based 0, if store_path0 not exists, it’s value is base_path
the paths must be exist
store_path0=/data/fastdfs
…
http.server_port=8088
其中tracker_server部分的IP为自己的tracker服务器IP,如果没有改成localhost也是可以的
FastDFS启动
接下来就是激动人心的时刻了,输入以下命令来启动服务
root@test:~# /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf start
root@test:~# /usr/bin/fdfs_storaged /etc/fdfs/storage.conf start
1
2
通过ps -ef | grep fdfs来查看fdfs进程是否存在
root@test:~# ps -ef | grep fdfs
root 2973 1 0 11:16 ? 00:00:00 /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf start
root 2988 1 0 11:16 ? 00:00:00 /usr/bin/fdfs_storaged /etc/fdfs/storage.conf start
root 2993 2803 0 11:17 pts/0 00:00:00 grep fdfs
测试上传
root@test:~# cd /etc/fdfs/
root@test:/etc/fdfs/# vim client.conf
在client.conf中找到
the base path to store log files
base_path=/home/yuqing/fastdfs
tracker_server can ocur more than once, and tracker_server format is
“host:port”, host can be hostname or ip address
tracker_server=192.168.0.197:22122
…
http.tracker_server_port=80
…
##include http.conf
改为
the base path to store log files
base_path=/data/fastdfs
tracker_server can ocur more than once, and tracker_server format is
“host:port”, host can be hostname or ip address
tracker_server=192.168.0.197:22122
…
http.tracker_server_port=8088
…
#include http.conf
这里的端口号http.tracker_server_port=8088一定要和tracker.conf中的端口号一致
root@test:~# echo ‘fastDFS_test’ >/tmp/test.txt
root@test:~# fdfs_test /etc/fdfs/client.conf upload /tmp/test.txt
…
example file url: http://114.215.131.59:8088/group1/M00/00/00/cteDO1cCB1eAFeqeAAAADVt_Kt4115_big.txt
如果出现最下面的URL则说明环境搭建成功
php FastDFS扩展安装
root@test:~# cd /fastdfs/php_client/
root@test:/fastdfs/php_client/# phpize
Configuring for:
PHP Api Version: 20100412
Zend Module Api No: 20100525
Zend Extension Api No: 220100525
root@test:/fastdfs/php_client/# ./configure --with-php-config=/usr/bin/php-config
checking for grep that handles long lines and -e… /bin/grep
…
config.status: creating config.h
config.status: executing libtool commands
root@test:/fastdfs/php_client/# make
…
Build complete.
Don`t forget to run ‘make test’.
root@test:/fastdfs/php_client/# make install
Installing shared extensions: /usr/lib/php5/20100525/
root@test:/fastdfs/php_client/# cat /root/fastdfs/php_client/fastdfs_client.ini >> /etc/php5/fpm/php.ini
至此php FastDFS扩展已经安装完成,重启服务器后进行测试
php FastDFS测试验证
root@test:~# /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf start
root@test:~# /usr/bin/fdfs_storaged /etc/fdfs/storage.conf start
root@test:~# cp /root/fastdfs/php_client/fastdfs_test.php /usr/share/nginx/www/
然后用浏览器打开,http://远端服务器地址/fasrdfs_test.php
如果出现
5.08 fastdfs_tracker_make_all_connections result: 1 array(1) { [“group1”]=> array(12) { [“total_space”]=> int(80632) … { [“color”]=> string(6) “yellow” [“font”]=> string(4) “Aris” [“size”]=> string(2) “32” } delete file group1/M00/00/00/cteDO1cCjI-ATDFJAAAAD61kmgs438.bin return: 1 bool(true) tracker_close_all_connections result: 1
则表示整个扩展已经安装完成并可以使用了。
常见错误及解决办法
浏览器端测试时出现
5.08 fastdfs_tracker_make_all_connections result: bool(false) bool(false)
重启服务器后未启动storage和tracker服务,解决办法:
root@test:~# /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf start
root@test:~# /usr/bin/fdfs_storaged /etc/fdfs/storage.conf start
安装FastFDS时出现
…/common/fdfs_global.c:20:20: fatal error: logger.h: No such file or directory
未安装libfastcommon,解决办法:
root@test:~# wget https://github.com/happyfish100/libfastcommon/archive/master.zip
root@test:~# unzip master.zip
root@test:~# cd libfastcommon-master
root@test:~/libfastcommon-master# ./make.sh
…
root@test:~/libfastcommon-master# ./make.sh install
…
测试上传时出现
ERROR - file: ini_file_reader.c, line: 394, include file “http.conf” not exists, line: “#include http.conf”
1
虽然引入了http.conf文件,但在指定配置目录下并未找到,解决办法:
root@test:/etc/fdfs/# find ~/fastdfs-master/ -name http.conf
/root/fastdfs-master/conf/http.conf
root@test:/etc/fdfs/# cp /root/fastdfs-master/conf/http.conf /etc/fdfs/http.con