5分钟搭一个FastDFS--Linux篇

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

阅读目录:

1. 关闭防火墙和Selinux

2. 下载并安装FastDFS 5.x版本

3. 配置并启动FastDFS

4.测试

5.搭配Nginx

6.报错及解决

7.说明

1. 关闭防火墙和Selinux

        Linux的防火墙是咱们新手的噩梦,很多情况会出现能ping通,但是访问不了Web页面。所以开始就干掉它!

    1.1 关闭防火墙

    [root@localhost ~]# /etc/init.d/iptables stop
    iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
    iptables: Flushing firewall rules:                         [  OK  ]
    iptables: Unloading modules:                               [  OK  ]

    1.2 开机自动关闭防火墙

    [root@localhost ~]# chkconfig iptables off

    1.3 查看Selinux状态

    [root@localhost ~]# sestatus
    SELinux status: enabled 
    SELinuxfs mount: /sys/fs/selinux 
    SELinux root directory: /etc/selinux 
    Loaded policy name: targeted 
    Current mode: enforcing 
    Mode from config file: enforcing 
    Policy MLS status: enabled 
    Policy deny_unknown status: allowed 
    Max kernel policy version: 28

    1.4 关闭selinux

    [root@localhost ~]# vim /etc/selinux/config 

修改 SELINUX=disabled ,重启机器
注:永久开启->改成:SELINUX=enforcing

2. 下载并安装FastDFS 5.x版本

注:为了方便管理,创建一个文件夹专门放所需软件

    [root@localhost /]# mkdir developer
    [root@localhost /]# cd developer

    2.1 检查依赖包

    [root@localhost developer]# yum -y install zlib zlib-devel pcre pcre-devel gcc gcc-c++ openssl openssl-devel libevent libevent-devel perl unzip net-tools wget

    2.2 下载安装包(fastdfs源码包、libfastcommon源码包)

    [root@localhost developer]# wget https://github.com/happyfish100/fastdfs/archive/V5.05.tar.gz 
    [root@localhost developer]# wget https://github.com/happyfish100/libfastcommon/archive/V1.0.7.tar.gz

    2.3 创建安装目录(-p是遍历创建目录)

    [root@localhost developer]# mkdir -p /usr/local/fastdfs/{storage,tracker,client}

    2.4 安装libfastcommon库(到指定文件夹下)

    [root@localhost developer]# tar -xzvf V1.0.7.tar.gz -C /usr/local
    [root@localhost developer]# cd /usr/local/libfastcommon-1.0.7 
    [root@localhost libfastcommon-1.0.7]# ./make.sh 
    [root@localhost libfastcommon-1.0.7]# ./make.sh install

    2.5 安装FastDFS

    [root@localhost libfastcommon-1.0.7]# cd /developer
    [root@localhost developer]# tar -zxvf V5.05.tar.gz -C /usr/local     
    [root@localhost developer]# cd /usr/local/fastdfs-5.05
    [root@localhost fastdfs-5.05]# ./make.sh 
    [root@localhost fastdfs-5.05]# ./make.sh install

3. 配置并启动FastDFS

    3.1 文件重命名

    [root@localhost developer]# cd /etc/fdfs
    [root@localhost fdfs]# mv storage.conf.sample storage.conf  
    [root@localhost fdfs]# mv tracker.conf.sample tracker.conf
    [root@localhost fdfs]# mv client.conf.sample client.conf

    3.2 查看ip

    [root@localhost developer]# ifconfig 
    eth0      Link encap:Ethernet  HWaddr 00:0C:29:1E:D7:FC  
              inet addr:192.168.28.133  Bcast:192.168.28.255  Mask:255.255.255.0
              inet6 addr: fe80::20c:29ff:fe1e:d7fc/64 Scope:Link
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:41654 errors:0 dropped:0 overruns:0 frame:0
              TX packets:8936 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000 
              RX bytes:55673943 (53.0 MiB)  TX bytes:1037412 (1013.0 KiB)

    3.3 配置tracker

    [root@localhost developer]# vim /etc/fdfs/tracker.conf

