FastDFS+Nginx实现文件服务器

来源:互联网 发布:淘宝手机端优惠券设置 编辑:程序博客网 时间:2024/06/07 23:04

FastDFS–tracker安装
tracker和storage使用相同的安装包,下载地址:http://sourceforge.net/projects/FastDFS/ 或https://github.com/happyfish100/FastDFS(推荐)

本教程下载:FastDFS_v5.05.tar.gz
FastDFS安装环境:
FastDFS是C语言开发,建议在linux上运行,本教程使用Centos6.4作为安装环境。
安装FastDFS需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有gcc环境,需要安装gcc:yum install gcc-c++

安装:libevent;
FastDFS依赖libevent库,需要安装:
yum -y install libevent

安装libfastcommon:
libfastcommon是FastDFS官方提供的,libfastcommon包含了FastDFS运行所需要的一些基础库。
将libfastcommonV1.0.7.tar.gz拷贝至/usr/local/下
cd /usr/local
tar -zxvf libfastcommonV1.0.7.tar.gz
cd libfastcommon-1.0.7
./make.sh
./make.sh install

注意:libfastcommon安装好后会自动将库文件拷贝至/usr/lib64下,由于FastDFS程序引用usr/lib目录所以需要将/usr/lib64下的库文件拷贝至/usr/lib下。
要拷贝的文件如下:
这里写图片描述

tracker编译安装:
将FastDFS_v5.05.tar.gz拷贝至/usr/local/下
tar -zxvf FastDFS_v5.05.tar.gz

cd FastDFS

./make.sh
./make.sh install

安装成功将安装目录下的conf下的文件拷贝到/etc/fdfs/下。
这里写图片描述

配置:
安装成功后进入/etc/fdfs目录:
这里写图片描述
拷贝一份新的tracker配置文件:
cp tracker.conf.sample tracker.conf

修改tracker.conf
vi tracker.conf
base_path=/home/yuqing/FastDFS
改为:
base_path=/home/FastDFS

启动:
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart
启动的日志显示先停止5619进程(实际环境不是5619)再启动,如下图:
注意:如果没有显示上图要注意是否正常停止原有进程。

设置开机自动启动:

[root@tracker FastDFS]# vim /etc/rc.d/rc.local

将运行命令行添加进文件:/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart

FastDFS–storage安装:(本次测试 是在同一台centos上操作)
同tracker安装 libevent (省略)
同tracker安装 libfastcommon(省略)
同tracker编译安装 storage(省略)
storage配置:
安装成功后进入/etc/fdfs目录:
这里写图片描述
拷贝一份新的storage配置文件:
cp storage.conf.sample storage.conf

修改storage.conf
vi storage.conf
group_name=group1
base_path=/home/yuqing/FastDFS改为:base_path=/home/FastDFS

store_path0=/home/yuqing/FastDFS改为:store_path0=/home/FastDFSStorage/fdfs_storage
如果有多个挂载磁盘则定义多个store_path,如下
store_path1=…..
store_path2=……
tracker_server=192.168.101.3:22122 #配置tracker服务器:IP
如果有多个则配置多个tracker
tracker_server=192.168.101.4:22122

启动:
/usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart
启动的日志显示先停止8931进程(实际环境不是8931)再启动,如下图:
这里写图片描述
注意:如果没有显示上图要注意是否正常停止原有进程。
设置开机自动启动
[root@storage1 FastDFS]# vim /etc/rc.d/rc.local
将运行命令行添加进文件:/usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart

此时我们可以测试一下,配置是否正确:
使用格式:
/usr/bin/fdfs_test 客户端配置文件地址 upload 上传文件
例如:
将/home下的图片上传到FastDFS中:
/usr/bin/fdfs_test /etc/fdfs/client.conf upload /home/tomcat.png

http://192.168.101.3/group1/M00/00/00/wKhlBVVY2M-AM_9DAAAT7-0xdqM485_big.png就是文件的下载路径。
对应storage服务器上的
/home/fastDFSStorage/fdfs_storage/data/00/00/wKhlBVVY2M-AM_9DAAAT7-0xdqM485_big.png文件。
由于现在还没有和nginx整合无法使用http下载。

