Day14_02_FastDFS教程之Ubuntu下FastDFS安装配置详细教程

Ubuntu18.04中FastDFS安装配置

一. 服务器要求

跟踪服务器(Tracker Server):192.168.1.104
    
存储服务器(Storage Server):192.168.1.104
    
操作系统: Ubuntu18.04
    
用户: root
    
数据存储目录: /usr/local/fastdfs
    
所需安装包:
fastdfs-master.zip: FastDFS源码;
    
libfastcommon-master.zip: (从 FastDFS 和 FastDHT 中提取出来的公共的C函数库)
    
fastdfs-nginx-module-master.zip: storage服务器节点所需要的http服务nginx    模块
    
nginx-1.8.0.tar.gz: Nginx安装包

下载完成后,将压缩包解压到/usr/local/fastdfs/目录下

二. 安装步骤

1. 安装所需的依赖包

1.1 安装gcc

安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装.

查看是否已安装gcc

dpkg -l | grep gcc

如果没有则下载安装

sudo apt-get install g++-5

apt-get install gcc

apt-get install build-essential

1.2 安装PCRE pcre-devel

PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库.nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库.nginx也需要此库.

查看是否已经安装libpcre3

dpkg -l | grep libpcre3

安装libpcre3

sudo apt-get install libpcre3 libpcre3-dev

1.3 安装zlib

zlib 库提供了很多种压缩和解压缩的方式,nginx 使用 zlib 对 http 包的内容进行 gzip,所以需要在 Centos 上安装 zlib 库.

查看是否安装zlib

dpkg -l | grep zlib

如果没有则下载安装

sudo apt-get install zlib1g-dev

1.4 安装OpenSSL

OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用.

nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库.

查看是否安装libssl-dev:

dpkg -l | grep libssl-dev

安装libssl-dev

sudo apt-get install openssl libssl-dev

2. 安装libfatscommon

[root@localhost ~]# cd /usr/local/
[root@localhost ~]# mkdir fastdfs
[root@localhost ~]# cd fastdfs
[root@localhost local]# unzip libfastcommon-master.zip
[root@localhost local]# cd libfastcommon-master
[root@localhost libfastcommon-master]# ll

总用量 36
drwxr-xr-x. 2 root root 4096 7月   6 2017 doc
-rw-r--r--. 1 root root 8005 7月   6 2017 HISTORY
-rw-r--r--. 1 root root  566 7月   6 2017 INSTALL
-rw-r--r--. 1 root root 1607 7月   6 2017 libfastcommon.spec
-rwxr-xr-x. 1 root root 3099 7月   6 2017 make.sh
drwxr-xr-x. 2 root root 4096 7月   6 2017 php-fastcommon
-rw-r--r--. 1 root root 2763 7月   6 2017 README
drwxr-xr-x. 3 root root 4096 9月  28 2017 src

####编译、安装
[root@localhost libfastcommon-master]# ./make.sh
[root@localhost libfastcommon-master]# ./make.sh install

3. 安装FastDFS

#上传fastdfs-master.zip,并将该包复制到/usr/local/fastdfs/目录下,解压缩

root@localhost ~]# cd /usr/local/fastdfs/
[root@localhost local]# unzip fastdfs-master.zip
[root@localhost local]# cd fastdfs-master
[root@localhost fastdfs-master]# ll

总用量 136
drwxr-xr-x. 3 root root  4096 9月  28 2017 client
drwxr-xr-x. 2 root root  4096 9月  28 2017 common
drwxr-xr-x. 2 root root  4096 8月  10 2017 conf
-rw-r--r--. 1 root root 35067 8月  10 2017 COPYING-3_0.txt
-rw-r--r--. 1 root root  3171 8月  10 2017 fastdfs.spec
-rw-r--r--. 1 root root 33207 8月  10 2017 HISTORY
drwxr-xr-x. 2 root root  4096 8月  10 2017 init.d
-rw-r--r--. 1 root root  7755 8月  10 2017 INSTALL
-rwxr-xr-x. 1 root root  5548 8月  10 2017 make.sh
drwxr-xr-x. 2 root root  4096 8月  10 2017 php_client
-rw-r--r--. 1 root root  2380 8月  10 2017 README.md
-rwxr-xr-x. 1 root root  1768 8月  10 2017 restart.sh
-rwxr-xr-x. 1 root root  1680 8月  10 2017 stop.sh
drwxr-xr-x. 4 root root  4096 9月  28 2017 storage
drwxr-xr-x. 2 root root  4096 8月  10 2017 test
drwxr-xr-x. 2 root root  4096 9月  28 2017 tracker

