Title: CentOS Sets up the FastDFS file system on a single node

This example sets up a FastDFS file management system on CentOS7.2

Upload all required files to baidu cloud disk at FastDFS. The installation list is as follows:

The name of the software version Baidu cloud disk storage name
FastDFS 5.11 Fastdfs – 5.11. Zip
FastDFS-Nginx-module There is no fastdfs-nginx-module-master.zip
LibFastCommon 1.0.36 Libfastcommon – 1.0.36. Zip
nginx 1.10.3 Nginx – 1.10.3. Tar. Gz

Install FastDFS

1. Install libfastcommon

Decompress the installation package to a directory

[root@localhost fastDFS]# unzip libfastcommon - 1.0.36. Zip
Copy the code

The decompressed directory is as follows:

[root @ localhost fastdfs 5.11]# ll[root @ localhost libfastcommon - 1.0.36]# ll32 drwxr-xr-x. 2 root root 117 4月 5 2017 doc-rw-r --r--. 1 root root 8005 4月 5 2017 history-rw-r --r-- 566 4月 5 2017 INSTALL -rw-r--r--. 1 root root 1606 4月 5 2017 libfastcommon.spec -rwxr-xr-x. 1 root root 3099 4月 5 2017 Make. Sh drwxr-xr-x. 2 root root 191 4月 5 2017 php-fastcommon-rw-r --r-- 1 root root 2763 4月 5 2017 README drwxr-xr-x Root root 4096 January 17 11:21 SRCCopy the code

Install the C compiler GCC

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

Compile and install the libfastcommon software

[root @ localhost fastdfs 5.11]# ./make.sh && ./make.sh install
Copy the code

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
Copy the code

2. Install FastDFS

Decompress the installation package

[root@localhost fastDFS]# unzip fastdfs - 5.11. Zip
Copy the code

The decompressed directory is as follows:

Total amount 128 drwxr-xr-x. 3 Root root 4096 Jan 17 11:25 Client drwxr-xr-x. 2 root root 4096 Jan 17 11:25 Common DRwxr-xr-x Root root 146 6月 3 2017 conf-rw-r --r--. 1 Root root 35067 6月 3 2017 copy-3_0.txt - RW-r --r--. 1 root root 3171 6月 3 2017 fastdfs.spec -rw-r--r-- 1 root root 33100 6月 3 2017 HISTORY drwxr-xr-x. 2 root root 48 6月 3 2017 init.d -rw-r--r--. 1 root root 7755 6月 3 2017 INSTALL -rwxr-xr-x. 1 root root 5548 6月 3 2017 make 4096 6月 3 2017 php_client-rw-r --r--. 1 root root 2380 6月 3 2017 readme. md-rwxr-xr-x. 1 root root 1768 6月 3 2017 php_client-rw-r --r-- 1 root root 1680 6月 3 2017 stop.sh drwxr-xr-x. 4 root root 4096 1月 17 11:25 storage drwxr-xr-x Root root 4096 June 3 2017testDrwxr-xr-x. 2 root root 4096 1月 17 11:25 trackerCopy the code

Go to the decompressed directory and compile and install the software

[root@localhost fastDFS]# CD fastdfs - 5.11 /[root @ localhost fastdfs 5.11]# ./make.sh && ./make.sh install
Copy the code

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

[root @ localhost fastdfs 5.11]# ll /etc/fdfs/Total usage 76 -rw-r--r--. 1 root root 316 January 17 11:47 client.conf -rw-r--r--. 1 root root 1461 January 17 11:25 client.conf.sample 76 -rw-r--r-- -rw-r--r-- 1 root root 955 1月 17 13:20 http.conf -rw-r--r-- 1 root root 31172 1月 17 13:21 MIME. Types -rw-r--r-- 1 Root root 3716 1月 17 12:57 mod_fastdfs.conf -rw-r--r--. 1 root root 1278 1月 17 11:40 storage.conf -rw-r--r-- Root 7927 January 17 11:25 storage.conf.sample -rw-r--r-- 1 root root 105 January 17 11:25 storage_ids.conf.sample -rw-r--r-- 1 Root root 1356 1月 17 11:34 tracker.conf -rw-r--r--. 1 root root 7389 1月 17 11:25 tracker.conf.sampleCopy the code

