/ 中存储网

Ubuntu系统下安装配置FastDFS+Nginx

2013-11-20 09:01:02 来源:itjs.cn
一、部署说明

部署环境ubuntu 12.0.4 server 32位or 64位

最简安装,除ssh服务不安装任何服务

服务器初始化

sudo apt-get update

sudo apt-get upgrade

sudo apt-get dist-upgrade

安装编译环境:

apt-get install build-essential

dpkg-reconfigure tzdata

然后在crontab设置服务器时间同步

二、安装libevent

官方推荐稳定的1.4.14b版本的,2.x的可能有不稳定性

获取下载

wget https://github.com/downloads/libevent/libevent/libevent-1.4.14b-stable.tar.gz

解压并编译安装

tar zvxf libevent-1.4.14b-stable.tar.gz

./configure --prefix=/usr

make clean;

make

sudo make install

获取下载

wget http://fastdfs.googlecode.com/files/FastDFS_v4.04.tar.gz

解压并编译安装

tar zvxf FastDFS_v4.04.tar.gz

先修改相关文件:

1:修改make.sh

查找:

#WITH_HTTPD=1

修改成:

WITH_HTTPD=1

查找:

if [ -f /usr/lib/libpthread.so ] || [ -f /usr/local/lib/libpthread.so ] || [ -f /lib64/libpthread.so ] || [ -f /usr/lib64/libpthread

.so ] || [ -f /usr/lib/libpthread.a ] || [ -f /usr/local/lib/libpthread.a ] || [ -

f /lib64/libpthread.a ] || [ -f /usr/lib64/libpthread.a ] ;

替换成:(以你服务器的实际路径为准)

if [ -f /usr/lib/libpthread.so ] || [ -f /usr/local/lib/libpthread.so ] || [ -f /lib64/libpthread.so ] || [ -f /usr/lib64/libpthread

.so ] || [ -f /usr/lib/x86_64-linux-gnu/libpthread.so ] || [ -f /usr/lib/libpthread.a ] || [ -f /usr/local/lib/libpthread.a ] || [ -

f /lib64/libpthread.a ] || [ -f /usr/lib64/libpthread.a ] || [ -f /usr/lib/x86_64-linux-gnu/libpthread.a ];

2:修改client/fdfs_link_library.sh.in(如果是64位os,32位os无需修改)

查找:

ln -fs $TARGET_LIB/libfastcommon.so.1 /usr/lib64/libfastcommon.so

ln -fs $TARGET_LIB/libfdfsclient.so.1 /usr/lib64/libfdfsclient.so

替换成:

ln -fs $TARGET_LIB/libfastcommon.so.1 /usr/lib/x86_64-linux-gnu/libfastcommon.so

ln -fs $TARGET_LIB/libfdfsclient.so.1 /usr/lib/x86_64-linux-gnu/libfdfsclient.so

3:进入解压后目录,编译并安装:

sudo ./make.sh

sudo ./make.sh install

四、配置及启动Tracker Server 修改tracker.conf

FastDFS默认装完会在/etc/fdfs/下产生配置文件tracker.conf

port=22122

base_path=/www/geebook_tracker

http.server_port=8090

#include http.conf

其它保持默认,注意上面那个是1个#,默认是2个#,去掉1个就行

启动tracker服务

sudo make /www/geebook_tracker

sudo fdfs_trackerd /etc/fdfs/tracker.conf

netstat –lnp –tcp 参看端口是否起来,默认如果显示22122和8090说明服务正常起来

五、配置及启动Storage Server 修改storage.conf

FastDFS默认装完会在/etc/fdfs/下产生配置文件storage.conf

port=23000

base_path=/www/geebook_storage

#base_path,指定data和log所在目录,根目录必须存在,子目录会自动创建

store_path0=/www/geebook_storage

#文件的存储位置,在一台Storage Server上可以指定多个存储位置

group_name=group1

tracker_server=127.0.0.1:22122

http.server_port=8888

#include http.conf

启动storage服务

sudo mkdir /www/geebook_storage

sudo fdfs_storaged /etc/fdfs/storage.conf

netstat –lnp –tcp 参看端口是否起来,默认如果显示23000和8888说明服务正常起来

修改client.conf

FastDFS默认装完会在/etc/fdfs/下产生配置文件client.conf

base_path=/www/geebook_client

tracker_server=127.0.0.1:22122

http.tracker_server_port=8090

测试上传

fdfs_test /etc/fdfs/client.conf upload /etc/fstab

Copyright (C) 2008, Happy Fish / YuQing

FastDFS may be copied only under the terms of the GNU General

Public License V3, which may be found in the FastDFS source kit.

Please visit the FastDFS Home Page http://www.csource.org/

for more detail.

[2012-12-27 14:27:01] DEBUG - base_path=/www/geebook_client, connect_timeout=30, network_timeout=60, tracker_server_count=1, anti_steal_token=0, anti_steal_secret_key length=0

