FastDFS+nginx服务搭建与配置

FastDFS有很多个版本,如果安装的是5.05以下的版本则首先需要安装libevent以及libfastcommon,如果安装的是5.05以上的版本(包含5.05)则不需要安装libevent,仅需要安装libfastcommon-master(与libfastcommon不同,里面多了很多头文件),如果安装的FastDFS5.05 和libcommon则会报connection_pool.h等等很多头文件找不到错误。在这里我安装的是FastDFS-5.03版本的,所以首先得安装libevent.
进入libevent解压目录执行 ./configure && make 发现make 的时候报错了

fatal error: openssl/bio.h: No such file or directory

openssl/bio.h 头文件找不到。注意 :一般编译的时候编译器会自动的在/usr/include 目录下寻找相关的头文件,可以直接把你安装的头文件复制到/usr/include目录下。我的openssl安装在/usr/local/ssl目录下头文件存放于/usr/local/ssl/include/openssl/下,直接拷贝 cp -r /usr/local/ssl/include/openssl /usr/include 或者建立软连接 ln -s /usr/local/ssl/include/openssl /usr/include/openssl

root@ubuntu:/usr/fastdfs/libevent-2.0.22-stable# ln -s /usr/local/ssl/include/openssl/ /usr/include/openssl
root@ubuntu:/usr/fastdfs/libevent-2.0.22-stable# 

再次make报错

/usr/fastdfs/libevent-2.0.22-stable/sample/le-proxy.c:256: undefined reference to `SSL_library_init'
/usr/fastdfs/libevent-2.0.22-stable/sample/le-proxy.c:257: undefined reference to `ERR_load_crypto_strings'
/usr/fastdfs/libevent-2.0.22-stable/sample/le-proxy.c:258: undefined reference to `SSL_load_error_strings'
/usr/fastdfs/libevent-2.0.22-stable/sample/le-proxy.c:259: undefined reference to `OPENSSL_add_all_algorithms_noconf'
/usr/fastdfs/libevent-2.0.22-stable/sample/le-proxy.c:265: undefined reference to `SSLv23_method'
collect2: error: ld returned 1 exit status
make[2]: *** [le-proxy] Error 1
make[2]: Leaving directory `/usr/fastdfs/libevent-2.0.22-stable/sample'

找了半天资料也没能解决这个问题,希望有知道的同学能给我一些指导
但是这个错误并不会影响libevent的安装直接只会在安装FastDFS时怂了一些*conf.samp配置文件,接着make install

安装libfastcommon,进入libfastcommon目录执行 ./make.sh 和./make.sh isntall 命令部分./make.sh install 命令执行代码如下

cp -f libfastcommon.so.1 /usr/local/lib/
mkdir -p /usr/local/include/fastcommon
cp -f common_define.h hash.h chain.h logger.h base64.h shared_func.h pthread_func.h ini_file_reader.h _os_bits.h sockopt.h sched_thread.h http_func.h md5.h local_ip_func.h /usr/local/include/fastcommon
ln -fs /usr/local/lib/libfastcommon.so.1 /usr/local/lib/libfastcommon.so
ln -fs /usr/local/lib/libfastcommon.so.1 /usr/lib/libfastcommon.so
root@ubuntu:/usr/fastdfs/libfastcommon# 

可以看到 头文件放在了/usr/local/include/fastcommon文件夹下,libfastcommon.so库放在了/usr/local/lib文件夹下但是在/usr/lib下建立了软连接

安装FastDFS-5.03 ,进入解压缩目录执行./make.sh 和 ./make.sh install 命令。如果报错

fatal error: fastcommon/logger.h: No such file or directory

可以将/usr/local/inclued/fastcommon 文件夹拷贝到/usr/include 文件夹下,或者建立软连接 ln -s /usr/local/include/fastcommon /usr/include/fastcommon。安装完成之后会在/usr/local/bin下生成很多个fdfs_*文件