FastDFS 和nginx整合
在tracker上安装nginx:
在每个tracker上安装nginx,的主要目的是做负载均衡及实现高可用。如果只有一台tracker服务器可以不配置nginx。

在Storage上安装nginx:
使用FastDFS-nginx-module:
将FastDFS-nginx-module_v1.16.tar.gz传至/usr/local/下
cd /usr/local
tar -zxvf FastDFS-nginx-module_v1.16.tar.gz
cd FastDFS-nginx-module/src
修改config文件将/usr/local/路径改为/usr/
将FastDFS-nginx-module/src下的mod_FastDFS.conf拷贝至/etc/fdfs/下
cp mod_FastDFS.conf /etc/fdfs/

并修改mod_FastDFS.conf的内容:
vi /etc/fdfs/mod_FastDFS.conf
base_path=/home/FastDFS
tracker_server=192.168.101.3:22122

“#tracker_server=192.168.101.4:22122(多个tracker配置多行)”

url_have_group_name=true #url中包含group名称
store_path0=/home/FastDFSSrorage/fdfs_storage #指定文件存储路径

将libfdfsclient.so拷贝至/usr/lib下
cp /usr/lib64/libfdfsclient.so /usr/lib/

创建nginx/client目录
mkdir -p /var/temp/nginx/client

nginx安装:
添加FastDFS-nginx-module模块:
进入nginx源码文件夹下:
使用命令:

./configure \--prefix=/usr/local/nginx \--pid-path=/var/run/nginx/nginx.pid \--lock-path=/var/lock/nginx.lock \--error-log-path=/var/log/nginx/error.log \--http-log-path=/var/log/nginx/access.log \--with-http_gzip_static_module \--http-client-body-temp-path=/var/temp/nginx/client \--http-proxy-temp-path=/var/temp/nginx/proxy \--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \--http-scgi-temp-path=/var/temp/nginx/scgi \--add-module=/usr/local/fastdfs-nginx-module/src/

此时在使用 命令nginx:
make
make install

配置nginx:
打开nginx.conf配置文件:

cd /usr/local/nginx/conf  gedit nginx.conf 

在server节点加入下面的配置

location /group1/M00{
root /home/FastDFSStorage/fdfs_storage/data;
ngx_fastdfs_module;
}

说明:
server_name指定本机ip
location /group1/M00/:group1为nginx 服务FastDFS的分组名称,M00是FastDFS自动生成编号,对应store_path0=/home/FastDFSStorage/fdfs_storage,如果FastDFS定义store_path1,这里就是M01

启动nginx:

/usr/local/nginx/sbin/nginx  

用如下命令查看nginx是否启动了:

netstat -tupln | grep nginx  

输出如下(nginx默认监听的是80端口):

tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 9342/nginx
查看日志文件:

gedit /usr/local/nginx/logs/error.log   

输出如下:

[2017-1-23 19:29:43] ERROR - file: ../storage/trunk_mgr/trunk_shared.c, line: 177,”Permission denied” can’t be accessed, error info: /home/zq/fastdfs
2015/12/23 19:29:43 [alert] 9094#0: worker process 9095 exited with fatal code 2 and cannot be respawned
2015/12/23 19:30:58 [notice] 9112#0: signal process started
ngx_http_fastdfs_process_init pid=9196
发现前面报错了(用红色标注的部分),意思是说/home/hadoop/fastDFS文件夹不能访问,遇到这个问题首先想到的是权限不够,但是当前我是以root账号启动的啊,应该拥有最高的权限,为什么说我root却不能访问这个文件了,这个问题确实把我搞晕了,有可能nginx启动的后台线程使用的不是root用户,后来把mod_fastdfs.conf文件的base_path和store_path0改为/home/fastDFS,发现启动不报错了,这太奇怪了,这个问题折腾了我好久,上网也没找到解决的办法。后来发现nginx.conf配置的最前面里有配置user,只不过被注释掉了,把user改为root就搞定了;

最后,测试配置的nginx:
这里写图片描述

PS:今天状态 不太对,安装了半天的服务器~ 可能写的有点差;不喜勿喷

原创粉丝点击