Building FastDFS File System on CentOS Single Machine

This example is to build a FastDFS file management system on CentOS 7.2 stand-alone computer.

FastDFS Architecture Diagram
Software name Edition Baidu Yunpan Storage Name
FastDFS 5.11 fastdfs-5.11.zip
FastDFS-Nginx-module nothing fastdfs-nginx-module-master.zip
LibFastCommon 1.0.36 libfastcommon-1.0.36.zip
nginx 1.10.3 nginx-1.10.3.tar.gz

Installation files are uploaded to Baidu Cloud Disk, location FastDFS Baidu Cloud Disk
The installation list is as follows:

Software name Edition Baidu Yunpan Storage Name
FastDFS 5.11 fastdfs-5.11.zip
FastDFS-Nginx-module nothing fastdfs-nginx-module-master.zip
LibFastCommon 1.0.36 libfastcommon-1.0.36.zip
nginx 1.10.3 nginx-1.10.3.tar.gz

Installation of FastDFS

1. Install libfast common

Unzip the installation package to the directory first

[root@localhost fastDFS]# unzip libfastcommon-1.0.36.zip 

The decompressed directory is as follows:

[root@localhost fastdfs-5.11]# ll
[root@localhost libfastcommon-1.0.36]# ll
//Total dosage 32
drwxr-xr-x. 2 root root  117 4 Month 52017 doc
-rw-r--r--. 1 root root 8005 4 Month 52017 HISTORY
-rw-r--r--. 1 root root  566 4 Month 52017 INSTALL
-rw-r--r--. 1 root root 1606 4 Month 52017 libfastcommon.spec
-rwxr-xr-x. 1 root root 3099 4 Month 52017 make.sh
drwxr-xr-x. 2 root root  191 4 Month 52017 php-fastcommon
-rw-r--r--. 1 root root 2763 4 Month 52017 README
drwxr-xr-x. 3 root root 4096 1 Month 1711:21 src

Install the C compiler gcc

[root@localhost fastdfs-5.11]# yum -y install gcc-c++

Compile the libfast common software and install it

[root@localhost fastdfs-5.11]# ./make.sh  && ./make.sh install

Create a soft link for libcommon to the / usr/local/lib directory

[root@localhost fastdfs-5.11]# ln -s /usr/lib64/libfastcommon.so /usr/local/lib/libfastcommon.so
[root@localhost fastdfs-5.11]# ln -s /usr/lib64/libfastcommon.so /usr/lib/libfastcommon.so
[root@localhost fastdfs-5.11]# ln -s /usr/lib64/libfdfsclient.so /usr/local/lib/libfdfsclient.so
[root@localhost fastdfs-5.11]# ln -s /usr/lib64/libfdfsclient.so /usr/lib/libfdfsclient.so

2. Installing FastDFS

Unzip installation package

[root@localhost fastDFS]# unzip fastdfs-5.11.zip 

The decompressed directory is as follows:

Total dosage 128
 Drwxr-xr-x.3 root 4096 January 17 11:25 client
 Drwxr-xr-x.2 root 4096 January 17 11:25 common
 Drwxr-xr-x.2 root 146 June 3 2017 conf
 - rw-r--r--. 1 Root 35067 June 3 2017 COPYING-3_0.txt
 - rw-r--r--. 1 root 3171 June 3 2017 fastdfs.spec
 - rw-r--r--. 1 root 33100 June 3 2017 HISTORY
 Drwxr-xr-x.2 root 48 June 3 2017 init.d
 - rw-r--r--. 1 Root 7755 June 3 2017 INSTALL
 - rwxr-xr-x.1 root 5548 June 3 2017 make.sh
 Drwxr-xr-x.2 root 4096 June 3 2017 php_client
 - rw-r--r--. 1 Root 2380 June 3 2017 README.md
 - rwxr-xr-x.1 root 1768 June 3 2017 restart.sh
 - rwxr-xr-x.1 root 1680 June 3 2017 stop.sh
 Drwxr-xr-x.4 root 4096 January 17 11:25 storage
 Drwxr-xr-x.2 root 4096 June 3 2017 test
 Drwxr-xr-x.2 root 4096 January 17 11:25 tracker