root@ubuntu:/usr/local/bin# ll fdfs_*
-rwxr-xr-x 1 root root  811263 Oct 30 18:04 fdfs_append_file*
-rwxr-xr-x 1 root root  829810 Oct 30 18:04 fdfs_appender_test*
-rwxr-xr-x 1 root root  829355 Oct 30 18:04 fdfs_appender_test1*
-rwxr-xr-x 1 root root  811535 Oct 30 18:04 fdfs_crc32*
-rwxr-xr-x 1 root root  811231 Oct 30 18:04 fdfs_delete_file*
-rwxr-xr-x 1 root root  812073 Oct 30 18:04 fdfs_download_file*
-rwxr-xr-x 1 root root  812245 Oct 30 18:04 fdfs_file_info*
-rwxr-xr-x 1 root root  831128 Oct 30 18:04 fdfs_monitor*
-rwxr-xr-x 1 root root 1761013 Oct 30 18:04 fdfs_storaged*
-rwxr-xr-x 1 root root  843393 Oct 30 18:04 fdfs_test*
-rwxr-xr-x 1 root root  841962 Oct 30 18:04 fdfs_test1*
-rwxr-xr-x 1 root root  957451 Oct 30 18:04 fdfs_trackerd*
-rwxr-xr-x 1 root root  811973 Oct 30 18:04 fdfs_upload_appender*
-rwxr-xr-x 1 root root  813409 Oct 30 18:04 fdfs_upload_file*
root@ubuntu:/usr/local/bin# root@ubuntu:

同时也会在/etc/fdfs/目录下生成很多配置文件

root@ubuntu:/etc/fdfs# ll
total 72
drwxr-xr-x   2 root root  4096 Oct 30 16:04 ./
drwxr-xr-x 130 root root 12288 Oct 30 17:35 ../
-rw-r--r--   1 root root  1465 Oct 30 16:04 client.conf
-rw-r--r--   1 root root   858 Oct 30 15:20 http.conf
-rw-r--r--   1 root root 31172 Oct 30 15:20 mime.types
-rw-r--r--   1 root root  7543 Oct 30 15:57 storage.conf
-rw-r--r--   1 root root  6994 Oct 30 15:47 tracker.conf

如果没有生成fdfs文件夹,可以在/etc下新建fdfs文件夹,将FastDFS-5.03源码包目录中的src目录下的conf文件夹下的文件拷贝到/etc/fdfs/下

安装完成,修改配置文件。首先修改tracker.conf

root@ubuntu:/etc/fdfs# vi /etc/fdfs/tracker.con
 #the base path to store data and log files
base_path=/home/wl/fastdfs/tracker

base_path为tracker的数据与日志文件存放目录,必须存在。保存退出,启动tracker 注意要指定配置文件

root@ubuntu:/usr/local/bin# fdfs_trackerd  /etc/fdfs/tracker.conf
root@ubuntu:/usr/local/bin# ps aux | grep tracker
root      4686  0.0  0.2 142436  2572 ?        Sl   15:48   0:02 /usr/local/bin/fdfs_trackerd /etc/fdfs/tracker.conf
root     16216  0.0  0.0   8864   648 pts/12   S+   18:43   0:00 grep --color=auto tracker
root@ubuntu:/usr/local/bin#

修改storage.conf配置文件

# the base path to store data and log files
base_path=/home/wl/fastdfs/storage
# store_path#, based 0, if store_path0 not exists, it's value is base_path
# the paths must be exist
store_path0=/home/wl/fastdfs/storage
#store_path1=/home/yuqing/fastdfs2
# 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.10.229:22122

base_path为日志文件的存放目录
store_path0为静态资源,如图片、安装包等的存放路径。注意可以指定多个路径
tracker_server为开启tracker_server的服务器的ip地址或域名。也可以指定多个
启动storage 指定配置文件

root@ubuntu:~# /usr/local/bin/fdfs_storaged /etc/fdfs/storage.conf
root@ubuntu:~# ps aux | grep storage
root      4766  0.0  6.5 144560 66500 ?        Sl   17:00   0:03 /usr/local/bin/fdfs_storaged /etc/fdfs/storage.conf
root     16585  0.0  0.0   8864   644 pts/3    S+   20:40   0:00 grep --color=auto storage
root@ubuntu:~#

完成后会在 store_path0 目录下新建非常多的子目录用来存放 资源文件

文件上传测试 更改/etc/fdfs/client.conf

root@ubuntu:~# vi /etc/fdfs/client.conf

# the base path to store log files
base_path=/home/wl/fastdfs/client

# 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.10.229:22122

上传测试执行/usr/local/bin/fdfs_test /etc/fdfs/client.conf upload /usr/fastdfs/FastDFS/conf/anti-steal.jpg

root@ubuntu:~# /usr/local/bin/fdfs_test /etc/fdfs/client.conf upload /usr/fastdfs/FastDFS/conf/anti-steal.jpg
This is FastDFS client test program v5.03

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.

