您的位置:首页 > 运维架构 > Nginx

centos安装FastDFS+Nginx

2017-05-15 10:00 393 查看
参考:http://blog.csdn.net/xll233xx/article/details/50619170
http://blog.csdn.net/gaofuqi/article/details/27052155 http://blog.csdn.net/pzw_0612/article/details/52573756
启动防火墙

systemctl start firewalld.service

CENTOS 7防火墙相关操作:http://www.cnblogs.com/handongyu/p/6260213.html

来自:
http://blog.csdn.net/gaofuqi/article/details/27052155
// 单台机器配置

一、在安装FastDFS之前必须先安装libevent,安装libevent步骤如下:

1.下载libevent:

wget https://github.com/downloads/libevent/libevent/libevent-2.0.21-stable.tar.gz
2.解压libevent:

tar -zxvpf libevent-2.0.21-stable.tar.gz

3.进入解压目录:

cd libevent-2.0.21-stable

4.指定安装目录:

./configure --prefix=/usr/local/libevent-2.0.21

5.安装libevent:

make

make install

二、安装FastDFS步骤如下:

1.下载FastDFS:

wget http://fastdfs.googlecode.com/files/FastDFS_v4.06.tar.gz
无法下载FASTDFS时可以:
https://code.google.com/archive/p/fastdfs/downloads
2.解压FastDFS:

tar xvf FastDFS_v4.06.tar.gz

3.进入解压目录:

cd FastDFS

4.修改FastDFS的安装文件:

vi make.sh

TARGET_PREFIX=/usr/local/FastDFS  ---安装路径  

TARGET_CONF_PATH=/etc/fdfs  ---配置文件路径  

WITH_LINUX_SERVICE=1   ---是否为Linux服务  

5.指定libevent的lib目录:

./make.sh C_INCLUDE_PATH=/usr/local/libevent-2.0.21/include LIBRARY_PATH=/usr/local/libevent-2.0.21/lib

6.安装FastDFS:

./make.sh install

7.设置软连接(否则编译nginx时会出错,64位系统  /usr/local/lib64与 32位系统/usr/local/lib的区别)

ln -sv /usr/local/FastDFS/include/fastcommon /usr/local/include/fastcommon

ln -sv /usr/local/FastDFS/include/fastdfs /usr/local/include/fastdfs

ln -sv /usr/local/FastDFS/lib/libfastcommon.so /usr/local/lib/libfastcommon.so

ln -sv /usr/local/FastDFS/lib/libfastcommon.so.1 /usr/local/lib/libfastcommon.so.1

ln -sv /usr/local/FastDFS/lib/libfdfsclient.so /usr/local/lib/libfdfsclient.so

ln -sv /usr/local/FastDFS/lib/libfdfsclient.so.1 /usr/local/lib/libfdfsclient.so.1

 

三、在安装Nginx之前,需要安装gcc、 openssl-devel、 pcre-devel和zlib-devel软件库,具体如下:

yum install gcc-c++

yum install openssl-devel

yum install pcre-devel

yum install zlib-devel

 

四、安装Nginx和fastdfs-nginx-module

wget http://nginx.org/download/nginx-1.6.0.tar.gz
wget http://fastdfs.googlecode.com/files/fastdfs-nginx-module_v1.15.tar.gz
/////////////////////////////////////////////////////////////////////////////////////////////////

