fastDFS File Server Setup

Posted by kritro on Fri, 16 Aug 2019 18:13:25 +0200

FastDFS is an open source, lightweight, distributed file system that manages files, including file storage, file synchronization, file access (file upload, file download), etc. It solves the problems of mass storage and load balancing.It is especially suitable for online services with files as carriers, such as photo album websites, video websites, etc.
FastDFS is tailored for the Internet, taking full account of redundant backup, load balancing, linear expansion and other mechanisms, and focusing on high availability, high performance and other indicators, it is easy to set up a high-performance file server cluster using FastDFS to provide file upload, download and other services.

Virtual Machine Environment
 liunx server (1): 192.168.124.241 (tracker)
linux server (2): 192.168.124.242 (storage)
Link: https://pan.baidu.com/s/1duk5TO0qupC6LEYhR4AKzA 
Extraction Code: k67x 

Install Dependent Packages
yum -y install pcre pcre- gcc-c++ gcc* zlib zlib-devel openssl openssl-devel gc-devel gd gd-devel git

Put all packages in / usr/local/src tar-zxvf and unzip all to / data/server/fastDFS directory

install libfastcommon
mkdir  -p /data/server/fastDFS
cd /data/server/fastDFS/libfastcommon-1.0.35
./make.sh
./make install
install libevent
cd /data/server/fastDFS/libevent-2.0.20-stable 
./configure --prefix=/data/server
make clean
make
make install
Create a tracker directory (this step is only performed when a tracker is installed)
mkdir -p /fdfs/tracker
tracker Installation (192.168.124.241)
Tar-zxvf FastDFS_v5.08.tar.gz (moved to/data/server after SRC decompression)
mv FastDFS /data/server/fastDFS/
Compile and install tracker
cd /data/server/fastDFS/FastDFS
./make.sh
./make.sh install

Configure tracker
cd /data/server/fastDFS/FastDFS/conf
vim tracker.conf

base_path=/fdfs/tracker   #Setting tracker's data files and log directories
max_connections=300      #maximum connection
work_threads=1            #Number of worker threads, preferably the same as cpu cores
http.server_port=8080     #Set http port number  

Function
/data/server/fastDFS/FastDFS/tracker/fdfs_trackerd /data/server/fastDFS/FastDFS/conf/tracker.conf
View tracker startup file information
cat /fdfs/tracker/logs/trackerd.log

Set tracker to boot-on self-start after confirming successful startup
vim /etc/rc.d/rc.local
Add on last line
/data/server/fastDFS/FastDFS/tracker/fdfs_trackerd /data/server/fastDFS/FastDFS/conf/tracker.conf

Install nginx on tracker (192.168.31.128)

Create Cache Directory
mkdir -p /var/cache/nginx/proxy_cache
Installation Package Preparation

nginx Source Package
nginx-1.11.5.tar.gz
fastDFS Related Plug-in Packages
ngx_cache_purge-2.3.tar.gz
pcre-8.34.tar.gz
zlib-1.2.8.tar.gz

The final image is shown below

cd /data/server/fastDFS/FastDFS/nginx-1.11.5

./configure --prefix=/data/server/fastDFS/trackerNginx --add-module=/data/server/fastDFS/ngx_cache_purge-2.3   --with-pcre=/data/server/fastDFS/pcre-8.34/ --with-zlib=/data/server/fastDFS/zlib-1.2.8

make compilation
make
make install

Configure nginx
Modify the nginx.conf file in the trackerNginx directory (my configuration is as follows)

#user  nobody;
worker_processes  1;
worker_cpu_affinity 1;
worker_rlimit_nofile 65535;
error_log  /data/server/fastDFS/trackerNginx/logs/error.log;
pid       /data/server/fastDFS/trackerNginx/logs/nginx.pid;
#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;
  use epoll;
}

http {
  server_names_hash_bucket_size 128;
  client_header_buffer_size 32k;
  large_client_header_buffers 4 32k;
  client_max_body_size 300m;
  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  /data/server/fastDFS/trackerNginx/logs/access.log  main;
  sendfile        on;
  #tcp_nopush     on;
  keepalive_timeout  65;

  gzip  on;
  proxy_redirect off;
  proxy_set_header Host $http_host;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_connect_timeout 90;
  proxy_send_timeout 90;
  proxy_read_timeout 90;
  proxy_buffer_size 16k;
  proxy_buffers 4 64k;
  proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
  #Set cache storage path, storage method, allocated memory size, disk maximum space, cache duration
  proxy_cache_path /var/cache/nginx/proxy_cache levels=1:2 keys_zone=http-cache:500m max_size=10g inactive=30d;
  proxy_temp_path /var/cache/nginx/proxy_cache/tmp;
  #Set up the head er server
  upstream fdfs_head {
      server 192.168.124.242:8080  fail_timeout=30s;
  }
  #Set up an other server
  upstream fdfs_other {
      server 192.168.124.242:8080  fail_timeout=30s;
  }
  server {
      listen       8888;
      server_name  localhost;
  #Setting load balancing parameters for head
      location /head/M00 {
          proxy_next_upstream http_502 http_504 error timeout invalid_header;
          proxy_cache http-cache;
          proxy_cache_valid  200 304 12h;
          proxy_cache_key $uri$is_args$args;
          proxy_pass http://fdfs_head;
          expires 30d;
      }

  #Setting load balancing parameters for other
      location /other/M00 {
          proxy_next_upstream http_502 http_504 error timeout invalid_header;
          proxy_cache http-cache;
          proxy_cache_valid  200 304 12h;
          proxy_cache_key $uri$is_args$args;
          proxy_pass http://fdfs_other;
          expires 30d;
      }

  location ~ /purge(/.*) {
          allow 127.0.0.1;
          allow 192.168.124.0/24;
          deny all;
          proxy_cache_purge http-cache  $1$is_args$args;
      }
      #charset koi8-r;
 #access_log  logs/host.access.log  main;

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

      #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;
      #}
  }

 }