[2016-10-30 21:29:59] DEBUG - base_path=/home/wl/fastdfs/client, 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.10.229, port=23000

group_name=group1, ip_addr=192.168.10.229, port=23000
storage_upload_by_filename
group_name=group1, remote_filename=M00/00/00/wKgK5VgV9deACTQtAABdrZgsqUU607.jpg
source ip address: 192.168.10.229
file timestamp=2016-10-30 21:29:59
file size=23981
file crc32=2553063749
example file url: http://192.168.10.229/group1/M00/00/00/wKgK5VgV9deACTQtAABdrZgsqUU607.jpg
storage_upload_slave_by_filename
group_name=group1, remote_filename=M00/00/00/wKgK5VgV9deACTQtAABdrZgsqUU607_big.jpg
source ip address: 192.168.10.229
file timestamp=2016-10-30 21:29:59
file size=23981
file crc32=2553063749
example file url: http://192.168.10.229/group1/M00/00/00/wKgK5VgV9deACTQtAABdrZgsqUU607_big.jpg
root@ubuntu:~#

图片目录

root@ubuntu:/home/wl/fastdfs/storage/data/00/00# ll
total 64
drwxr-xr-x   2 root root  4096 Oct 30 21:29 ./
drwxr-xr-x 258 root root  4096 Oct 30 15:58 ../
-rw-r--r--   1 root root 23981 Oct 30 21:29 wKgK5VgV9deACTQtAABdrZgsqUU607.jpg
-rw-r--r--   1 root root    49 Oct 30 21:29 wKgK5VgV9deACTQtAABdrZgsqUU607.jpg-m
-rw-r--r--   1 root root 23981 Oct 30 21:29 wKgK5VgV9deACTQtAABdrZgsqUU607_big.jpg
-rw-r--r--   1 root root    49 Oct 30 21:29 wKgK5VgV9deACTQtAABdrZgsqUU607_big.jpg-m
root@ubuntu:/home/wl/fastdfs/storage/data/00/00#

此时通过http并不能访问该图片,需要安装配置fastdfs-nginx-module以及nginx。若之前已经安装nginx 需要重新编译
进入fastdfs-nginx-module解压目录下的src目录下 修改conf文件,原conf文件如下

ngx_addon_name=ngx_http_fastdfs_module
HTTP_MODULES="$HTTP_MODULES ngx_http_fastdfs_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_fastdfs_module.c"
CORE_INCS="$CORE_INCS /usr/local/include/fastdfs /usr/local/include/fastcommon/"
 # fastcommon 为libfastcommon 头文件安装目录
 # fastdfs  为FastDFS 头文件的安装目录
 # 注意fastcommon fastdfs可能安装在/usr/inclued目录下
 # 如果你的fastcommon fastdfs  不在/usr/local/include目录下,请将上面路径中的local去掉
CORE_LIBS="$CORE_LIBS -L/usr/local/lib -lfastcommon -lfdfsclient"
# lib路径同上 我的安装路径都是在/usr/local/lib 和/usr/local/include下所以并不需要更改
CFLAGS="$CFLAGS -D_FILE_OFFSET_BITS=64 -DFDFS_OUTPUT_CHUNK_SIZE='256*1024' -DFDFS_MOD_CONF_FILENAME='\"/etc/fdfs/mod_fastdfs.conf\"'"

重新编译nginx—进入nginx源码目录–如果你没有安装nginx 可以参考我的另一篇博文 《nginx的安装》
执行./configure –add-module=/usr/fastdfs/fastdfs-nginx-module/src
/usr/fastdfs/fastdfs-nginx-module 目录为fastdfs-nginx-module 源码目录

编译部分代码如下 表示一些文件的存放路径

./configure --add-module=/usr/fastdfs/fastdfs-nginx-module/src  

adding module in /usr/fastdfs/fastdfs-nginx-module/src
 + ngx_http_fastdfs_module was configured
checking for PCRE library ... found
checking for PCRE JIT support ... found

 nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

make && make install 完毕

将/usr/fastdfs/fastdfs-nginx-module/src 目录下 mod_fastdfs.conf文件复制到/etc/fdfs目录下

root@ubuntu:/usr/fastdfs/fastdfs-nginx-module/src# cp mod_fastdfs.conf  /etc/fdfs/

修改该配置文件(部分配置如下)