Enter the decompressed directory and compile and install it

[root@localhost fastDFS]# cd fastdfs-5.11/
[root@localhost fastdfs-5.11]# ./make.sh  && ./make.sh install

After successful installation, FastDFS will be installed in the / etc/fdfs directory:

[root@localhost fastdfs-5.11]# ll /etc/fdfs/
//Total dosage 76
-rw-r--r--. 1 root root   316 1 Month 1711:47 client.conf
-rw-r--r--. 1 root root  1461 1 Month 1711:25 client.conf.sample
-rw-r--r--. 1 root root   955 1 Month 1713:20 http.conf
-rw-r--r--. 1 root root 31172 1 Month 1713:21 mime.types
-rw-r--r--. 1 root root  3716 1 Month 1712:57 mod_fastdfs.conf
-rw-r--r--. 1 root root  1278 1 Month 1711:40 storage.conf
-rw-r--r--. 1 root root  7927 1 Month 1711:25 storage.conf.sample
-rw-r--r--. 1 root root   105 1 Month 1711:25 storage_ids.conf.sample
-rw-r--r--. 1 root root  1356 1 Month 1711:34 tracker.conf
-rw-r--r--. 1 root root  7389 1 Month 1711:25 tracker.conf.sample

We need to copy the three sample files and remove them.

[root@localhost fdfs]# cp client.conf.sample client.conf
[root@localhost fdfs]# cp storage.conf.sample storage.conf
[root@localhost fdfs]# cp tracker.conf.sample tracker.conf

FastDFS Installation Ended

Installation of Tracker

1. Create a tracker working directory

This directory is used to save tracker's data and log

[root@localhost fdfs]# mkdir /opt/fastdfs_tracker

2. Configure tracker

Configure tracker.conf in / etc/fdfs directory
The following four configurations are implemented:

1.disabled=false 
2.port=22122 #Default port number 
3.base_path=/opt/fastdfs_tracker #Directory I just created 
4.http.server_port=8080 #The default port is 8080
5.bind_addr= 0.0.0.0 Monitor address

The complete tracker.conf file information is as follows:

disabled=false

bind_addr= 0.0.0.0

port=22122

connect_timeout=30

network_timeout=60

base_path=/opt/fastdfs_tracker

max_connections=512

accept_threads=1

work_threads=4

min_buff_size = 8KB

max_buff_size = 128KB

store_lookup=2

store_group=group2

store_server=0

store_path=0

download_server=0

reserved_storage_space = 10%

log_level=info

run_by_group=

run_by_user=

allow_hosts=*

sync_log_buff_interval = 10

check_active_interval = 120

thread_stack_size = 64KB

storage_ip_changed_auto_adjust = true

storage_sync_file_max_delay = 86400

storage_sync_file_max_time = 300

use_trunk_file = false 

slot_min_size = 256

slot_max_size = 16MB

trunk_file_size = 64MB

trunk_create_file_advance = false

trunk_create_file_time_base = 02:00

trunk_create_file_interval = 86400

trunk_create_file_space_threshold = 20G

trunk_init_check_occupying = false

trunk_init_reload_from_binlog = false

trunk_compress_binlog_min_interval = 0

use_storage_id = false

storage_ids_filename = storage_ids.conf

id_type_in_filename = ip

store_slave_file_use_link = false

rotate_error_log = false

error_log_rotate_time=00:00

rotate_error_log_size = 0

log_file_keep_days = 0

use_connection_pool = false

connection_pool_max_idle_time = 3600

http.server_port=8080

http.check_alive_interval=30

http.check_alive_type=tcp

http.check_alive_uri=/status.html

Create Soft References after Modification and Save

[root@localhost fdfs]# ln -s /usr/bin/fdfs_storaged /usr/local/bin

4. Start tracker and add boot start item

[root@localhost fdfs]# service fdfs_trackerd start

Go through the tracker directory you just created and find that there are two more directories in the directory, data and log.

[root@localhost fdfs]# ll /opt/fastdfs_tracker/
//Total dosage 0
drwxr-xr-x. 2 root root 178 1 Month 1713:48 data
drwxr-xr-x. 2 root root  26 1 Month 1711:35 logs

Add tracker to startup entry

[root@localhost fdfs]# echo "service fdfs_trackerd start" |tee -a /etc/rc.d/rc.local

Take a look at tracker port listening

[root@localhost fdfs]# netstat -unltp|grep fdfs
tcp        0      0 0.0.0.0:22122           0.0.0.0:*               LISTEN      3088/fdfs_trackerd  

III. Installation of Storage

1. Configure the storage working directory

Because storage also requires a directory to store data, two more directories, fastdfs_storage_data and fastdfs_storage, have been built.

[root@localhost opt]# mkdir fastdfs_storage
[root@localhost opt]# mkdir fastdfs_storage_data
[root@localhost opt]# ll
//Total dosage 0
drwxr-xr-x. 4 root root 30 1 Month 1711:45 fastdfs_storage
drwxr-xr-x. 3 root root 18 1 Month 1711:45 fastdfs_storage_data
drwxr-xr-x. 4 root root 30 1 Month 1711:35 fastdfs_tracker

2. Configure storage file

Modify the storage.conf file in the / etc/fdfs directory
The main points of modification are as follows:

1.disabled=false 
2.group_name=group1 #Group name, change according to actual situation 
3.port=23000 #Set the storage port number, default is 23000. The storage port number of the same group must be the same. 
4.base_path=/opt/fastdfs_storage #Setting storage Data Files and Log Directories 
5.store_path_count=1 #Number of storage paths, need to match the number of store_path s 
6.store_path0=/opt/fastdfs_storage_data #Actual file storage path 
7.tracker_server=192.168.43.60:22122 #My ip address for CentOS7 
8.http.server_port=8888 #Setting the http port number

The complete information is as follows:

disabled=false
group_name=group1
bind_addr= 0.0.0.0
client_bind=true
port=23000
connect_timeout=30
network_timeout=60
heart_beat_interval=30
stat_report_interval=60
base_path=/opt/fastdfs_storage
max_connections=256
buff_size = 256KB
accept_threads=1
work_threads=4
disk_rw_separated = true
disk_reader_threads = 1
disk_writer_threads = 1
sync_wait_msec=50
sync_interval=0
sync_start_time=00:00
sync_end_time=23:59
write_mark_file_freq=500
store_path_count=1
store_path0=/opt/fastdfs_storage_data
subdir_count_per_path=256
tracker_server=192.168.43.60:22122
log_level=info
run_by_group=
run_by_user=
allow_hosts=*
file_distribute_path_mode=0
file_distribute_rotate_count=100
fsync_after_written_bytes=0
sync_log_buff_interval=10
sync_binlog_buff_interval=10
sync_stat_file_interval=300
thread_stack_size=512KB
upload_priority=10
if_alias_prefix=
check_file_duplicate=0
file_signature_method=hash
key_namespace=FastDFS
keep_alive=0
use_access_log = false
rotate_access_log = false
access_log_rotate_time=00:00
rotate_error_log = false
error_log_rotate_time=00:00
rotate_access_log_size = 0
rotate_error_log_size = 0
log_file_keep_days = 0
file_sync_skip_invalid_record=false
use_connection_pool = false
connection_pool_max_idle_time = 3600
http.domain_name=
http.server_port=8888

Create Soft References after Modification and Save

[root@localhost fdfs]#  ln -s /usr/bin/fdfs_storaged /usr/local/bin

3. Start Storage

[root@localhost fdfs]# service fdfs_storaged start

Set boot start:

[root@localhost fdfs]#  echo "service fdfs_storaged start" |tee -a /etc/rc.d/rc.local

Check to see if the service is started

[root@localhost fdfs]# netstat -unltp | grep fdfs 
tcp        0      0 0.0.0.0:22122           0.0.0.0:*               LISTEN      3088/fdfs_trackerd  
tcp        0      0 0.0.0.0:23000           0.0.0.0:*               LISTEN      3139/fdfs_storaged  

4. Verification Integration

At this point, everything for fastdfs has been installed. Finally, we need to make sure that storage is registered with tracker.
View commands:

[root@localhost fdfs]# /usr/bin/fdfs_monitor /etc/fdfs/storage.conf

After success, we can see that:
ip_addr = 192.168.43.60 (localhost.localdomain) ACTIVE

Four, test

1. Configuring Client

Modify client.conf file in / etc/fdfs/directory
The main points of modification are as follows:

base_path=/opt/fastdfs_tracker #tracker server file path
tracker_server=192.168.43.60:22122 #tracker server IP address and port number
http.tracker_server_port=8080 # The http port number of the tracker server must correspond to the settings of the tracker

The complete client.conf file information is as follows:

connect_timeout=30
network_timeout=60
base_path=/opt/fastdfs_tracker
tracker_server=192.168.43.60:22122
log_level=info
use_connection_pool = false
connection_pool_max_idle_time = 3600
load_fdfs_parameters_from_tracker=false
use_storage_id = false
storage_ids_filename = storage_ids.conf
http.tracker_server_port=8080

Analog upload
Upload a picture from the personal user directory for testing

[root@localhost fdfs]# fdfs_upload_file  /etc/fdfs/client.conf /home/zhangyongliang/9408.jpg  #The position of the picture is behind it.

When successful, the image storage path will be returned.

[root@localhost fdfs]# fdfs_upload_file  /etc/fdfs/client.conf /home/zhangyongliang/9408.jpg 
group1/M00/00/00/wKgrPFpe9OqAWsHxAAH5yvc2jn8251.jpg

Group name: group 1
Disk: M00
Catalogue: 00/00
File name: wKgrPFpe9OqAWsHxAAH5yvc2jn8251.jpg
The location of the uploaded file is as follows:

[root@localhost fdfs]# ll /opt/fastdfs_storage_data/data/00/00/
//Total dosage 256
-rw-r--r--. 1 root root 129482 1 Month 1715:02 wKgrPFpe9OqAWsHxAAH5yvc2jn8251.jpg
-rw-r--r--. 1 root root 129482 1 Month 1711:53 wKgrPFpeyM2ATkGUAAH5yvc2jn8013.jpg

There are created multi-level directories in the actual file storage path. There are 256 1-level directories under data, and 256 2-level subdirectories under each level, totaling 65536 files. Newly written files are routed to one of these subdirectories in hash mode, and then the file data is stored directly as a local file in the directory.

If we want to access the newly uploaded image, we need to combine nginx to achieve it.

5. Install Nginx and implement configuration

1. Install Nginx dependency environment

[root@localhost fdfs]# yum -y install pcre pcre-devel  
[root@localhost fdfs]# yum -y install zlib zlib-devel  
[root@localhost fdfs]# yum -y install openssl openssl-devel

2. Install nginx and add fastdfs-nginx-module

Unzip nginx and fastdfs-nginx-module

[root@localhost fdfs]# tar -zxvf nginx-1.10.3.tar.gz
[root@localhost fdfs]# unzip fastdfs-nginx-module-master.zip

After decompression, compile and install nginx in nginx directory, and add fastdfs-nginx-module

[root@localhost nginx-1.10.3]# ./configure --prefix=/usr/local/nginx --add-module=/home/zhangyongliang/apps/fastdfs-nginx-module-master/src #Location of fastdfs-nginx-module after decompression

Then compile and install

[root@localhost nginx-1.10.3]# make && make isntall

After successful installation, nginx will be installed in / usr/local/nginx. Check after installation

[root@localhost src]# ll /usr/local/nginx/
//Total dosage 8
drwx------. 2 nobody root    6 1 Month 1713:23 client_body_temp
drwxr-xr-x. 2 root   root 4096 1 Month 1713:17 conf
drwx------. 2 nobody root    6 1 Month 1713:23 fastcgi_temp
drwxr-xr-x. 2 root   root   40 1 Month 1713:17 html
drwxr-xr-x. 2 root   root   58 1 Month 1713:49 logs
-rw-r--r--. 1 root   root 1156 1 Month 1713:29 nginx.conf
drwx------. 2 nobody root    6 1 Month 1713:23 proxy_temp
drwxr-xr-x. 2 root   root   19 1 Month 1713:17 sbin
drwx------. 2 nobody root    6 1 Month 1713:23 scgi_temp
drwx------. 2 nobody root    6 1 Month 1713:23 uwsgi_temp

After successful installation, nginx is not running, and there are no temporary folders in the nginx folder, such as fastcgi_temp files.

3. Configure Storage Nginx

Modify the configuration file nginx.conf under the Nginx directory and add location information as follows:

server {
        listen       9991;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }

        location ~/group1/M00 {
            root /opt/fastdfs_storage/data;
            ngx_fastdfs_module;
        }

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

Then enter the unzipped directory of FastDFS installation and copy http.conf and mime.types to / etc/fdfs directory:

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

In addition, it is necessary to copy mod_fastdfs.conf in the src directory of the fastdfs-nginx-module installation directory to the / etc/fdfs directory:

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

Modify the mod_fastdfs.conf file just copied:

base_path=/opt/fastdfs_storage  #Save Log Directory
tracker_server=192.168.43.60:22122 #IP address and port number of tracker server
storage_server_port=23000 #Port number of storage server
url_have_group_name = true #Does the group name exist in the file url?
store_path0=/opt/fastdfs_storage_data   #Storage path
group_count = 1 #Set the number of groups

At the end of the file, set the group

[group1]
group_name=group1
storage_server_port=23000
store_path_count=1
store_path0=/opt/fastdfs_storage_data

Create a symbolic connection from M00 to storage directory:

ln  -s  /opt/fastdfs_storage_data/data/ /opt/fastdfs_storage_data/data/M00

Start Nginx:

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

Accessing Nginx to Start

[root@localhost src]# curl localhost:9991
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

4. Configure Tracker Nginx

Add a virtual machine to the nginx.conf file

upstream fdfs_group1 {
        server 127.0.0.1:9991;
    }
   server {
        listen       80;
        server_name  localhost;
       location /group1/M00 {
            proxy_pass http://fdfs_group1;
        }
      error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

The complete nginx.conf configuration file is as follows:


#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       9991;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }
        location ~/group1/M00 {
            root /opt/fastdfs_storage/data;
            ngx_fastdfs_module;
        }
        #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;
        #}
    }

    upstream fdfs_group1 {
       server 127.0.0.1:9991;
    }
     server {
        listen       80;
        server_name  localhost;
       location /group1/M00 {
            proxy_pass http://fdfs_group1;
        }
      error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    # 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;
    #    }
    #}

}

Restart Nginx

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

Has access to Nginx been started

[root@localhost src]# curl localhost:9991
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@localhost src]# curl localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

5.HTTP Access to Image Resources

Access Picture Resource Path http://192.168.43.60/group1/M00/00/00/wKgrPFpeyM2ATkGUAAH5yvc2jn8013.jpg

FastDFS resource access screenshot

Description: If the image resources are not accessible, please close the system firewall.
You can see the pictures that show that you have successfully built the file system of FastDFS stand-alone.

Keywords: Nginx PHP yum curl

Added by Tanus on Sun, 19 May 2019 06:41:55 +0300