tracker_query_storage_store_list_without_group:

server 1. group_name=group1, ip_addr=127.0.0.1, port=23000

group_name=group1, ip_addr=127.0.0.1, port=23000

storage_upload_by_filename

group_name=group1, remote_filename=M00/00/00/OlPQG1Db6jWAFmbTAAADqdgNsks5096460

source ip address: 58.83.208.27

file timestamp=2012-12-27 14:27:01

file size=937

file crc32=3624776267

file url: http://127.0.0.1:8090/group1/M00/00/00/OlPQG1Db6jWAFmbTAAADqdgNsks5096460

storage_upload_slave_by_filename

group_name=group1, remote_filename=M00/00/00/OlPQG1Db6jWAFmbTAAADqdgNsks5096460_big

source ip address: 58.83.208.27

file timestamp=2012-12-27 14:27:01

file size=937

file crc32=3624776267

file url: http://127.0.0.1:8090/group1/M00/00/00/OlPQG1Db6jWAFmbTAAADqdgNsks5096460_big

直接访问http://127.0.0.1:8090/group1/M00/00/00/OlPQG1Db6jWAFmbTAAADqdgNsks5096460_big 正常说明上传成功,并且fastdfs正常运行

删除测试

/etc/fdfs/client.conf delete group1 M00/00/00/OlPQG1Db6jWAFmbTAAADqdgNsks5096460_big

This is FastDFS client test program v4.04

Copyright (C) 2008, Happy Fish / YuQing

FastDFS may be copied only under the terms of the GNU General

Public License V3, which may be found in the FastDFS source kit.

Please visit the FastDFS Home Page http://www.csource.org/

for more detail.

[2012-12-27 14:31:42] DEBUG - base_path=/www/geebook_client, connect_timeout=30, network_timeout=60, tracker_server_count=1, anti_steal_token=0, anti_steal_secret_key length=0

storage=58.83.208.27:23000

delete file success

显示删除成功

七、FastDFS和nginx整合

因为FastDFS默认自带的http服务器性能不好,所以一般建议用外置的apache或者nginx来解决http下载,以应付大并发的情况

注意nginx扩展模块只支持GET和HEAD模式获取文件,需要开发那边配合修改程序

获取nginx和FastDFS的nginx扩展插件

wget http://fastdfs-nginx-module.googlecode.com/files/fastdfs-nginx-module_v1.13.tar.gz

wget http://nginx.org/download/nginx-1.2.6.tar.gz

解压并编译安装

apt-get install libssl-dev zlib1g-dev libpcre3-dev

tar zvxf nginx-1.2.6.tar.gz

tar zvxf fastdfs-nginx-module_v1.13.tar.gz

cd nginx-1.2.6

./configure --prefix=/usr/local/nginx --with-http_gzip_static_module --add-module=/www/soft/fastdfs-nginx-module/src/

make

make install

修改相关配置文件

修改FastDFSstorage.conf

http.disabled=true

#关闭内置web server

其它保持不变,保存退出

修改nginx扩展模块的配置文件

cd ../fastdfs-nginx-module/src

cp mod_fastdfs.conf /etc/fdfs/

vi /etc/fdfs/mod_fastdfs.conf

base_path=/www/logs

#存放log的路径

tracker_server=127.0.0.1:22122

#指定tracker服务器及端口

url_have_group_name = true

#这个很重要,在URL中包含group名称

store_path0=/www/geebook_storage

#存储文件的路径

storage_server_port=23000

#与storage的配置端口保持一致

保存后退出

M00的链接

ln -s /www/geebook_storage/data /www/geebook_storage/data/M00

修改nginx的配置文件

vi /usr/local/nginx/conf/nginx.conf

server {

listen 8888;

location / {

root /www/geebook_storage/data;

index index.html index.htm;

}

location /group1/M00 {

root /www/geebook_storage/data;

ngx_fastdfs_module;

}

}

重启相关服务,验证整合是否成功

先重启storage服务:

/etc/init.d/fdfs_storaged start

然后再启动nginx,注意顺序,否则会报端口占用的错误

/usr/local/nginx/sbin/nginx

查看端口使用情况:

netstat -lnp --tcp

tcp 0 0 0.0.0.0:23000 0.0.0.0:* LISTEN 1761/fdfs_storaged

tcp 0 0 0.0.0.0:8888 0.0.0.0:* LISTEN 1718/nginx

tcp 0 0 0.0.0.0:8090 0.0.0.0:* LISTEN 1809/fdfs_trackerd

tcp 0 0 0.0.0.0:22122 0.0.0.0:* LISTEN 1809/fdfs_trackerd

默认的8888端口本来是storage的,现在被nginx替代

完成了FastDFS和nginx的整合

用上面的fdfs_test程序测试上传和下载,能正常访问下载,说明nginx已经顺利替代FastDFS的内置Web server。