zoukankan      html  css  js  c++  java
  • linux搭建FastDFS文件服务器

    本文主要介绍在linux服务器如何搭建FastDFS文件服务器。大概分为9个步骤,由于内容较为繁琐。下面带你入坑!

    首先简单介绍一下FastDFS是淘宝资深架构师余庆老师主导开源的一个分布式文件系统,用C语言编写。适应与中小企业,对文件不分块。

    主要有Tracker(管理) 和Storage (储存)。

    返回路径= 组名 /虚拟盘符{M00/00/02} /文件名

    1.软件包

    下面是安装FastDFS的安装包,也可以下载到:FastDFS,密码为:ideg

     

    2.安装gcc

    检查系统是否有安装GCC

    gcc –version

     安装GCC

    yum -y install gcc gcc-c++ autoconf pcre pcre-devel make automake
    yum -y install wget httpd-tools vim

     就把gcc当成c语言编译器, g++当成c++语言编译器用就是了.(知乎)

    3.安装libfastcommon

    进入libfastcommon压缩包目录解压,编译,安装

    tar -zxvf libfastcommonV1.0.7.tar.gz

    进入解压的libfastcommon-1.0.7目录,编译

    ./make.sh

    安装

    ./make.sh install 

    最后,libfastcommon.so文件到usr/lib下 ,请按照自己的实际文件路径修改被复制路径

    cp /data/fastDFS/software/libfastcommon-1.0.7/src/libfastcommon.so /usr/lib

    4.安装FastDFS

    解压FastDFS_v5.05.tar.gz,然后编译安装FastDFS

    tar -zxvf FastDFS_v5.05.tar.gz
    ./make.sh
     ./make.sh install

    安装成功后将目录conf内的文件拷贝到/etc/fdfs目录下:

    cp /data/fastDFS/software/FastDFS/conf/* /etc/fdfs/

    5.安装tracker

    进入/etc/fdfs目录,修改tracker.conf文件。如果不存在,就拷贝tracker.conf.sample文件为tracker.conf,然后再修改:

    base_path=/home/yuqing/fastdfs >>> base_path=/home/logs/fastdfs (注解:日志文件)
    
    http.server_port=8080 >>> http.server_port=80 (注解:端口,80是方便默认)
    
    store_group=group1 (注解:组名)

    其中,/home/logs/fastdfs目录如果不存在,就创建一个。

    之后,就启动tracker,并查看是启动成功(出现如下提示,表示启动成功):restart重启

    fdfs_trackerd /etc/fdfs/tracker.conf start
    netstat -unltp | grep tracker

    6.安装storage

    由于storage和tracker运行的都是fastDFS程序,每一台服务器都部署一台fasfDFS,一台服务器是tracker,一台是storage

    修改storage.conf文件。如果不存在,就拷贝storage.conf.sample文件为storage.conf,然后再修改:

    base_path=/home/yuqing/fastdfs >>> base_path=/home/logs/storage (日志路径)
    
    store_path0=/home/yuqing/fastdfs >>> store_path0=/home/data/storage  (实际储存文件路径,可以配置多个)
    
    tracker_server=192.168.209.121:22122 >>> tracker_server=192.168.2.231:22122 (连接tracker服务器地址)
    
    group_name=group1 (必须和tracker的组名相同)

    http.server_port=80 (这个端口也要改)

     其中,如果/home/logs/storage和/home/data/storage不存在,就创建该目录 

    然后,就启动storage,并查看是否成功(出现如下提示,表示启动成功):

    fdfs_storaged /etc/fdfs/storage.conf start
    netstat -unltp | grep storage

    最后,查看trackerstorage是不是在通信:

    fdfs_monitor /etc/fdfs/storage.conf

    如下提示,出现ACTIVE,表示二者均正常启动,至此就可以进行上传文件测试了。

    7.测试图片上传

    Tracker和storage都已经安装完成,使用命令测试文件上传:
    FastDFS提供一个文件上传命令:usr/bin/fdfs_test 测试文件上传。测试上传需要连接tracker服务器,连接storage服务器。因此需要指定一个配置文件:client.conf配置文件,通过Client.conf连接tracker服务器。

    修改/etc/fdfs下client.conf

      

    base_path=/home/logs/client (日志目录)
    
    tracker_server=192.168.2.231:22122  (tracker端口)

    测试命令

    /usr/bin/fdfs_test /etc/fdfs/client.conf upload anti-steal.jpg

    如下图返回地址则成功

    8.安装Nginx。

    安装Nginx所需的其他环境,gcc在上面装过了。本人安装没走这一步

    #gcc安装 
    yum install gcc-c++
    
    #PCRE pcre-devel 安装
    yum install -y pcre pcre-devel
    
    # zlib 安装
    yum install -y zlib zlib-devel
    
    #OpenSSL 安装
    yum install -y openssl openssl-devel

    解压、编译、安装。

    tar -zxvf nginx-1.12.1.tar.gz
    cd nginx-1.12.1
    ./configure
    make
    make install

     

    9.安装fastdfs-nginx-module 

    首先解压fastdfs-nginx-module_v1.16.tar.gz,修改/fastdfs-nginx-module/src/config文件。去掉所有的local(三个)

    拷贝usr/lib64目录下库文件libfdfsclient.so 

    cp /usr/lib64/libfdfsclient.so /usr/lib 

    在nginx安装目录下执行如下命令:把module添加nginx中。通过设置安装参数方式添加模块。

    cd nginx-1.12.1
    ./configure --add-module=../fastdfs-nginx-module/src

    注意:我这里nginx-1.12.1和fastdfs-nginx-module在同一级目录,所以这样写

    重新安装编译

    make && make install

    查看Nginx的模块

    /usr/local/nginx/sbin/nginx -V

    复制 fastdfs-nginx-module 源码中的配置文件到/etc/fdfs 目录, 并修改

    cd fastdfs-nginx-module/src
    cp mod_fastdfs.conf /etc/fdfs/

    进入/etc/fdfs/修改mod_fastdfs.conf如下配置,其他默认

    # 连接超时时间
    connect_timeout=10
    
    # Tracker Server
    tracker_server=192.168.2.231:22122
    
    # StorageServer 默认端口
    storage_server_port=23000
    
    # 如果文件ID的uri中包含/group**,则要设置为true
    url_have_group_name = true
    
    # Storage 配置的store_path0路径,必须和storage.conf中的一致
    store_path0=/home/data/storage
    
    # the base path to store log files
    base_path=/home/logs/storage

    配置iNginx ,进入/usr/local/nginx/conf目录下修改nginx.conf

    注意:无论你在哪解压安装的。必须进这个目录/usr/local/nginx/conf下修改nginx.conf

    vim nginx.conf
    修改配置,其它的默认
    在80端口下添加fastdfs-nginx模块
    
    location ~/group([0-9])/M00 {
        ngx_fastdfs_module;
    }
    注意下面#user  nobody;改成 #user  root;
    #user  nobody;
    worker_processes  1;
    
    #error_log  logs/error.log;
    #error_log  logs/error.log  notice;
    #error_log  logs/error.log  info;
    
    #pid        logs/nginx.pid;
    
    
    events {
        worker_connections  1024;
    }
    
    
    http {
        include       mime.types;
        default_type  application/octet-stream;
    
        #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
        #                  '$status $body_bytes_sent "$http_referer" '
        #                  '"$http_user_agent" "$http_x_forwarded_for"';
    
        #access_log  logs/access.log  main;
    
        sendfile        on;
        #tcp_nopush     on;
    
        #keepalive_timeout  0;
        keepalive_timeout  65;
    
        #gzip  on;
    
        server {
            listen       80;
            server_name  192.168.2.231;
            location ~/group([0-9])/M00 {
                ngx_fastdfs_module;
            }
            #charset koi8-r;
    
            #access_log  logs/host.access.log  main;
    
            location / {
                root   html;
                index  index.html index.htm;
            }
    
            #error_page  404              /404.html;
    
            # redirect server error pages to the static page /50x.html
            #
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
    
            # proxy the PHP scripts to Apache listening on 127.0.0.1:80
            #
            #location ~ .php$ {
            #    proxy_pass   http://127.0.0.1;
            #}
    
            # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
            #
            #location ~ .php$ {
            #    root           html;
            #    fastcgi_pass   127.0.0.1:9000;
            #    fastcgi_index  index.php;
            #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            #    include        fastcgi_params;
            #}
    
            # deny access to .htaccess files, if Apache's document root
            # concurs with nginx's one
            #
            #location ~ /.ht {
            #    deny  all;
            #}
        }
    
    
        # another virtual host using mix of IP-, name-, and port-based configuration
        #
        #server {
        #    listen       8000;
        #    listen       somename:8080;
        #    server_name  somename  alias  another.alias;
    
        #    location / {
        #        root   html;
        #        index  index.html index.htm;
        #    }
        #}
    
    
        # HTTPS server
        #
        #server {
        #    listen       443 ssl;
        #    server_name  localhost;
    
        #    ssl_certificate      cert.pem;
        #    ssl_certificate_key  cert.key;
    
        #    ssl_session_cache    shared:SSL:1m;
        #    ssl_session_timeout  5m;
    
        #    ssl_ciphers  HIGH:!aNULL:!MD5;
        #    ssl_prefer_server_ciphers  on;
    
        #    location / {
        #        root   html;
        #        index  index.html index.htm;
        #    }
        #}
    
    }
    注意:
      listen 80 端口值是要与 /etc/fdfs/storage.conf 中的 http.server_port=80 (前面改成80了)相对应。如果改成其它端口,则需要统一,同时在防火墙中打开该端口。
    
      location 的配置,如果有多个group则配置location ~/group([0-9])/M00 ,没有则不用配group。
    
    
    在文件存储目录下创建软连接,将其链接到实际存放数据的目录,注意这个文件存储的位置后面多个data目录是系统自动生成的
    ln -s /home/data/storage/data /home/data/storage/data/M00
     

    启动nginx

    #启动nginx
    cd /usr/local/nginx/sbin/
    ./nginx 

    直接启动
    /usr/local/nginx/sbin/nginx #设置开机启动 vim
    /etc/rc.local /usr/local/nginx/sbin/nginx # 设置执行权限 chmod 755 rc.local

    查看Nginx是否启动
    ps -ef | grep nginx
    
    
    OK记得云服务的安全组 和防火墙设置需要方通 80、22122、 23000 这个不会的请参考腾讯云服务器tomcat端口无法访问 中的防火墙设置

    在地址栏中访问成功 http://192.175.231.128/group1/M00/00/00/rBAACVzBeU2AQBKJAAOHDqS1H9o350.jpg

    报错!不存在的。有问题110,120,119

    注意:
    1.group2同组的Storage2和Storage3 FastDFS服务端口必须一致: port=23000。

    2.一台服务器可以装多个组(group)但不能装同组的多个Storage,日志会报错误,日志报错原因是"注意1"

    3.Version 4.05之前fastdfs内部绑定了libevent作为http服务器.Version 4.05之后的版本删除了内置的web http服务,内置的web http服务是个累赘,不用也罢!

    4.启动storage server时,一直处于僵死状态.启动storage server,storage将连接tracker server,如果连不上,将一直重试。直到连接成功,启动才算真正完成!如果集群中有2台tracker server,而

    其中一台tracker没有启动,可能会导致storage server一直处于僵死状态。

    
    
  • 相关阅读:
    ROS tf 两个常用的函数
    C/C++ assert()函数用法总结
    drand48 等 随机数生成函数
    PF部分代码解读
    Error "Client wants topic A to have B, but our version has C. Dropping connection."
    launch 文件的写法
    Spring七大框架
    web.xml filter配置
    web.xml listener配置
    web.xml加载过程
  • 原文地址:https://www.cnblogs.com/zeussbook/p/10757699.html
Copyright © 2011-2022 走看看