We need to make a copy of these three sample files and remove the. Sample

[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
Copy the code

The FastDFS installation is complete

Install Tracker

1. Create a tracker working directory

This directory is used to store data and logs for the tracker

[root@localhost fdfs]# mkdir /opt/fastdfs_tracker
Copy the code

2. Configure the tracker

To configure tracker.conf in the /etc/fdfs directory, perform the following operations:

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 80805. Bind_addr = 0.0.0.0 Listening ADDRESSCopy the code

The complete tracker.conf file contains the following information:

disabled=falseBind_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
Copy the code

Create a soft reference after saving the modification

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

4. Start the Tracker and add the boot option

[root@localhost fdfs]# service fdfs_trackerd start
Copy the code

Create a tracker directory and find that there are two more directories: data and log

[root@localhost fdfs]# ll /opt/fastdfs_tracker/Drwxr-xr-x. 2 root root 178 1月 17 13:48 data drwxr-xr-x. 2 root root 26 1月 17 11:35 logsCopy the code

Add tracker to the boot option

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

Check the port monitoring status of the tracker

[root@localhost fdfs]# netstat -unltp|grep fdfsTCP 0 0 0.0.0.0:22122 0.0.0.0:* LISTEN 3088/ fDFs_trackerdCopy the code

Install Storage

1. Configure a working directory for storage

Because a storage requires a directory for storing data, two more directories are created: fastDFs_storage_data and fastDFs_storage

[root@localhost opt]# mkdir fastdfs_storage
[root@localhost opt]# mkdir fastdfs_storage_data
[root@localhost opt]# llDrwxr-xr-x. 4 Root root 30 January 17 11:45 fastDFs_storage drwxr-xr-x. 3 Root root 18 January 17 11:45 fastDFs_storage_data Drwxr-xr-x. 4 root root 30 1月 17 11:35 fastdfs_trackerCopy the code

2. Configure the storage file

Modify the storage.conf file in the /etc/fdfs directory as follows:

1.disabled=false 
2.group_name=group1 The group name can be changed based on the actual situation
3.port=23000 Set the port number of the storage group to 23000
4.base_path=/opt/fastdfs_storage Set storage data file and log directory
5.store_path_count=1 Number of storage paths, which must match store_path number
6.store_path0=/opt/fastdfs_storage_data The actual file storage path7. Tracker_server = 192.168.43.60:22122# my CentOS7 IP address
8.http.server_port=8888 Set the HTTP port number
Copy the code

The full information is as follows:

disabled=falseGroup_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 = truedisk_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
Copy the code

Create a soft reference after saving the modification

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

3. Start the Storage

[root@localhost fdfs]# service fdfs_storaged start
Copy the code

Set boot:

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

Check to see if the service is started

[root@localhost fdfs]# netstat -unltp | grep fdfs TCP 00 0.0.0.0:22122 0.0.0.0:* LISTEN 3088/ fDFs_trackerd TCP 00 0.0.0.0:23000 0.0.0.0:* LISTEN 3139/ fDFs_storagedCopy the code

4. Check and integrate

At this point, the Fastdfs setup is complete, and 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
Copy the code

The command output is as follows: ip_addr = 192.168.43.60 (localhost.localdomain) ACTIVE

Four, test,

1. Configure the client

Modify the client.conf file in the /etc/fdfs/directory as follows:

base_path=/opt/fastdfs_tracker #tracker Specifies the server file pathTracker_server = 192.168.43.60:22122#tracker Specifies the server IP address and port number
http.tracker_server_port=8080 The HTTP port number of the tracker server must correspond to the tracker setting
Copy the code

The complete information about the client.conf file 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
Copy the code

Test by uploading an image from a personal user’s directory

[root@localhost fdfs]# fdfs_upload_file/etc/FDFS/client. The conf/home/zhangyongliang / 9408 JPG # that is placed behind the location of the picture
Copy the code

The image storage path is returned upon success

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

Group name: group1 disk: M00 directory: 00/00 file name: wKgrPFpe9OqAWsHxAAH5yvc2jn8251. JPG position the uploaded files are as follows:

[root@localhost fdfs]# ll /opt/fastdfs_storage_data/data/00/00/Total amount - r - 256 - rw - r. 1 root root on January 17, 129482 15:02 wKgrPFpe9OqAWsHxAAH5yvc2jn8251. JPG - rw - r - r -. 1 root root in January 129482 17 11:53 wKgrPFpeyM2ATkGUAAH5yvc2jn8013.jpgCopy the code

A multi-level directory has been created in the actual file storage path. Data has 256 level 1 directories, and each level 2 directory has 256 level 2 subdirectories, 65536 files in total. Newly written files are routed to a subdirectory in hash mode, and file data is directly stored as a local file in this directory. If we want to access the image we just uploaded, we need to use nginx to do this

Install Nginx and implement the configuration

1. Install the Nginx dependent 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
Copy the code

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
Copy the code

Install nginx and add fastdfs-nginx-module to the fastdfs-nginx-module directory

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

Then compile and install

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

Nginx is installed in /usr/local/nginx

[root@localhost src]# ll /usr/local/nginx/Total usage 8 DRWX ------. 2 nobody root 6 1月 17 13:23 client_body_temp drwxr-xr-x. 2 root root 4096 1月 17 13:17 conf DRWX ------ 2 Nobody root 6 January 17 13:23 fastcgi_temp drwxr-xr-x. 2 Root root 40 January 17 13:17 HTML drwxr-xr-x. 2 root root 58 January 17 13:49 logs-rw-r --r--. 1 root root 1156 1月 17 13:29 nginx.conf DRWX ------. 2 nobody root 6 1月 17 13:23 proxy_temp 2 root root 19 January 17 13:17 sbin DRWX ------. 2 Nobody root 6 January 17 13:23 scgi_temp DRWX ------. 2 nobody root 6 January 17 13:23 uWSGI_tempCopy the code

The nginx folder does not have temporary folders, such as fastcgi_temp, when nginx is not yet running.

3. Configure Storage Nginx

Conf file in Nginx directory. 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; }}Copy the code