## 编译、安装
[root@localhost fastdfs-master]# ./make.sh
[root@localhost fastdfs-master]# ./make.sh install

4. 采用默认安装方式

4.1 fastdfs服务脚本安装后存放位置

  /etc/init.d/fdfs_storaged

  /etc/init.d/fdfs_trackerd

4.2 配置文件(示例配置文件)

  [root@localhost fastdfs-master]# ll /etc/fdfs/
  -rw-r--r--. 1 root root 1461 9月 28 2017 client.conf.sample
  -rw-r--r--. 1 root root 7927 9月 28 2017 storage.conf.sample
  -rw-r--r--. 1 root root 7200 9月 28 2017 tracker.conf.sample

4.3 命令行工具(/usr/bin目录下)

  [root@localhost fastdfs-master]# ll /usr/bin/ | grep fdfs
  -rwxr-xr-x. 1 root root      262291 9月  28 2017 fdfs_appender_test
  -rwxr-xr-x. 1 root root      261988 9月  28 2017 fdfs_appender_test1
  -rwxr-xr-x. 1 root root      252300 9月  28 2017 fdfs_append_file
  -rwxr-xr-x. 1 root root      251458 9月  28 2017 fdfs_crc32
  -rwxr-xr-x. 1 root root      252407 9月  28 2017 fdfs_delete_file
  -rwxr-xr-x. 1 root root      253222 9月  28 2017 fdfs_download_file
  -rwxr-xr-x. 1 root root      252948 9月  28 2017 fdfs_file_info
  -rwxr-xr-x. 1 root root      265628 9月  28 2017 fdfs_monitor
  -rwxr-xr-x. 1 root root      879078 9月  28 2017 fdfs_storaged
  -rwxr-xr-x. 1 root root      268651 9月  28 2017 fdfs_test
  -rwxr-xr-x. 1 root root      267828 9月  28 2017 fdfs_test1
  -rwxr-xr-x. 1 root root      374227 9月  28 2017 fdfs_trackerd
  -rwxr-xr-x. 1 root root      253350 9月  28 2017 fdfs_upload_appender
  -rwxr-xr-x. 1 root root      256992 9月  28 2017 fdfs_upload_file

5. 配置tracker服务器

5.1 复制tracker样例配置文件,并重命名

[root@localhost fastdfs-master]# cp /etc/fdfs/tracker.conf.sample /etc/fdfs/tracker.conf

5.2 创建base_path指定的目录

[root@localhost fastdfs-master]# mkdir -p /usr/local/fastdfs/tracker

5.3 修改tracker配置文件

[root@localhost fastdfs-master]# vim /etc/fdfs/tracker.conf

#修改内容如下

# 启用配置文件
disabled=false         

# tracker服务器端口(默认22122)
port=22122

# 存储日志和数据的根目录
base_path=/usr/local/fastdfs/tracker

5.4 启动tracker服务器

[root@localhost fastdfs-master]# /etc/init.d/fdfs_trackerd start

初次启动,会在/usr/local/fastdfs/tracker目录下生成logs、data两个目录.

检查FastDFS Tracker Server是否启动成功

ps -ef | grep fdfs_trackerd

重启命令

[root@localhost fastdfs-master]# /etc/init.d/fdfs_trackerd restart

 

6. 配置storage服务器

6.1 复制storage样例配置文件,并重命名

[root@localhost fastdfs-master]# cp /etc/fdfs/storage.conf.sample /etc/fdfs/storage.conf

6.2 创建基础数据目录

#此处配置的 base path 和store_path0路径,因为上面的路径可能不存在

[root@localhost fastdfs-master]# mkdir -p /usr/local/fastdfs/storage       

6.3 编辑配置文件

[root@localhost fastdfs-master]# vim /etc/fdfs/storage.conf

修改部分内容如下

disabled=false                      # 启用配置文件

port=23000                          # storage服务端口

base_path=/usr/local/fastdfs/storage #数据和日志文件存储根目录

store_path0=/usr/local/fastdfs/storage      #第一个存储目录

tracker_server=192.168.1.104:22122  # tracker服务器的IP和端口号,此处是上面预定好的ip,一般就是本机ip,不能写127.0.0.1,否则不能自动生成data目录

http.server_port=8080    # http访问文件的端口,此处需要和后面 nginx 监听端口保持一致

6.4 启动storage服务器

[root@localhost fastdfs-master]# /etc/init.d/fdfs_storaged start

初次启动,会在/usr/local/fastdfs/storage目录下生成logs、data两个目录

检查FastDFS Tracker Server是否启动成功:

ps -ef | grep fdfs_storaged

 

7.文件上传测试(192.168.1.104,此处仅为测试,后续会用代码上传)

7.1 修改Tracker服务器客户端配置文件

[root@localhost fastdfs-master]# cp /etc/fdfs/client.conf.sample /etc/fdfs/client.conf

[root@localhost fastdfs-master]# vim /etc/fdfs/client.conf

# 修改以下配置,其它保持默认
base_path=/usr/local/fastdfs/tracker

tracker_server=192.168.1.104:22122 #修改为tracker_server地址和端口

7.2 执行文件上传命令

[root@localhost fastdfs-master]# /usr/bin/fdfs_upload_file /etc/fdfs/client.conf /home/yyg/桌面/logo.jpg

返回文件ID号: /group1/M00/00/00/CgAfvVnMWOiAaxm0AAA5ilsiE-g844.png
如果返回类似内容,则表示上传成功

8. 在所有storage节点安装fastdfs-nginx-module

8.1 fastdfs-nginx-module 作用说明

FastDFS 通过 Tracker 服务器,然后将文件放在 Storage 服务器进行存储,但是同组存储服务器之间在进行文件复制的时候,会有同步延迟的问题.假如Tracker 服务器将文件上传到了 10.0.31.201节点上,上传成功后文件ID已经返回给了客户端.而此时 FastDFS 存储集群机制会将这个文件同步到同组的存储服务器节点10.0.31.202和其他的节点上面.在文件还没有复制完成的情况下,客户端如果用这个文件 ID 在 10.0.31.202 节点上取文件,就会出现文件无法访问的错误.而 fastdfs-nginx-module 可以重定向文件从而连接到源服务器获取文件,避免客户端由于复制延迟导致的文件无法访问错误.

8.2 解压fastdfs-nginx-module-master.zip

[root@localhost fastdfs-master]# cd /usr/local/fastdfs/
[root@localhost local]#  unzip fastdfs-nginx-module-master.zip

8.3 安装Nginx

本机安装了2个nginx服务器,第一个nginx服务器采用在线安装方式安装,专门讲解nginx负载均衡;第2个nginx采用源码编译方式安装,配合fastdfs使用.

#在/usr/local/目录下创建nginx,解压缩nginx-1.8.0的压缩包

tar -xvzf nginx-1.8.0.tar.gz

#进入到/usr/local/nginx/nginx-1.8.0目录下:

cd /usr/local/nginx/nginx-1.8.0/

#注意: 在执行./configure配置nginx参数的时候,需要将fastdfs-nginx-moudle源码#作为模块编译进去.

./configure --prefix=/usr/local/nginx/nginx-1.8.0 --sbin-path=/usr/bin/nginx --add-module=/usr/local/fastdfs/fastdfs-nginx-module-master/src

8.4 进行编译并安装

make && make install

