搭建图片服务器

一、安装并配置FastDFS

1、运行环境及相关软件

CentOS 7.4 64位

FastDFS_v5.08.tar.gz

nginx-1.8.1.tar.gz

fastdfs-nginx-module_v1.16.tar.gz

libfastcommon

2、服务器规划

服务器名称IP地址和端口备注
fastdfs-tracker192.168.25.133:22122跟踪服务器/调度服务器
fastdfs-storage192.168.25.133:23000存储服务器

3、安装FastDFS

3.1 首先创建工具目录(非必须)
[root@fastdfs-storage ~]# mkdir -p /home/oldcat/tools
[root@fastdfs-storage ~]# cd /home/oldcat/tools/
3.2 下载并安装FastDFS依赖包libfastcommon
[root@fastdfs-storage tools]# wget https://codeload.github.com/happyfish100/libfastcommon/zip/master
[root@fastdfs-storage tools]# unzip master
[root@fastdfs-storage tools]# cd libfastcommon-master/
[root@fastdfs-storage libfastcommon-master]# ls
HISTORY  INSTALL  libfastcommon.spec  make.sh  php-fastcommon  README  src
[root@fastdfs-storage libfastcommon-master]# ./make.sh
[root@fastdfs-storage libfastcommon-master]# ./make.sh install
提示:如果执行第二句话报以下错,没报错直接3.3
-bash: unzip: command not found

则是命令没有找到,其原因肯定是没有安装unzip。
安装命令是:

yum install -y unzip zip
3.3 下载并安装FastDFS
[root@fastdfs-tracker tools]# wget http://jaist.dl.sourceforge.net/project/fastdfs/FastDFS%20Server%20Source%20Code/FastDFS%20Server%20with%20PHP%20Extension%20Source%20Code%20V5.08/FastDFS_v5.08.tar.gz
[root@fastdfs-tracker tools]# tar xf FastDFS_v5.08.tar.gz
[root@fastdfs-tracker tools]# cd FastDFS
[root@fastdfs-tracker FastDFS]# ./make.sh && ./make.sh install

采用默认方式安装后的文件及目录:

  • 服务脚本:
[root@fastdfs-tracker ~]# ll /etc/init.d/ |grep fdfs
-rwxr-xr-x. 1 root root   918 4月  22 22:08 fdfs_storaged
-rwxr-xr-x. 1 root root   920 4月  22 22:08 fdfs_trackerd
  • 样例配置文件
[root@fastdfs-tracker ~]# ll /etc/fdfs/
总用量 20
-rw-r--r--. 1 root root 1461 4月  22 22:08 client.conf.sample
-rw-r--r--. 1 root root 7927 4月  22 22:08 storage.conf.sample
-rw-r--r--. 1 root root 7200 4月  22 22:08 tracker.conf.sample
  • 命令行工具
[root@fastdfs-tracker ~]# ll /usr/bin|grep fdfs
-rwxr-xr-x.   1 root root     252272 4月  22 22:08 fdfs_appender_test
-rwxr-xr-x.   1 root root     252225 4月  22 22:08 fdfs_appender_test1
-rwxr-xr-x.   1 root root     242449 4月  22 22:08 fdfs_append_file
-rwxr-xr-x.   1 root root     242013 4月  22 22:08 fdfs_crc32
-rwxr-xr-x.   1 root root     242508 4月  22 22:08 fdfs_delete_file
-rwxr-xr-x.   1 root root     243627 4月  22 22:08 fdfs_download_file
-rwxr-xr-x.   1 root root     243369 4月  22 22:08 fdfs_file_info
-rwxr-xr-x.   1 root root     255657 4月  22 22:08 fdfs_monitor
-rwxr-xr-x.   1 root root     863913 4月  22 22:08 fdfs_storaged
-rwxr-xr-x.   1 root root     258712 4月  22 22:08 fdfs_test
-rwxr-xr-x.   1 root root     257881 4月  22 22:08 fdfs_test1
-rwxr-xr-x.   1 root root     365232 4月  22 22:08 fdfs_trackerd
-rwxr-xr-x.   1 root root     243547 4月  22 22:08 fdfs_upload_appender
-rwxr-xr-x.   1 root root     244453 4月  22 22:08 fdfs_upload_file
注意:虽然FastDFS区分tracker和storage服务器,但是安装的软件及步骤均相同,只是不同的配置文件而已,因此以上安装适用tracker server和storage server

4、配置跟踪服务器(tracker server)

4.1拷贝tracker server和client端样例配置文件并重命名
[root@fastdfs-tracker ~]# cp /etc/fdfs/tracker.conf.sample /etc/fdfs/tracker.conf
[root@fastdfs-storage ~]# cp /etc/fdfs/client.conf.sample /etc/fdfs/client.conf
4.2 编辑tracker server配置文件tracker.conf,需要修改内容如下:
disabled=false(默认为false,表示是否无效)
port=22122(默认为22122)
base_path=/data/fastdfs/tracker
4.3 编辑client端的配置文件client.conf,需要修改内如下
base_path=/data/fastdfs/tracker
tracker_server=192.168.25.133:22122
IP填写自己服务器的公有IP(必须),如果是虚拟机直接用下面的
例:192.168.25.133
4.4 创建tracker server数据目录
[root@fastdfs-tracker ~]# mkdir -p /data/fastdfs/tracker
4.5 测试启动trackerserver,启动成功会自动在/data/fastdfs/tracker目录建data和logs目录
[root@fastdfs-tracker ~]# cd /data/fastdfs/tracker/
[root@fastdfs-tracker tracker]# ls
[root@fastdfs-tracker tracker]# /etc/init.d/fdfs_trackerd start
Starting FastDFS tracker server:
[root@fastdfs-tracker tracker]# ss -lntup|grep 22122
tcp    LISTEN     0      128                    *:22122                 *:*      users:(("fdfs_trackerd",3785,5)) 
[root@fastdfs-tracker tracker]# ls
data  logs
4.6 关闭tracker server
[root@fastdfs-tracker tracker]# /etc/init.d/fdfs_trackerd stop
waiting for pid [3785] exit ...
pid [3785] exit.

5、配置存储服务器(storage server)

5.1 拷贝storage server样例配置文件并重命名
[root@fastdfs-storage ~]# cp /etc/fdfs/storage.conf.sample /etc/fdfs/storage.conf
5.2 编辑storage server配置文件storage.conf,需要修改内容如下:
disabled=false(默认为false,表示是否无效)
port=23000(默认为23000)
base_path=/data/fastdfs/storage
tracker_server=192.168.25.133:22122
store_path0=/data/fastdfs/storage
http.server_port=8888(默认为8888,nginx中配置的听端口那之一致)
IP填写自己服务器的公有IP(必须),如果是虚拟机直接用下面的,必须与上面的tracker server一致
例:192.168.25.133
5.3 创建storage server数据目录
[root@fastdfs-storage ~]# mkdir -p /data/fastdfs/storage
5.4 测试启动storage server,启动成功会自动在/data/fastdfs/tracker目录新建data和logs目录(启动storage server的前提是tracker server必须事先已启动)
[root@fastdfs-storage ~]# cd /data/fastdfs/storage/
[root@fastdfs-storage storage]# ls
[root@fastdfs-storage storage]# /etc/init.d/fdfs_storaged start
Starting FastDFS storage server:
[root@fastdfs-storage storage]# ss -lntup|grep 23000
tcp    LISTEN     0      128                    *:23000                 *:*      users:(("fdfs_storaged",3786,5))
[root@fastdfs-storage storage]# ls
data  logs

6、文件上传测试

执行如下上传命令:

[root@fastdfs-tracker ~]#mkdir /home/oldcat/imgs
然后通过sftp传一张图片到服务器即可(改名为test.xxx);
[root@fastdfs-tracker ~]# /usr/bin/fdfs_upload_file /etc/fdfs/client.conf /home/oldcat/imgs/test.jpg
group1/M00/00/00/rBIK6VcaP0aARXXvAAHrUgHEviQ394.jpg
返回文件ID即说明文件已经上传成功

二、存储服务器(storage server)安装并配置nginx

1、下载并安装fastdfs-nginx-module模块

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

[root@fastdfs-storage  tools]# wget http://nchc.dl.sourceforge.net/project/fastdfs/FastDFS%20Nginx%20Module%20Source%20Code/fastdfs-nginx-module_v1.16.tar.gz
[root@fastdfs-storage tools]# tar xf fastdfs-nginx-module_v1.16.tar.gz 
[root@fastdfs-storage tools]# cd fastdfs-nginx-module/src/
[root@fastdfs-storage src]# vim config
编辑config文件,执行如下命令进行批量替换并保存退出
:%s+/usr/local/+/usr/+g

2、拷贝fastdfs-nginx-module模块中配置文件到/etc/fdfs目录中并编辑

[root@fastdfs-storage ~]# cp /home/oldcat/tools/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/
[root@fastdfs-storage ~]# vim /etc/fdfs/mod_fastdfs.conf
修改内容如下:
connect_timeout=10
base_path=/tmp(默认为/tmp)
tracker_server=192.168.25.133:22122(云服务器为公有IP)
storage_server_port=23000(默认配置为23000)
url_have_group_name = true
store_path0=/data/fastdfs/storage
group_name=group1(默认配置为group1)

3、安装nginx依赖库

[root@fastdfs-storage nginx-1.8.1]# yum install -y pcre-devel zlib-devel

4、下载并安装nginx

[root@fastdfs-storage  tools]# wget http://nginx.org/download/nginx-1.8.1.tar.gz
[root@fastdfs-storage tools]# tar xf nginx-1.8.1.tar.gz
[root@fastdfs-storage tools]# cd nginx-1.8.1
[root@fastdfs-storage nginx-1.8.1]# ./configure --prefix=/application/nginx/ --add-module=../fastdfs-nginx-module/src/
[root@fastdfs-storage nginx-1.8.1]# make && make install

5、拷贝FastDFS中的部分配置文件到/etc/fdfs目录中

[root@fastdfs-storage ~]# cp /home/oldcat/tools/FastDFS/conf/http.conf /etc/fdfs/
[root@fastdfs-storage ~]# cp /home/oldcat/tools/FastDFS/conf/mime.types /etc/fdfs/

6、配置nginx,如下所示:

[root@fastdfs-storage ~]# vim /application/nginx/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 {
            listen       8888;
            server_name  localhost;
            location ~/group[0-9]/ {
                ngx_fastdfs_module;
            }
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
            root   html;
            }
        }
    }

说明:

  • "user root"是解决下载操作时报404的问题
  • 8888端口号与/etc/fdfs/storage.conf中的http.server_port=8888相对应
  • storage对应有多个group的情况下,访问路径带group名称,例如:/group1/M00/00/00/**,对应nginx配置:
    location ~/group[0-9]/ {
        ngx_fastdfs_module;
    }

7、拷贝nginx服务到/etc/init.d/目录下并启动

[root@fastdfs-storage ~]# cp /application/nginx/sbin/nginx /etc/init.d/
[root@fastdfs-storage ~]# /etc/init.d/nginx
[root@fastdfs-storage ~]# ss -lntup|grep 8888
tcp    LISTEN     0      128                    *:8888                  *:*      users:(("nginx",7308,6),("nginx",7309,6))

8、通过浏览器访问之前已经上传的文件

例:IP:8888/group1/M00/00/00/rBGcBVqBZ_mAGl9oAAEfvSdB99w682.jpg

image
浏览器可以成功访问到上传的图片,说明使用FastDFS搭建的图片服务器成功完成!

注意:若是在远程服务器上此处IP应为公网IP,并且服务器需要设置安全组开放8888端口

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

我的世界没光

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值