Modify client.conf file Note: Both machines need to be modified
vim /data/server/fastDFS/FastDFS/conf/client.conf
base_path=/fdfs/tracker #log storage path (tracker and storage are different)
tracker_server=192.168.124.241:22122
http.tracker_server_port=8080 #tracker server's http port number

++++++++++++
Install nginx in storage (192.168.124.242)
Install the corresponding dependency packages libfastcommon and libevent (unzip to the / data/server/fastDFS directory and compile as on machine 241)
mkdir -p /data/server/fastDFS
Mkdir-p/fdfs/head/data #Store avatar files
Mkdir-p/fdfs/other/data #Store other files
Mkdir-p/fdfs/storage #storage data node

To configure

cd /data/server/fastDFS/FastDFS/conf/

vim storage.conf
disabled=false                    #Enable Profile
max_connections=300      #maximum connection
work_threads=1            #Number of worker threads, preferably the same as cpu cores
group_name=head                   #Group name, currently we have 3 (head, item, other)
port=23000                        #Set the port number of storage (head, item, other)
base_path=/fdfs/storage           #Set up a log directory for storage (pre-created)
store_path_count=1                #Number of storage paths, need to match number of store_path s
store_path0=/fdfs/head         #Storage Path
tracker_server=192.168.41.241:22122 #IP address and port number of tracker server
http.server_port=8080             #Set http port number

cd /fdfs/head/data
ls
As shown in the following figure

See if the log started successfully
/data/server/fastDFS/FastDFS/storage/fdfs_storaged /data/server/fastDFS/FastDFS/conf/storage.conf

cat /fdfs/storage/logs/storaged.log

Install nginx in storage (192.168.41.242)
Installation Package Preparation

nginx Source Package
nginx-1.11.5.tar.gz
fastDFS Related Plug-in Packages
fastdfs-nginx-module_v1.16.tar.gz
pcre-8.34.tar.gz
zlib-1.2.8.tar.gz

Unzip nginx and all other plug-in packages under/usr/local/fastDFS/

Modify the path parameters in the config file under fastdfs-nginx-module/src, otherwise the command below will not compile.
vim /data/server/fastDFS/FastDFS/fastdfs-nginx-module/src/config
 Find line 4, changed to
CORE_INCS="$CORE_INCS /usr/local/include/fastdfs /usr/local/include/fastcommon/"
Modified to
CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"

Install nginx
cd /data/server/fastDFS/FastDFS/nginx-1.11.5

./configure --prefix=/data/server/fastDFS/trackerNginx --add-module=/data/server/fastDFS/ngx_cache_purge-2.3   --with-pcre=/data/server/fastDFS/pcre-8.34/ --with-zlib=/data/server/fastDFS/zlib-1.2.8

make compilation
make
make install
copy the configuration file of the nginx plug-in module of FastDFS to the FastDFS configuration file directory
cp /data/server/fastDFS/fastdfs-nginx-module/src/mod_fastdfs.conf /data/server/fastDFS/FastDFS/conf/mod_fastdfs.conf

Configure nginx

worker_processes  1;
events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

sendfile        on;

keepalive_timeout  65;
 server {
        listen       8080;
        server_name  192.168.124.242;
location ~/head/M00 {
            root   /fdfs/head/data;
            ngx_fastdfs_module;
        }

        location ~/other/M00 {                  #Group other
            root /fdfs/other/data;
            ngx_fastdfs_module;
        }
error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
}

Configure mod_fastdfs.conf
vim /data/server/fastDFS/FastDFS/conf/mod_fastdfs.conf

connect_timeout=2
network_timeout=30
base_path=/fdfs/storage
load_fdfs_parameters_from_tracker=true
storage_sync_file_max_delay = 86400
use_storage_id = false
storage_ids_filename = storage_ids.conf
tracker_server=192.168.124.241:22122
storage_server_port=23000
group_name=group1
url_have_group_name = true
store_path_count=1
store_path0=/home/yuqing/fastdfs
log_level=info
log_filename=
response_mode=proxy
if_alias_prefix=
flv_support = true
flv_extension = flv
group_count = 2
[group1]
group_name=head
storage_server_port=23000
store_path_count=1
store_path0=/fdfs/head
[group2]
group_name=other
storage_server_port=23001
store_path_count=1
store_path0=/fdfs/other

Start storage for other group after modification
/data/server/fastDFS/FastDFS/storage/fdfs_storaged /data/server/fastDFS/FastDFS/conf/storage_other.conf restart
Check to see if the other directory generated 256 subdirectories, and if it did, it succeeded
cd /fdfs/other/data/
ls
As shown in the following figure

Add Start-Up Self-Start
vim /etc/rc.d/rc.local
Last line joined
/usr/local/fastDFS/FastDFS/storage/fdfs_storaged /data/server/fastDFS/FastDFS/conf/storage_other.conf

Nginx running 242 first nginx running 241
Two machines shut down the firewall
systemctl stop firewalld
setenforce
Executing the upload command generates a fileid
/data/server/fastDFS/FastDFS/client/fdfs_upload_file /data/server/fastDFS/FastDFS/conf/client.conf /home/test.png

Browser access ip:8080/fileid

Topics: Linux Nginx zlib PHP OpenSSL