(fastdfs-nginx-module,GOOGLE无法下载,可到这里下载:
https://sourceforge.net/projects/fastdfs/files/FastDFS%20Nginx%20Module%20Source%20Code/)
/////////////////////////////////////////////////////////////////////////////////////////////

将安装包文件上传到/usr/local中执行以下操作:

1.进入文件目录:

cd /usr/local

2.解压安装包:

tar -zxv -f nginx-1.6.0.tar.gz

tar -xvf fastdfs-nginx-module_v1.15.tar.gz

3.修改安装包目录名称:

mv nginx-1.6.0 nginx

4.进入安装包目录:

cd /usr/local/nginx

5.配置安装目录:

./configure --prefix=/usr/local/nginx --conf-path=/usr/local/nginx/nginx.conf  --add-module=/usr/local/fastdfs-nginx-module/src

6.安装Nginx:

make

make install

7.修改防火墙:

vi + /etc/sysconfig/iptables

8.添加配置项 

-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT

9.重启防火墙:

service iptables restart

 10.将libfastcommon.so、libfastcommon.so.1、libfdfsclient.so、libfdfsclient.so.1复制到/usr/lib,(64位系统  /usr/lib64与 32位系统/usr/lib的区别)

否则nginx启动会报错。

Nginx的操作:

启动:

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

或者

cd /usr/local/nginx/sbin

./nginx

 

停止服务:

#查询nginx主进程号 

ps -ef | grep nginx

#停止进程 

kill -QUIT 主进程号 

#快速停止 

kill -TERM 主进程号 

#强制停止 

pkill -9 nginx

 

重启:

/usr/local/nginx/sbin/nginx -s reload

 

五、配置tracker server:

1. vi /etc/fdfs/tracker.conf

bind_addr=192.168.227.128

base_path=/home/tracker/fastdfs

2.配置防火墙:

iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport 22122 -j

3.保存配置:

/etc/init.d/iptables save

4. 启动 tracker

/usr/local/FastDFS/bin/fdfs_trackerd /etc/fdfs/tracker.conf

在启动时报错:

/usr/local/bin/fdfs_trackerd: error while loading shared libraries: libevent-2.0.so.5: cannot open shared object file: No such file or directory

执行下面的命令即可正常启动:

echo '/usr/local/libevent-2.0.21/include/' >> /etc/ld.so.conf

echo '/usr/local/libevent-2.0.21/lib/' >> /etc/ld.so.conf

 ldconfig

5. 查看监听程序:

netstat -ntpl | grep fdfs

6. 查看日志文件:

cat /home/tracker/fastdfs/logs/trackerd.log

7.添加开机启动命令行:

vim /etc/rc.d/rc.local

/usr/local/FastDFS/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart

 

六、配置storage server

1. vi /etc/fdfs/storage.conf

group_name=group1

bind_addr=192.168.227.130

base_path=/home/storage/fastdfs

store_path0=/home/storage/fastdfs

tracker_server=192.168.227.130:22122

http.server.port=80

2. cp /usr/local/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/

3. vi /etc/fdfs/mod_fastdfs.conf

base_path=/home/storage/fastdfs

group_name=group1

store_path0=/home/storage/fastdfs

tracker_server=192.168.227.128:22122

url_have_group_name = true

group_count = 0

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

   location /group1/M00 {

            root /home/storage/fastdfs/data;  //这里的路径是 六、配置storage server 中的base_path 的子目录

            ngx_fastdfs_module;

}

5.配置防火墙:

//  CENTOS 7之前的版本是这样配置,之后的版本配置方法:http://www.cnblogs.com/handongyu/p/6260213.html

iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport 23000 -j ACCEPT  

6.保存配置:

/etc/init.d/iptables save  //  CENTOS 7之前的版本是这样配置

7.启动 storage  

/usr/local/FastDFS/bin/fdfs_storaged /etc/fdfs/storage.conf

8. 查看监听程序:

netstat -ntpl | grep fdfs

9. 查看日志文件:

cat /home/storage/fastdfs/logs/storaged.log

10.   vim  /etc/fdfs/client.conf

base_path=/home/client/fastdfs 

tracker_server=192.168.153.130:22122  

http.tracker_server_port=80  // 这个有必要的话也要修改下,上传之后返回的URL端口

七、测试:

 /usr/local/FastDFS/bin/fdfs_test /etc/fdfs/client.conf upload /etc/passwd

结果如下:

This is FastDFS client test program v4.06

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.

[2014-06-03 20:26:21] DEBUG - base_path=/home/client/fastdfs, connect_timeout=30, network_timeout=60, tracker_server_count=1, anti_steal_token=0, anti_steal_secret_key length=0, use_connection_pool=0, g_connection_pool_max_idle_time=3600s, use_storage_id=0,
storage server id count: 0

tracker_query_storage_store_list_without_group: 

 server 1. group_name=, ip_addr=192.168.227.130, port=23000

group_name=group1, ip_addr=192.168.227.130, port=23000

storage_upload_by_filename

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

source ip address: 192.168.227.130

file timestamp=2014-06-03 20:26:21

file size=1438

file crc32=218985275

file url: http://192.168.227.130/group1/M00/00/00/wKjjglOOkd2AdZiiAAAFng0Nczs9728851
storage_upload_slave_by_filename

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

source ip address: 192.168.227.130

file timestamp=2014-06-03 20:26:21

file size=1438

file crc32=218985275

file url: http://192.168.227.130/group1/M00/00/00/wKjjglOOkd2AdZiiAAAFng0Nczs9728851_big
 

在浏览器上打开http://192.168.227.130/group1/M00/00/00/wKjjglOOkd2AdZiiAAAFng0Nczs9728851_big

测试成功!!!

// 测试接口

#上传  

/usr/bin/fdfs_test /etc/fdfs/client.conf  upload /home/lily/a.txt  

#下载  

/usr/bin/fdfs_download_file  /etc/fdfs/client.conf group1/M00/00/00/wKiZglauvkmANC2wAAAAPpWmuhI171.txt

#删除  

/usr/bin/fdfs_delete_file  /etc/fdfs/client.conf group1/M00/00/00/wKiZglauvkmANC2wAAAAPpWmuhI171.txt

参考:http://www.cnblogs.com/zhoulf/archive/2013/02/09/2909653.html

            http://blog.csdn.net/yl_wh/article/details/8784569
// 2台机器主从配置

搭建FastDFS分布式集群环境笔记:https://my.oschina.net/xiejunbo/blog/726192

//2台以上分布式集群配置

FastDFS分布式文件系统集群安装与配置

参考:http://www.2cto.com/kf/201611/560911.html

fastDFS中使用php上传文件

参考:http://blog.csdn.net/clevercode/article/details/52334597

用以上方法出现较多问题,主要是版本之间兼容的问题,但配置文件可以用上面的配置

现下载比较新的版本编译生成rpm包

将之前的版本都卸载,安装以下版本

fastdfs-5.10-1.el7.centos.x86_64.rpm

fastdfs-debuginfo-5.10-1.el7.centos.x86_64.rpm

fastdfs-server-5.10-1.el7.centos.x86_64.rpm

fastdfs-tool-5.10-1.el7.centos.x86_64.rpm

libfastcommon-1.0.36-1.el7.centos.x86_64.rpm

libfastcommon-debuginfo-1.0.36-1.el7.centos.x86_64.rpm

libfastcommon-devel-1.0.36-1.el7.centos.x86_64.rpm

libfdfsclient-5.10-1.el7.centos.x86_64.rpm

libfdfsclient-devel-5.10-1.el7.centos.x86_64.rpm

nginx-1.12.0-1.el7.centos.ngx.x86_64.rpm

nginx-module-geoip-1.12.0-1.el7.centos.ngx.x86_64.rpm

nginx-module-image-filter-1.12.0-1.el7.centos.ngx.x86_64.rpm

nginx-module-njs-1.12.0.0.1.10-1.el7.centos.ngx.x86_64.rpm

nginx-module-perl-1.12.0-1.el7.centos.ngx.x86_64.rpm

nginx-module-xslt-1.12.0-1.el7.centos.ngx.x86_64.rpm

php-fastcommon-1.0.12-1.el7.centos.x86_64.rpm

php-fastdfs_client-5.0.9-1.el7.centos.x86_64.rpm

 php -m | grep fastdfs_client

查看是否安装成功

# php -m | grep fastdfs_client

fastdfs_client    // 有这个提示表示已经安装成功

上传页面文件 test.html

<html>

<body>

<form action="upload.php" method="post" enctype="multipart/form-data">

    <label for="file">Filename:</label>

    <input type="file" name="upFile" id="upFile" /> 

    <br />

    <input type="submit" name="submit" value="Submit" />

</form>

</body>

</html>

接收文件php

upload.php

<?php

//上传附件

function uploadAttach()                                                                            

{/*{{{*/                                                                                                  

    $ret = array();

    $ret['errorcode'] = 0;

    $ret['errormsg'] = '';

    if(!$_FILES || false == isset($_FILES["upFile"]))

    {

        $ret['errorcode'] = 1;

        $ret['errormsg'] = "ERROR:upFile is not set";

        return $ret;

    }

    $file = $_FILES["upFile"];

    if (false == isset($file['tmp_name']) || false == is_file($file['tmp_name']))

    {

        $ret['errorcode'] = 2;

        $ret['errormsg'] = "tmp_name is not file";

        return $ret;

    }

    if (0 == filesize($file['tmp_name']))

    {

        $ret['errorcode'] = 3;

        $ret['errormsg'] = "tmp_name filesize is 0";

        return $ret;

    }

    $curlFile = new CurlFile($file['tmp_name'], $file['type'], $file['name']);  // new CurlFile需要PHP 5.5以上版本,否则这一步过不了

    $fileSuffix = getSuffix($curlFile->getPostFilename());                                              

    

    $ret['file'] = $file;

    $ret['fileId'] = uploadToFastdfs($curlFile, $fileSuffix);                                                        

    return $ret;

}/*}}}*/                                                                                                  

//获取后缀

 function getSuffix($fileName) 

 {/*{{{*/

     preg_match('/\.(\w+)?$/', $fileName, $matchs);

     return isset($matchs[1])?$matchs[1]:'';

 }/*}}}*/

//上传文件到fastdfs

function uploadToFastdfs(CurlFile $file, $fileSuffix)                                                  

{/*{{{*/                                                                                                  

    $fdfs = new FastDFS(); // 

    $tracker = $fdfs->tracker_get_connection();  

    $fileId = $fdfs->storage_upload_by_filebuff1(file_get_contents($file->getFilename()), $fileSuffix);  

    $fdfs->tracker_close_all_connections();    

    return $fileId;

}/*}}}*/                                                                                                  

function start()

{

    $ret = uploadAttach();  

    print_r($ret);

}

start();

?>





需要注意的点:

// new CurlFile需要PHP 5.5以上版本

若出现[fileId]没有返回值

解决办法:

vim/etc/php.d/fastdfs_client.ini

中的fastdfs_client.base_path =

路径 改为

/etc/fdfs/client.conf 中的base_path= 
配置路径
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: