FastDFS is an open source lightweight distributed file system. It manages files, including file storage, file synchronization, and file access (file upload and download). It solves the problems of large-capacity storage and load balancing. It is especially suitable for online services with file as the carrier, such as photo album website, video website and so on. FastDFS is tailor-made for the Internet. It takes into account redundant backup, load balancing, and linear expansion, and emphasizes high availability and performance. It is easy to set up a high-performance file server cluster to provide file uploading and downloading services.

1. Download and install libfastCommon

Libfastcommon is a public C function library extracted from FastDFS and FastDHT.

1.1 download:
Wget HTTP: / / https://github.com/happyfish100/libfastcommon/archive/V1.0.7.tar.gzCopy the code
1.2 unzip the:
The tar - ZXVF V1.0.7. Tar. GzCopy the code
1.3 installation:
cdLibfastcommon - 1.0.7. / make. Sh. / make. Sh installCopy the code
1.4 Copying files:

Fix lib configuration file path in FastDFS.

cp /usr/lib64/libfastcommon.so /usr/local/lib/libfastcommon.so
cp /usr/lib64/libfastcommon.so /usr/lib/libfastcommon.so
Copy the code

2. Download and install FastDFS

2.1 download:
Wget HTTP: / / https://github.com/happyfish100/fastdfs/archive/V5.05.tar.gzCopy the code
2.2 unzip the:
The tar - ZXVF V5.05. Tar. GzCopy the code
2.3 installation:
cdFastdfs - 5.05. / make. Sh. / make. Sh installCopy the code

After the installation is complete, the default configuration file directory is /etc/fdfs/and the default commands are stored in /usr/bin/and start with fdfs_.

3. The configuration FastDFS

First copy the configuration file to the /etc/fdfs directory.

cd/ home/fantj/download/fastdfs - 5.05 / conf/etc/FDFS/cp * is as follows: [root @ s168 conf]# cd /etc/fdfs/
[root@s168 fdfs]# ls
anti-steal.jpg  client.conf.sample  mime.types    storage.conf.sample  tracker.conf
client.conf     http.conf           storage.conf  storage_ids.conf     tracker.conf.sample
Copy the code

3.1. The configuration tracker. Conf

FastDFS tracker

Modify the following configuration:

#Tracker data and log directory address (root directory must exist, subdirectories will be created automatically)
base_path=/fastdfs/tracker
port=22122
Copy the code
3.1.1 Creating a Directory

mkdir -p /fastdfs/tracker

3.1.2 Start/Stop:

fdfs_trackerd /etc/fdfs/tracker.conf start/stop

By default, the configuration file in /etc/fdfs is used. If there is a specified configuration, you can add parameters to it. Enable firewall rules, if any.

3.1.3 Setting startup:
# systemctl enable fdfs_trackerdOr:# vim /etc/rc.d/rc.localAdd the configuration: /etc/init.d/fdfs_trackerd startCopy the code
3.1.4 Tracker Server Directory and File Structure:

After the Tracker service is successfully started, the data and logs directories are created in base_path. The directory structure is as follows:

${base_path}| __data | | __storage_groups. Dat: storage grouping information | | __storage_servers. Dat: storage server list | __logs | | __trackerd. Log: the tracker server log filesCopy the code

3.2. The configuration storage

The following is the basic configuration, but if you need more details, look at all the configurations.

group_name=fantj     # group name
base_path=/fastdfs/storage   # log directory
store_path0=/fastdfs/storage     # store directoryTracker_server = 192.168.27.168:22122# tracker node
http.server_port=8888    # port
Copy the code
3.2.1 start
Fdfs_storaged /etc/ffs/storage. conf start fdfs_storaged /etc/ffs/storage. conf start fdfs_storaged /etc/ffs/storage. conf# service fdfs_storaged start
Copy the code
3.2.2 Checking the Process

netstat -unltp|grep fdfs

3.2.3 Storage Startup
# systemctl enable fdfs_storagedOr:# vim /etc/rc.d/rc.localAdd the following configuration: /etc/init.d/fdfs_storaged startCopy the code
3.2.4 Storage directory

After the Storage is successfully started, the data and logs directories are created in base_path to record information about the Storage Server.

In the store_path0 directory, N*N subdirectories are created:

[root@s168 data]# pwd
/fastdfs/storage/data
[root@s168 data]# ls
00  07  0E  15  1C  23  2A  31  38  3F  46  4D  54  5B  62  69  70  77  7E  85  8C  93  9A  A1  A8  AF  B6  BD  C4  CB  D2  D9  E0  E7  EE  F5  FC
01  08  0F  16  1D  24  2B  32  39  40  47  4E  55  5C  63  6A  71  78  7F  86  8D  94  9B  A2  A9  B0  B7  BE  C5  CC  D3  DA  E1  E8  EF  F6  FD
02  09  10  17  1E  25  2C  33  3A  41  48  4F  56  5D  64  6B  72  79  80  87  8E  95  9C  A3  AA  B1  B8  BF  C6  CD  D4  DB  E2  E9  F0  F7  fdfs_storaged.pid
03  0A  11  18  1F  26  2D  34  3B  42  49  50  57  5E  65  6C  73  7A  81  88  8F  96  9D  A4  AB  B2  B9  C0  C7  CE  D5  DC  E3  EA  F1  F8  FE
04  0B  12  19  20  27  2E  35  3C  43  4A  51  58  5F  66  6D  74  7B  82  89  90  97  9E  A5  AC  B3  BA  C1  C8  CF  D6  DD  E4  EB  F2  F9  FF
05  0C  13  1A  21  28  2F  36  3D  44  4B  52  59  60  67  6E  75  7C  83  8A  91  98  9F  A6  AD  B4  BB  C2  C9  D0  D7  DE  E5  EC  F3  FA  storage_stat.dat
06  0D  14  1B  22  29  30  37  3E  45  4C  53  5A  61  68  6F  76  7D  84  8B  92  99  A0  A7  AE  B5  BC  C3  CA  D1  D8  DF  E6  ED  F4  FB  sync
Copy the code

