在前面一篇文章中介绍了fastfds的简单安装和文件上传功能,以及使用fastdfs自带的http下载功能等;本文中将介绍如何使用fastdfs-apache-module模块整合fastdfs和apahce;整合完毕后,客户端访问apache,apache根据配置,使用fastdfs连接tracker查询相应组内可用的stroage服务器,最后完成http请求的重定向!因而建议在每台storage服务器上都配置fastdfs和apahce的整合,各个组间配置相应的虚拟主机,还可以在前端使用nginx做负载均衡来分配用户的请求。在开始之前,需要在storage服务器上安装好apache,本例中使用编译方式安装!
1:下载fastdfs-apache-module源码
[root@www ~]# wget http://fastdfs.googlecode.com/files/fastdfs-apache-module_v1.08.tar.gz
[root@www ~]# tar -zxvf fastdfs-apache-module_v1.08.tar.gz
[root@www ~]# cd fastdfs-apache-module/src/
2:查看apache的编译路径和参数
[root@www src]# /home/web/apache/bin/apachectl -V
Server version: Apache/2.2.17 (Unix)
Server built: Apr 1 2011 06:11:57
Server's Module Magic Number: 20051115:25
Server loaded: APR 1.4.2, APR-Util 1.3.10
Compiled using: APR 1.4.2, APR-Util 1.3.10
Architecture: 32-bit
Server MPM: Prefork
threaded: no
forked: yes (variable process count)
Server compiled with....
-D APACHE_MPM_DIR="server/mpm/prefork"
-D APR_HAS_SENDFILE
-D APR_HAS_MMAP
-D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
-D APR_USE_SYSVSEM_SERIALIZE
-D APR_USE_PTHREAD_SERIALIZE
-D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
-D APR_HAS_OTHER_CHILD
-D AP_HAVE_RELIABLE_PIPED_LOGS
-D DYNAMIC_MODULE_LIMIT=128
-D HTTPD_ROOT="/home/web/apache"
-D SUEXEC_BIN="/home/web/apache/bin/suexec"
-D DEFAULT_PIDLOG="logs/httpd.pid"
-D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
-D DEFAULT_LOCKFILE="logs/accept.lock"
-D DEFAULT_ERRORLOG="logs/error_log"
-D AP_TYPES_CONFIG_FILE="conf/mime.types"
-D SERVER_CONFIG_FILE="conf/httpd.conf"
3:根据apache编译情况,修改makefile文件如下;在这里不要修改INCLUDES和LIBS参数,否则编译会报错,后面使用软连接实现!
[root@www src]# grep -v '^#' Makefile |grep -v '^$'
APACHE_BASE_PATH=/home/web/apache
builddir=.
top_srcdir=$(APACHE_BASE_PATH)
top_builddir=$(APACHE_BASE_PATH)
include $(APACHE_BASE_PATH)/build/special.mk
APXS=$(APACHE_BASE_PATH)/bin/apxs
APACHECTL=$(APACHE_BASE_PATH)/bin/apachectl
CFLAGS=-Wall
DEFS=-D_FILE_OFFSET_BITS=64 -DFDFS_OUTPUT_CHUNK_SIZE='256*1024' -DFDFS_MOD_CONF_FILENAME='"/etc/fdfs/mod_fastdfs.conf"'
INCLUDES=-I/usr/local/include/fastdfs -I/usr/local/include/fastcommon/
LIBS=-L/usr/local/lib -lfastcommon -lfdfsclient
SH_LIBS=-lfastcommon -lfdfsclient
all: local-shared-build
install: install-modules-yes
if [ ! -f /etc/fdfs/mod_fastdfs.conf ]; then cp mod_fastdfs.conf /etc/fdfs/; fi
clean:
-rm -f mod_fastdfs.o mod_fastdfs.lo mod_fastdfs.slo mod_fastdfs.la
test: reload
lynx -mime_header http://localhost/fastdfs
reload: install restart
start:
$(APACHECTL) start
restart:
$(APACHECTL) restart
stop:
$(APACHECTL) stop
4:对fastfds目录下的lib和include目录做软连接,这里出于个人习惯,喜欢把软件编译在/usr/local目录下,同时按软件名来做子目录
[root@www src]# ln -s /usr/local/FastFDS/lib/libf* /usr/local/lib
[root@www src]# ln -s /usr/local/FastFDS/include/fastdfs /usr/local/include/
[root@www src]# ln -s /usr/local/FastFDS/include/fastcommon/ /usr/local/include/
5:编译安装
[root@www src]# make
/home/web/apache/build/libtool --silent --mode=link gcc -g -O2 -pthread -Wall -lfastcommon -lfdfsclient -o
mod_fastdfs.la -rpath /home/web/apache/modules -module -avoid-version mod_fastdfs.lo
[root@www src]# make install
/home/web/apache/build/libtool --silent --mode=install cp mod_fastdfs.la /home/web/apache/modules/
make[1]: Entering directory `/root/fastdfs-apache-module/src'
make[1]: Leaving directory `/root/fastdfs-apache-module/src'
if [ ! -f /etc/fdfs/mod_fastdfs.conf ]; then cp mod_fastdfs.conf /etc/fdfs/; fi
[root@www src]# ls /home/web/apache/modules/*fast*
/home/web/apache/modules/mod_fastcgi.so
/home/web/apache/modules/mod_fastdfs.so
6:修改apache配置文件,让虚拟主机下的/M00所有请求都交给fastdfs模块处理
[root@www src]# tail /home/web/apache/conf/httpd-vhosts.conf
#test fastdfs
LoadModule fastdfs_module modules/mod_fastdfs.so
<VirtualHost *:80>
Servername img.yang.com
<Location /M00>
sethandler fastdfs
</Location>
</VirtualHost>
7:修改mod_fastdfs.conf文件,指定tracker服务器的ip和端口
[root@www src]# grep -v '^#' /etc/fdfs/mod_fastdfs.conf |grep -v '^$'
connect_timeout=2
network_timeout=30
base_path=/home/data/fastdfs
tracker_server=192.168.123.110:22122
storage_server_port=23000
group_name=group1
url_have_group_name = false
store_path_count=1
store_path0=/home/data/fastdfs
log_level=debug
log_filename=
response_mode=redirect
if_alias_prefix=
http.need_find_content_type=false
8:测试
[root@www src]# /home/web/apache/bin/apachectl -t
Syntax OK
[root@www src]# /home/web/apache/bin/apachectl -k restart
<a target="_blank" href="http://blog.51cto.com/attachment/201208/120834161.jpg"></a>
<a target="_blank" href="http://blog.51cto.com/attachment/201208/120936152.jpg"></a>
备注:
当apache服务器不为storage节点时,apache接受到的用户请求将会跳转!
[root@www fdfs]# tail -f /home/web/apache/logs/access_log
192.168.123.102 - - [01/Aug/2012:11:46:08 +0800] "GET /M00/00/00/wKh7FFAYiID1TnRyAAAA4VuHcSQ5892200 HTTP/1.1" 302 -
这个时候,若组内其他可用的storage服务器没有配置fastdfs同apache或者nginx的模块整合,则用户无法访问相应的资源,因而建议所有的storage服务器都进行整合,关于fastdfs同nginx的整合,可参考官方文档或自行google!
[root@www fdfs]# tail -f /home/web/apache/logs/error_log
本文转自斩月博客51CTO博客,原文链接http://blog.51cto.com/ylw6006/949907如需转载请自行联系原作者
ylw6006