- Overview
FastDFS is an open-source lightweight distributed file system consisting of three main components: tracker servers, storage servers, and clients.
The system has two primary roles:
- Tracker Service: Handles coordination and load balancing across the cluster.
- Storage Service: Manages file storage, synchronization, and file access interfaces, maintaining file metadata in a key-value format.
Storage servers within the same group communicate with eachother for file synchronization, while servers in different groups operate independently.
- Installation
2.1 Installing libfastcommon
unzip libfastcommon-master.zip
cd libfastcommon-master
yum install gcc
./make.sh && ./make.sh install
2.2 Installing FastDFS
unzip fastdfs-master.zip
cd fastdfs-master
./make.sh && ./make.sh install
2.3 Configuring the Tracker Service
mkdir -pv /data/fastdfs_tracker
Step 1: Modify the configuration file
cd /etc/fdfs
cp tracker.conf.sample tracker.conf
vim tracker.conf
base_path=/data/fastdfs_tracker
Step 2: Start the service
/etc/init.d/fdfs_trackerd start
netstat -lnt | grep 22122
2.4 Configuring the Storage Service
mkdir -pv /data/fastdfs_storage/data
mkdir -pv /data/fastdfs_storage/files
Step 1: Modify the configuration file
cd /etc/fdfs
cp storage.conf.sample storage.conf
vim storage.conf
base_path=/data/fastdfs_storage/data
store_path0=/data/fastdfs_storage/files
tracker_server=192.168.5.20:22122
tracker_server=192.168.5.21:22122
Step 2: Start the service
/etc/init.d/fdfs_storaged start
netstat -lnt | grep 23000
2.5 Verifying Service Registration
Check if the storage server has successfully registered with the tracker:
/usr/bin/fdfs_monitor /etc/fdfs/storage.conf
2.6 Client Configuration
cd /etc/fdfs
cp client.conf.sample client.conf
vim client.conf
base_path=/var/log/fastdfs
tracker_server=192.168.5.20:22122
tracker_server=192.168.5.21:22122
2.7 File Operations
Upload a file:
fdfs_upload_file /etc/fdfs/client.conf /etc/passwd
Donwload a file:
fdfs_download_file /etc/fdfs/client.conf group1/M00/00/00/wKgJFxlpqrAep2zqAAAEINsBJNc3958404
View file information:
fdfs_file_info /etc/fdfs/client.conf group1/M00/00/00/wKgJFxlpqrAep2zqAAAEINsBJNc3958404
Delete a file:
fdfs_delete_file /etc/fdfs/client.conf group1/M00/00/00/wKgJFxlpqrAep2zqAAAEINsBJNc3958404
Append operation:
echo content1 > part1.txt
echo content2 > part2.txt
fdfs_upload_appender /etc/fdfs/client.conf part1.txt
# Download to verify content matches part1.txt
fdfs_append_file /etc/fdfs/client.conf group1/M00/00/00/wKgCDFhQDwuET0QeAAAAAEd3frE088.txt part2.txt
# Download again to verify combined content
2.8 Removing a Storage Node
# Step 1: Stop the storage service
/etc/init.d/fdfs_storaged stop
# Step 2: Remove the node from the cluster
fdfs_monitor /etc/fdfs/client.conf delete group1 192.168.5.21
- PHP Client Integration
# Install PHP and required extensions
yum install php php-devel php-fpm php-mbstring php-common
# Build the PHP client module
cd fastdfs-master/php_client
/usr/bin/phpize
./configure --with-php-config=/usr/bin/php-config
make && make install
# Add configuration to PHP
cat fastdfs_client.ini >> /etc/php.ini
# Test the client
/usr/bin/php fastdfs_test.php
# Verify configuration
/usr/bin/php -i | grep configure
/usr/bin/php -i | grep php.ini
- Java Client Integration
# Verify Java environment
java -version
# Download and build the Java client
# From: https://github.com/happyfish100/fastdfs-client-java
yum install ant
cd fastdfs-client-java/src
ant
# Generated JAR: /root/fastdfs-client-java/src/build/fastdfs_client.jar
Upload using Java client:
java -cp fastdfs_client.jar org.csource.fastdfs.test.TestClient /etc/fdfs/client.conf /etc/resolv.conf
Monitor cluster status:
java -cp fastdfs_client.jar org.csource.fastdfs.test.Monitor /etc/fdfs/client.conf
- Nginx Module Configuration
Download from: https://github.com/happyfish100/fastdfs-nginx-module
# Extract the module
unzip fastdfs-nginx-module-master.zip
# Recompile Nginx with the module (backup existing binary first)
cd nginx
./configure --prefix=/app/nginx --add-module=/root/fastdfs-nginx-module/src/
make
cp objs/nginx /app/nginx/sbin/nginx
# Copy configuration files
cd /root/fastdfs-nginx-module/src/
cp mod_fastdfs.conf /etc/fdfs/
cd /root/fastdfs/conf/
cp anti-steal.jpg http.conf mime.types /etc/fdfs/
# Create log file
touch /var/log/mod_fastdfs.log
chown nginx.nginx /var/log/mod_fastdfs.log
# Configure Nginx
vim /etc/nginx/nginx.conf
location /group1/M00 {
root /data/fastdfs_storage/files;
ngx_fastdfs_module;
}
# Configure the module
vim /etc/fdfs/mod_fastdfs.conf
tracker_server=192.168.5.20:22122
tracker_server=192.168.5.21:22122
url_have_group_name=true
store_path0=/data/fastdfs_storage/files
log_filename=/var/log/mod_fastdfs.log
response_mode=proxy
# Restart Nginx
nginx -t
nginx -s stop && nginx
5.1 Testing Upload
fdfs_upload_file /etc/fdfs/client.conf sample_image.jpg
Access the file via browser: http://192.168.5.20/group1/M00/00/00/wKgXhVh545iAP6ETADyH-kmtQ2U888.jpg
Important: In production environments, the fastdfs-nginx-module should be installed on every storage server to enable direct file access.