注意:

在Ubuntu18.04上安装Ngnix,在编译步骤可能会出现如下错误:

cc1: all warnings being treated as errors
objs/Makefile:460: recipe for target 'objs/src/core/ngx_murmurhash.o' failed
make[1]: *** [objs/src/core/ngx_murmurhash.o] Error 1
make[1]: Leaving directory '/home/wzj/tools/nginx/nginx-1.11.3'
Makefile:8: recipe for target 'build' failed
make: *** [build] Error 2

解决办法

找到对应的Makefile文件(在/usr/local/nginx/nginx-1.8.0/objs/目录下),将第三行的-Werror去掉就可以了.

vim /usr/local/nginx/nginx-1.8.0/objs/Makefile

注意:

在去掉-Werror参数之后,要重新在/usr/local/nginx/nginx-1.8.0/目录下执行一次 make && make install命令!!!

cd /usr/local/nginx/nginx-1.8.0/

make && make install

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

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

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

#修改以下配置 其他配置默认即可
connect_timeout=10                  # 客户端访问文件连接超时时长(单位:秒)

base_path=/tmp                      # 临时目录

tracker_server=192.168.1.104:22122    # tracker服务IP和端口

storage_server_port=23000           # storage服务端口

group_name=group1                   # 组名

url_have_group_name=true    #访问链接前缀加上组名,注意此处一定要改为true!!!

store_path0=/usr/local/fastdfs/storage        # 文件存储路径

10. 复制 FastDFS 的部分配置文件到/etc/fdfs 目录下

root@localhost local]# cd /usr/local/fastdfs/fastdfs-master/conf

root@localhost local]# cp http.conf mime.types /etc/fdfs/

11. 在/usr/local/fastdfs/storage文件存储目录下创建软连接,将其链接到实际存放数据的目录,/M00是软连接目录.

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

12. 配置nginx用来访问storage的文件

注意下面的 user root,此处打开前面注释并修改为 root ,防止后面运行出错

root@localhost local]# vim /usr/local/nginx/nginx-1.8.0/conf/nginx.conf

user  root;
worker_processes 1;

events {
    worker_connections 1024;
}

http {
    include mime.types;
    default_type application/octet-stream;
    sendfile on;
    keepalive_timeout 65;
    
    server {    
        ##此处和前面storage.conf配置的 http.server 保持一致
        listen 8080;
        server_name localhost;
        
        ##配置通配符,将/group 开头的地址映射到 fastdfs 的项目,如果#只有一个 group1 可以直接写 group1s
        location ~/group([0-9])/M00 {
           ngx_fastdfs_module;
        }
        
        #error_page 500 502 503 504 /50x.html;

        #location = /50x.html {
        #    root html;
        #}
    }
}

13. 以指定配置文件的方式启动Nginx

#先在/usr/local/nginx/nginx-1.8.0/目录下创建一个logs目录

cd /usr/local/nginx/nginx-1.8.0/

mkdir logs

#因为本机安装了两个nginx,所以为了区分,采用指定nginx配置文件的方式来启动nginx
#一定要执行该命令!!!

root@localhost local]# /usr/bin/nginx -c /usr/local/nginx/nginx-1.8.0/conf/nginx.conf

#nginx重启命令为:
/usr/bin/nginx -s reload -c /usr/local/nginx/nginx-1.8.0/conf/nginx.conf

#启动nginx后,如果打印出ngx_http_fastdfs_set pid=xxx,表示fastdfs-nginx-#module模块加载成功了,xxx表示模块的进程ID。

14. 通过浏览器访问测试时上传的文件

测试上传后返回的文件ID为: group1/M00/00/00/CgAfvVnMWOiAaxm0AAA5ilsiE-g844.png,用浏览访问的地址为: http://192.168.1.104:8080/group1/M00/00/00/CgAfvVnMWOiAaxm0AAA5ilsiE-g844.png

若出现此效果,说明fastdfs完全安装成功!

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一一哥Sun

您的鼓励是我继续创作的动力哦

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值