3.3 configure the client

Vim client. Conf tracker_server = 192.168.27.168:22122# tracker node
base_path=/fastdfs/client   # log path
Copy the code

3.4. Local picture upload test

[root@s168 fdfs]# fdfs_upload_file /etc/fdfs/client.conf /home/test.png Fantj/M00/00/00 / wKgbqFu7T7iAJh7lAABDYbhAMC4812. PNG or: [root @ s168 FDFS]# fdfs_test /etc/fdfs/client.conf /home/test.png 
This is FastDFS client testCopyright (C) 2008, Happy Fish / YuQing FastDFS may be copied only under the terms of the GNU General Public License V3,which may be found in the FastDFS source kit.
Please visit the FastDFS Home Page http://www.csource.org/ 
for more detail.

[2018-5-08 20:41:15] DEBUG - base_path=/fastdfs/client, connect_timeout=30, network_timeout=60, tracker_server_count=1, anti_steal_token=0, anti_steal_secret_key length=0, use_connection_pool=0, g_connection_pool_max_idle_time=3600s, use_storage_id=0, storage server id count: 0

invalid operation: /home/test.png

Copy the code

4. Install nginx

I have written about nginx installation before, so I will not repeat it here. If not installed, please click:

Yum Install (not recommended because you still need to recompile nginx from source code) : Check out my article: Nginx Install

Tar package installation:

Wget http://nginx.org/download/nginx-1.12.2.tar.gz tar ZXVF nginx - 1.12.2. Tar. GzcdNginx-1.12.2 Run the following command:./configure make make install mkdir -p /var/tmp/nginx/client/ cp /usr/local/nginx/sbin/nginx /usr/sbin/
# start
nginx 
Copy the code

5. FastDFS configures Nginx module

Fastdfs-nginx-module redirects the file link to the source server to retrieve the file, avoiding file failure caused by replication delay on the client.

5.1 Download and Installation
wget https://github.com/happyfish100/fastdfs-nginx-module/archive/5e5f3566bbfa57418b5506aaefbe107a42c9fcb1.zip
unzip 5e5f3566bbfa57418b5506aaefbe107a42c9fcb1.zip
mv fastdfs-nginx-module-5e5f3566bbfa57418b5506aaefbe107a42c9fcb1/ fastdfs-nginx-module
Copy the code
5.2 configure Nginx
ngix -s stop
cdNginx - 1.12.1# Add module./configure --add-module=.. /fastdfs-nginx-module/ SRC (yum -y install pcre*)# recompile and install
make
make install

# Copy the configuration file
[root@s168 src]# pwd
/home/fantj/download/fastdfs-nginx-module/src
[root@s168 src]# cp mod_fastdfs.conf /etc/fdfs/
Copy the code

Configure the /etc/fdfs/mod_fastdfs.conf file

/etc/fdfs/mod_fastdfs.conf

base_path=/fastdfs/tmp  # log directoryStore_path0 = / fastdfs/storage tracker_server = 192.168.27.168:22122# the group name of the local storage server
group_name=fantj   # corresponds to the groupname of the storage
url_have_group_name = true   # enable group_name attached to url
Copy the code

Configure nginx

# configure nginx
[root@s168 fdfs]# cd /usr/local/nginx/conf/
[root@s168 conf]# vim nginx.conf
Add the following configurationserver { listen 88; Server_name 192.168.27.168; location /fantj/M00{ ngx_fastdfs_module; }}/usr/local/nginx/sbin
[root@s168 sbin]# ./nginx -t
ngx_http_fastdfs_set pid=6431
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
# start nginx
[root@s168 sbin]# ./nginx 
ngx_http_fastdfs_set pid=6436
Copy the code

The service is restarted and tested

Before restarting the service, you need to confirm that the used directory has been created. Mkdir -p/fastdfs / {TMP, tracker, storage, client}, if there is no TMP, nginx will launch a success but cannot access, no error log!

[root@s168 fdfs]# fdfs_trackerd tracker.conf restart
waiting for pid [1202] exit. starting ... [root@s168 fdfs]# fdfs_storaged storage.conf restart
waiting for pid [1211] exit. starting ... [root@s168 fdfs]# /usr/local/nginx/sbin/nginx -s reload
ngx_http_fastdfs_set pid=6463
Copy the code

6. The configuration is tested successfully