root@ubuntu:~# vi /etc/fdfs/mod_fastdfs.conf

# the base path to store log files
base_path=/tmp

# FastDFS tracker_server can ocur more than once, and tracker_server format is
#  "host:port", host can be hostname or ip address
# valid only when load_fdfs_parameters_from_tracker is true
tracker_server=192.168.10.229:22122

# if the url / uri including the group name
# set to false when uri like /M00/00/00/xxx
# set to true when uri like ${group_name}/M00/00/00/xxx, such as group1/M00/xxx
# default value is false
url_have_group_name = true

# store_path#, based 0, if store_path0 not exists, it's value is base_path
# the paths must be exist
# must same as storage.conf
store_path0=/home/wl/fastdfs/storage
#store_path1=/home/yuqing/fastdfs1

注意:如果此时重启nginx会发现 nginx的主进程启动但是工作进程却并未启动

还需要修改/usr/local/nginx/conf/nginx.conf文件如下

server{
 listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location /group1/M00/{
           ngx_fastdfs_module;

        }
}

重启nginx

root@ubuntu:/usr/local/nginx# sbin/nginx -s stop
ngx_http_fastdfs_set pid=3391
root@ubuntu:/usr/local/nginx# sbin/nginx
ngx_http_fastdfs_set pid=3392
root@ubuntu:/usr/local/nginx#

再上传一张图片

root@ubuntu:~# /usr/local/bin/fdfs_test /etc/fdfs/client.conf upload /usr/fastdfs/FastDFS/conf/anti-steal.jpg
This is FastDFS client test program v5.03

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.

[2016-10-31 00:30:00] DEBUG - base_path=/home/wl/fastdfs/client, 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.10.229, port=23000

group_name=group1, ip_addr=192.168.10.229, port=23000
storage_upload_by_filename
group_name=group1, remote_filename=M00/00/00/wKgK5VgWIAiAEiODAABdrZgsqUU375.jpg
source ip address: 192.168.10.229
file timestamp=2016-10-31 00:30:00
file size=23981
file crc32=2553063749
example file url: http://192.168.10.229/group1/M00/00/00/wKgK5VgWIAiAEiODAABdrZgsqUU375.jpg
storage_upload_slave_by_filename
group_name=group1, remote_filename=M00/00/00/wKgK5VgWIAiAEiODAABdrZgsqUU375_big.jpg
source ip address: 192.168.10.229
file timestamp=2016-10-31 00:30:00
file size=23981
file crc32=2553063749
example file url: http://192.168.10.229/group1/M00/00/00/wKgK5VgWIAiAEiODAABdrZgsqUU375_big.jpg
root@ubuntu:~#

通过浏览器访问上面的example file url
这里写图片描述
成功

java代码测试
首先导入fastdfs-client-java-1.25.jar下载地址点击here
java代码如下

package wl;

import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.junit.Test;

public class TestFastFDSClient {
    @Test
    public void testFastFDSClient() throws Exception {
        // 1、把FastDFS提供的jar包添加到工程中
        // 2、初始化全局配置。加载一个配置文件。
        ClientGlobal.init(this.getClass().getClassLoader().getResource("").toString().substring(6) + "client.conf");
        // 3、创建一个TrackerClient对象。
        TrackerClient trackerClient = new TrackerClient();
        // 4、创建一个TrackerServer对象。
        TrackerServer trackerServer = trackerClient.getConnection();
        // 5、声明一个StorageServer对象,null。
        StorageServer storageServer = null;
        // 6、获得StorageClient对象。
        StorageClient storageClient = new StorageClient(trackerServer, storageServer);
        // 7、直接调用StorageClient对象方法上传文件即可。
        String[] strings = storageClient.upload_file("C:/Users/Administrator/Desktop/2.jpg", "jpg", null);
        for (String string : strings) {
            System.out.println(string);
        }
    }

}

在你的src目录下加入client.conf 文件名字可以随便取配置如下tracker_server=192.168.10.229:22122
打印结果为

group1
M00/00/00/wKgK5VgWOuOAb_sSAAGBpiieEQ8457.jpg

通过http://nginx_server:port/group1/M00/00/00/wKgK5VgWOuOAb_sSAAGBpiieEQ8457.jpg 访问该图片
这里写图片描述
成功
本文所用到的安装包下载地址

猜你喜欢

转载自blog.csdn.net/name_is_wl/article/details/52979208
今日推荐