bind_addr=

base_path=/usr/local/fastdfs/tracker

    3.4 配置storage

    [root@localhost developer]# vim /etc/fdfs/storage.conf

group_name=group1

bind_addr=192.168.28.133

base_path=/usr/local/fastdfs/tracker

store_path0=/usr/local/fastdfs/storage

tracker_server=192.168.28.133:22122

    3.5 配置client

    [root@localhost developer]# vim /etc/fdfs/client.conf

# 用于存储日志文件

base_path=/usr/local/fastdfs/client 

tracker_server=192.168.28.133:22122

    3.6 启动trackerd和storaged

    [root@localhost fdfs]# /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf start 
    [root@localhost fdfs]# /usr/bin/fdfs_storaged /etc/fdfs/storage.conf start

4.测试

    4.1 上传并查看

    [root@localhost fdfs]# /usr/bin/fdfs_test /etc/fdfs/client.conf upload /usr/include/stdlib.h

    成功提示:

group_name=group1, remote_filename=M00/00/00/wKgchVqh9U6AClDVAACFzpgzGxg08920.h
source ip address: 192.168.28.133
file timestamp=2018-03-09 10:45:34
file size=34254
file crc32=2553486104
example file url: http://192.168.28.133/group1/M00/00/00/wKgchVqh9U6AClDVAACFzpgzGxg08920.h
storage_upload_slave_by_filename
group_name=group1, remote_filename=M00/00/00/wKgchVqh9U6AClDVAACFzpgzGxg08920_big.h
source ip address: 192.168.28.133
file timestamp=2018-03-09 10:45:34
file size=34254
file crc32=2553486104
example file url: http://192.168.28.133/group1/M00/00/00/wKgchVqh9U6AClDVAACFzpgzGxg08920_big.h

5.搭配Nginx

    5.1 Nginx的安装和下载参考:Nginx 1.12.x 安装和配置--Linux篇

    https://my.oschina.net/u/3209432/blog/1581391

    5.2 设置软链接(必需)

    [root@localhost fdfs]# ln -sv /usr/include/fastcommon /usr/local/include/fastcommon 
    [root@localhost fdfs]# ln -sv /usr/include/fastdfs /usr/local/include/fastdfs 
    [root@localhost fdfs]# ln -sv /usr/lib64/libfastcommon.so /usr/local/lib/libfastcommon.so

    5.3 下载Nginx的FastDFS模块

    [root@localhost fdfs]# cd /developer
    [root@localhost developer]# wget http://jaist.dl.sourceforge.net/project/fastdfs/FastDFS%20Nginx%20Module%20Source%20Code/fastdfs-nginx-module_v1.16.tar.gz

    5.4 解压文件(到指定文件夹下)

    [root@localhost developer]# tar -xvzf fastdfs-nginx-module_v1.16.tar.gz -C /usr/local

    5.5 编译并安装

    [root@localhost developer]# cd nginx-1.12.2  
    [root@localhost nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --add-module=/usr/local/fastdfs-nginx-module/src
    [root@localhost nginx-1.12.2]# make && make install 

    5.6 配置FastDFS模块

        5.6.1 复制fastdfs-nignx模块的配置文件 mod_fastdfs.conf 到 /etc/fdfs/

    [root@localhost nginx-1.12.2]# cd /usr/local/fastdfs-nginx-module/src
    [root@localhost src]# cp mod_fastdfs.conf /etc/fdfs/

        5.6.2 复制fastdfs中的http.conf、mime.types文件到/etc/fdfs

        [root@localhost src]# cp /usr/local/fastdfs-5.05/conf/http.conf /usr/local/fastdfs-5.05/conf/mime.types /etc/fdfs/

        5.6.3 修改mod_fastdfs.conf

        [root@localhost src]# vim /etc/fdfs/mod_fastdfs.conf

    base_path=/usr/local/fastdfs/tracker

    tracker_server=192.168.19.128:22122

    #http带group名字     

    url_have_group_name = true

    #和storage.conf 一样
    store_path0=/usr/local/fastdfs/storage

        5.6.3 在storage的data目录中建立软连接,不然找不到目录

        [root@localhost src]# mkdir /usr/local/fastdfs/storage/data/
        [root@localhost src]# ln -s /usr/local/fastdfs/storage/data/ /usr/local/fastdfs/storage/data/M00

        5.6.4 修改storage.conf

        [root@localhost src]# vim /etc/fdfs/storage.conf

#端口号必须和nginx的端口好一样

http.server_port=80

    5.7 编辑 Nginx 模块的配置文件

   [root@localhost src]# vim /usr/local/fastdfs-nginx-module/src/config

    修改一行:

    CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/local/include/fastcommon/

    5.8 编辑nginx.conf

   [root@localhost src]# vim /usr/local/nginx/conf/nginx.conf

    增加

# 拦截包含 /group1/M00 请求,使用 fastdfs 这个 Nginx 模块进行转发

location ~/group([0-9])/M00 {
            ngx_fastdfs_module;
 }

5分钟搭一个FastDFS--Linux篇_第1张图片

    5.9 启动Nginx

    [root@localhost src]# /usr/local/nginx/sbin/nginx

6.报错及解决

    6.1 编译nginx的时候报错
        在nginx目录下,执行configure之后,进行make编译报错,报错内容如下:

s -I src/mail \
                -o objs/addon/src/ngx_http_fastdfs_module.o \
                /usr/local/fastdfs-nginx-module/src/ngx_http_fastdfs_module.c
In file included from /usr/local/fastdfs-nginx-module/src/ngx_http_fastdfs_module.c:6:
/usr/local/fastdfs-nginx-module/src/common.c:21:25: error: fdfs_define.h: No such file or directory

......

/usr/local/fastdfs-nginx-module/src/ngx_http_fastdfs_module.c:933: error: ‘true’ undeclared (first use in this function)
make[1]: *** [objs/addon/src/ngx_http_fastdfs_module.o] Error 1
make[1]: Leaving directory `/usr/local/nginx-1.7.9'
make: *** [build] Error 2

        方法1:

    [root@localhost src]# vim /usr/local/fastdfs-nginx-module/src/config

            将CORE_INCS="$CORE_INCS /usr/local/include/fastdfs /usr/local/include/fastcommon/"
            修改为:

            CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/local/include/fastcommon/"
            其实就是改动了fastdfs的路径,没改之前直接访问cd /usr/local/include/fastdfs为空目录。

        方法2:设置对应的软连接也可以,详情查看上文5.2章节
            修改完成之后在nginx目录,重新configure和make就好了。

    6.2 启动nginx报错
        6.2.1 在storage上配置nginx相关信息后启动nginx,查看日志发现报错:

    [root@loubobooo src]# cat /usr/local/nginx/logs/error.log
    ngx_http_fastdfs_process_init pid=4963
    [2018-03-12 00:03:20] ERROR - file: ini_file_reader.c, line: 315, include file "http.conf" not exists, line: "#include http.conf"

        解决方案:详情查看上文5.6.1章节
        6.2.2 查看日志,还报错:

    [root@loubobooo src]# cat /usr/local/nginx/logs/error.log
    ngx_http_fastdfs_process_init pid=4963
    [2018-03-12 00:03:20] EERROR - file: /usr/local/fastdfs-nginx-module/src/common.c, line: 180, config file: /etc/fdfs/mod_fastdfs.conf, you must set url_have_group_name to true to support multi-group!

        解决方案:详情查看上文5.6.2章节,重启trackerd、storaged以及Nginx

7.说明

    说明:本次使用

        操作系统:CentOS 6.8 64位

        FastDFS版本:v5.05

        Nginx版本:1.12.2

转载于:https://my.oschina.net/loubobooo/blog/1632180

你可能感兴趣的:(5分钟搭一个FastDFS--Linux篇)