Copy http.conf and mime.types to /etc/fdfs:

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

Copy mod_fastdfs.conf from SRC in the fastdfs-nginx-module installation directory to /etc/fdfs:

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

Modify the mod_fastdfs.conf file you just copied:

base_path=/opt/fastdfs_storage  # Save log directoryTracker_server = 192.168.43.60:22122#tracker Specifies the IP address and port number of the server
storage_server_port=23000 Storage server port number
url_have_group_name = true Is there a group name in the file URL
store_path0=/opt/fastdfs_storage_data   # storage path
group_count = 1 # set the number of groups
Copy the code

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
Copy the code

Create symbolic link from M00 to storage directory:

ln  -s  /opt/fastdfs_storage_data/data/ /opt/fastdfs_storage_data/data/M00
Copy the code

Nginx start:

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

Whether access to Nginx is enabled

[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>
Copy the code

4. Configure Tracker Nginx

Add a virtual machine to the nginx.conf file

Upstream {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; }}Copy the code

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 {server 127.0.0.1:9991;} upstream {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;
    #}
    #}

}
Copy the code

Restart Nginx

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

Whether access to Nginx has 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>
Copy the code

5.HTTP access to image resources

http://192.168.43.60/group1/M00/00/00/wKgrPFpeyM2ATkGUAAH5yvc2jn8013.jpg to access the resources path

Supplementary notes:

If the Tracker, Storage, or Nginx service does not start automatically after startup, perform the following operations and restart the service

[root@localhost ~]# chkconfig --add fdfs_trackerd
[root@localhost ~]# chkconfig --add fdfs_storaged
Copy the code

Edit /etc/rc.d/rc.local as follows:

#! /bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.

touch /var/lock/subsys/local
/usr/local/nginx/sbin/nginx
Copy the code

Added Nginx startup, after the file effect, restart the system

[root@localhost ~]# chmod +x /etc/rc.d/rc.local
[root@localhost ~]# source /etc/rc.d/rc.local 
[root@localhost ~]# reboot
Copy the code

View the fastDFS connection count

 netstat -antp | grep 23000 | grep ESTABLISHED -c
Copy the code