Installation and use of FastDFS server

Premise introduction

  1. The server part was originally installed by the person in charge of the server, but now there is no such person, so I try to install it myself. All the processes follow the online steps step by step. If you have some problems in the installation process. This article may not help you. But the author's problems will certainly be introduced in the article.
  2. This article shows the installed http version. Later, we will introduce the transformation of http to https. The author didn't install it when writing, and then wanted to make it https after success, but it's troublesome to change the blog. According to the above, the server is not his job. It took a whole day to finish it. It's too hard to change. And many blogs are installed with http. In fact, my https transformation part also has some reference value

FastDFS server installation

Environment introduction

Alibaba cloud CentOS 7.7 64 bit

preparation in advance

Just choose the latest download directly. The version number used in this article has been indicated in [].

Common core package [1.0.58]: https://github.com/happyfish100/libfastcommon/tags

FastDFS package [6.08]: https://github.com/happyfish100/fastdfs/tags

Fastdfs nginx redirection package [1.22]: https://github.com/happyfish100/fastdfs-nginx-module/tags

Nginx redirection tool [1.22.0]: http://nginx.org/en/download.html

Install initial environment

#Install GCC-c++
yum -y install gcc-c++
#Install the libevent Library
yum -y install libevent
#Upload files to the server through Xftp. My location is /usr/local/fastdfs

Install libfastcommon

This must be installed first, otherwise FastDFS installation will report that the path does not exist

#decompression
tar -zxvf libfastcommon-1.0.58.tar.gz
#Please enter the unzipped file path according to your own
cd /usr/local/fastdfs/libfastcommon-1.0.58/
#Compilation and installation
./make.sh
./make.sh install
#After the installation number, you will be prompted that you have added a libfastcommon The so file is created under /usr/lib64 [figure 1]
#Now you need to put this file in the standard path of Linux /usr/lib
# ---------------------------Attention---------------------------
# The new version will be created not only in /usr/lib64 but also in /usr/lib. So there is no need for assistance. If you need to copy, run the following code
# cp /usr/lib64/libfastcommon.so /usr/lib

Figure 1 Parameters for installing libfastcommon

Install FastDFS

#decompression
tar -zxvf fastdfs-6.08.tar.gz
#Please enter the unzipped file path according to your own
cd /usr/local/fastdfs/fastdfs-6.08/
#Compilation and installation [figure 2]
./make.sh
./make.sh install
# At this time, many configuration files appear in the /etc/fdfs/ folder, but a part of the corresponding original folder is missing.
# If you need nginx services, you need all the assistance.
# I need to install nginx here, so I choose to copy all
cp /usr/local/fastdfs/fastdfs-6.08/conf/* /etc/fdfs/
# Ask me to overwrite all select overwrite
\> cp: overwrite '/etc/fdfs/client.conf'? y
\> cp: overwrite '/etc/fdfs/storage.conf'? y
\> cp: overwrite '/etc/fdfs/storage_ids.conf'? y
\> cp: overwrite '/etc/fdfs/tracker.conf'? y
# Create a storage path, which is optional for storage. This path is required for configuration. Attention
mkdir -p /home/fastDFS_data/tracker/
# Modify the configuration file [see Figure 3 for the modified content]
vim /etc/fdfs/tracker.conf
# Start start restart close stop. Just modify the end
fdfs_trackerd /etc/fdfs/tracker.conf start
# It is recommended not to set the automatic startup for the time being. Be careful not to start the machine. I won't paste the instructions here without trying

Figure 2 installing FastDFS parameters

Figure 3 parameters to be modified for fastdfs

Configure Storage side Storage

Security group configuration

The author uses Alibaba cloud server. Please set this step according to the actual situation

Firewall configuration

The author likes to turn on the firewall. If the usual test is to turn off the firewall, this step can be directly ignored

# Open the protective wall
systemctl start firewalld
# Release port
firewall-cmd --zone=public --add-port=22122/tcp --permanent
firewall-cmd --zone=public --add-port=23000/tcp --permanent
# service iptables restart 
systemctl restart firewalld

Configure Storage

# Create a storage path, which is optional for storage. This path is required for configuration. Attention
mkdir -p /home/fastDFS_data/storage
# Modify the configuration file [see Figure 4 for the modified content]
vim /etc/fdfs/storage.conf
# Start service
fdfs_storaged /etc/fdfs/storage.conf start

Figure 4 Storage parameters to be modified

After this step is completed, you need to go to the Storage directory you set to check whether to create the corresponding directory. If it fails, you can check the log.

Configure Client client

# Copy libfdfsclient.so to /usr/lib
# This step is for people who don't have it. My version has been created
cp /usr/local/fastdfs/fastdfs-6.08/client/libfdfsclient.so /usr/lib
# Create a storage path, which is optional for storage. This path is required for configuration. Attention
mkdir -p /home/fastDFS_data/client
# Modify the configuration file [see Figure 5 for the modified content]
vim /etc/fdfs/client.conf

Figure 5 parameters to be modified by the client

Test whether the installation is successful

  1. Check whether the process is started

    netstat -unltp|grep fdfs
    

  2. Upload a file

    fdfs_test /etc/fdfs/client.conf upload  /home/img/123.jpg
    # If you succeed, you will jump out a lot, of which the important thing is to be able to appear the following content
    \> example file url: ~H~ttp://Mosaic /group1/M00/00/00/ag5p7mLbzt-AV2bbAADg_skRKOk283_big.jpg
    # Finally, you need to confirm whether the file is uploaded
    cd /home/fastDFS_data/storage/data/00/00/
    ll
    # ok, we see that the files we uploaded have been saved in it
    

Summary

So far, the whole process of installing fastDFS has ended. If you want to modify ports 22122 and 23000, you can modify them through the tracker configuration file and storage configuration file, which is not fixed.

Nginx service installation

Why install

Web services use Http or Https protocols, which fastdfs cannot recognize. There needs to be something special as an interpreter to help him translate bytecode files. The author of fastdfs provides an interpreter called fastdfs nginx. Therefore, nginx needs to be installed

Install initial environment

#Install PCRE, zlib, openssl
#yum install -y pcre pcre-devel;yum install -y zlib zlib-devel;yum install -y openssl openssl-devel;
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel

Configure the interpreter fastdfs nginx

Delete local tag

# Unzip file
tar -zxvf fastdfs-nginx-module-1.22.tar.gz
# Modify configuration file
vim /usr/local/fastdfs/fastdfs-nginx-module-1.22/src/config
#Modify the content as /usr/local/xxx = = > /usr/xxx
# vim=> indicates that it currently belongs to vim syntax
vim=>:%s/local\///g
vim=>:wq

Modify configuration file

# Copy the configuration file to the previous configuration file directory
cp /usr/local/fastdfs/fastdfs-nginx-module-1.22/src/mod_fastdfs.conf /
etc/fdfs/
# Modify configuration file
vim /etc/fdfs/mod_fastdfs.conf

Install Nginx

# Unzip nginx
tar -zxvf nginx-1.22.0.tar.gz
# Enter niginx folder
cd /usr/local/fastdfs/nginx-1.22.0/
# You must pay attention to the format when entering the configuration command. It is recommended to prepare a notepad, edit it, and then copy and paste it
#./configure --add module=[the absolute path of your fastdfs nginx src]
./configure --add-module=/usr/local/fastdfs/fastdfs-nginx-module-1.22/src
# Wait for a pile of not found
# compile
make
# The following content appears. The installation is successful
# The comment area said that it was easy to make mistakes here, and explained that changing to version 1.16 or below would be successful
# But it is recommended to find the cause of the error first, and then change it if it is not possible
> make[1]: Leaving directory '/usr/local/fastdfs/nginx-1.22.0'
# install
make install
# Enter the installation directory
cd /usr/local/nginx/conf
# Modify the configuration file. See below for the configuration file
vim nginx.conf
# Just find an empty order in the original server and add the following sentence directly
server {
	# If the access path is /group1/M00, it will be directly forwarded to fastdfs nginx for processing instead of the server for processing
	location ~/group1/M00{
		ngx_fastdfs_module;
	}
}
# start nginx 
cd /usr/local/nginx/sbin/
./nginx
# Check whether the service is started
ps -aux | grep nginx
> nginx: master process
> nginx: worker process
# Access the previous path and find that you can connect to the corresponding file through the Internet

Problems of the author

No worker thread or log printing error occurred - file:../tracker/fdfs_ shared_ func. c, line: 474, host "tracker" is invalid, error info: Unknown host
resolvent:

  1. /There is no mime in the etc/fdfs/ folder Types copy mime.types in conf under fastDFS decompression package to /etc/fdfs/
  2. Mod not modified_ I was careless and forgot to save the fastdfs.conf configuration file.

Https transformation

Set the transmitted protocol as the encryption protocol

Security group

firewall

firewall-cmd --zone=public --add-port=443/tcp --permanent
systemctl restart firewalld

Certificate installation

# First, prepare the certificate. The author is Alibaba cloud's certificate. Just download nginx directly
# Enter the conf directory of nginx and create the cert folder. Then upload the downloaded certificate
cd /usr/local/nginx/conf/
mkdir cert
# Unzip file
# If you don't have unzip, install yum install unzip first
unzip Mosaic_nginx.zip
# Edit the nginx file configuration as follows
vim /usr/local/nginx/conf/nginx.conf
# ————————————————————————————This tutorial comes from the official Alibaba cloud tutorial————————————————————————————
# ————————————————————————————These can be copied directly and then modified according to the comments————————————————————————————
#Among the following attributes, attributes beginning with ssl are related to certificate configuration.
server {
	#The default access port of HTTPS is 443.
	#If the default access port of HTTPS is not configured here, Nginx may not start.
	#[need to be modified] if you use Nginx 1.15.0 or above, please use listen 443 ssl instead of listen 443 and ssl on.
	listen 443 ssl;
	#[need to be modified] you need to replace yourdomain with the domain name bound by the certificate.
	server_name yourdomain;
	root html;
	index index.html index.htm;
	# [need to be modified] note that the cert file name is modified to the full name of your certificate. Mine is a number_ Domain name_ nginx
	ssl_certificate cert/cert-file-name.pem;  
	ssl_certificate_key cert/cert-file-name.key; 
	ssl_session_timeout 5m;
	ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
	#Indicates the type of encryption suite used.
	ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3; #Indicates the type of TLS protocol used. You need to evaluate whether to configure TLSv1.1 protocol by yourself.
	ssl_prefer_server_ciphers on;
	location / {
		root html;  #Web site program storage directory.
		index index.html index.htm;
	}
	# Here is your fastFDS service
	# If the access path is /group1/M00, it will be directly forwarded to fastdfs nginx for processing instead of the server for processing
	location ~/group1/M00/{
		ngx_fastdfs_module;
	}
	# I suggest you bring it with you so that you can redirect to a default page
	error_page  404              /404.html;
	error_page   500 502 503 504  /50x.html;
	location = /50x.html {
		root   html;
	}
}

# ————————————————————————————Optional————————————————————————————
# If you want all HTTP accesses to automatically jump to HTTPS pages, you can add the following rewrite statement under the HTTP site that needs to jump
# nginx comes with an 80. My suggestion is to directly cover all, leaving only these
server {
	listen 80;
	# [need to be modified] you need to replace yourdomain with the domain name bound by the certificate.
	server_name yourdomain;
	rewrite ^(.*)$ https://$host$1; # Redirect all HTTP requests to HTTPS through the rewrite instruction.
	location / {
		index index.html index.htm;
	}
}

If you receive an error when restarting the Nginx service, you can use the following methods for troubleshooting:

  • Receive the "ssl" parameter requires ngx_http_ssl_module error: you need to recompile Nginx and add --with HTTP when compiling and installing_ ssl_ Module configuration.
  • Received "/cert/3970497_demo.aliyundoc.com.pem":BIO_new_file() failed (SSL: error:02001002:system library:fopen:No such file or directory:fopen('/cert/3970497_demo.aliyundoc.com.pem','r') error:2006D080:BIO routines:BIO_new_file:no such file) error: you need to remove the front /. For example, you need to remove /cert/cert-file-name.pem and use the correct relative path cert/cert-file-name.pem.

    There are many possibilities for similar problems, almost all of which are in incorrect format. Just change it according to the prompt``

Then it is the process of recompiling and installing

# The first step must be to check whether nginx is started and kill the nginx thread directly. If it is not closed here, it may succeed but not complete
# A special feat of success. Because the above part has been configured, the built-in shutdown will report an error or it is convenient to use kill
# kill according to the port numbers of work and master.
ps -aux|grep nginx
# Kill the process and enter your nginx path
cd /usr/local/fastdfs/nginx-1.22.0/
# Recompile and install 
# Note that the path after --add module= should be the same as your fastdfs nginx location
# Note that --prefix=/usr/local/nginx is not --prefix=/usr/local/nginx/ the article you read is really stupid
./configure --prefix=/usr/local/nginx  --add-module=/usr/local/fastdfs/fastdfs-nginx-module-1.22/src --with-http_ssl_module
make
make install
# Enter the directory after nginx installation
cd /usr/local/nginx/sbin/
# start nginx 
./nginx
# Return the process number to check whether it starts normally. If there are not two processes, check the possible problems of the installation in the http section above. however
# There should be no more mistakes along the way
> nginx: master process
> nginx: worker process
# Finally, visit https:// domain name on the browser, and the following figure appears: perfect success

Modify tracker, storage, and client configuration files

# Enter the configuration file directory
cd /etc/fdfs/ 
# Modify http server_ Port is set to 443, and how to modify it will not be explained here
#Restart service
fdfs_trackerd /etc/fdfs/tracker.conf restart;fdfs_storaged /etc/fdfs/storage restart
# Check whether it is on
netstat -unltp|grep fdfs
# See that both are open, try uploading the file
fdfs_test /etc/fdfs/client.conf upload /home/img/123.jpg
# The upload is successful, and there is no problem with the configuration. Try to access the returned image and change the beginning to https:// domain name //group1
# Perfect display

So far, you have perfectly realized the establishment of FastDFS service. Wuhu!!!!!!!!!!

Delete FastDFS service

Just kidding, I thought there was a problem with the service and deleted it for reinstallation. Who knows it was my mistake. I'm too lazy to delete it

# Out of Service
service fdfs_trackerd stop;service fdfs_storaged stop;
# View pid
ps -ef | grep fdfs
# Killing process xxxx is the result of your query
kill xxxx
# Delete the locally stored configuration and log path. This is the path in your configuration file.
# Note the scope of deletion
rm -rf /home/fastDFS_data/*
# The rest is crazy deletion
# Delete the default installation directory, that is, where your extracted files are.
rm -rf /etc/fdfs
# The rest are almost the same, and they should not have moved. It can be run directly. Delete all directly
rm -rf /usr/bin/fdfs_*;rm -rf /usr/bin/stop.sh/rm -rf /usr/bin/restart.sh;rm -rf /usr/include/fastdfs;rm -rf /usr/include/fastcommon;rm -rf /usr/lib64/libfdfsclient*;rm -rf /usr/lib/libfdfsclient*;

Operate FastDFS through Java

When you come to the familiar code link, the first is dependency injection.

The official one cannot lead into another and various security vulnerabilities. So this is the last choice

<!-- https://mvnrepository.com/artifact/net.oschina.zcx7878/fastdfs-client-java -->
<dependency>
    <groupId>net.oschina.zcx7878</groupId>
    <artifactId>fastdfs-client-java</artifactId>
    <version>1.27.0.0</version>
</dependency>

First, you need to prepare a configuration file. Name it xxxx.conf. I call it fdfs.conf here. The configuration is as follows

# Timeout
connect_timeout = 10
network_timeout = 30
# Coded character set
charset = UTF-8
# IP and port of tracker server
tracker_server = Yours tracker address:22122

After the configuration number, put the changed file under your resources folder

Then paste my toolkit directly. It can be used directly by calling methods

/**
 * This class is used for the operation tool class of FastDFS service
 * @author musiro
 * @version 0.1
 * @date 2022/7/24
 */
@Slf4j
public final class FastDFSUtil {

	private static final StorageClient CLIENT;

	/**
     * Prohibit instantiation
     */
	private FastDFSUtil() {}

	static {
		try {
			ClientGlobal.init("fdfs.conf");
			TrackerClient trackerClient = new TrackerClient(ClientGlobal.g_tracker_group);
			TrackerServer trackerServer = trackerClient.getConnection();
			StorageServer storeStorage = trackerClient.getStoreStorage(trackerServer);
			CLIENT = new StorageClient(trackerServer, storeStorage);
		} catch (IOException | MyException  e) {
			throw new RuntimeException(e);
		}
	}

	/**
     * Get client information
     * @return Client information
     */
	private static StorageClient getClientInfo(){
		return CLIENT;
	}

	/**
     * Upload file [cannot upload in segments]
     * @param fileName File name -- no suffix required
     * @param ext file extension
     * @param bytes Binary file
     * @return parameter
     * <ul>
     *     <li>Result [0]: group name example: group1</li>
     *     <li>Result [1]: save the location of the server, for example: m01/00/00/rbntrggkx7aeg67qaaaaaiptlyq431.jpg</li>
     * </ul>
     * @throws InterruptedException
     * @throws MyException
     * @throws IOException
     */
	public static String[] upload(String fileName,String ext,byte[] bytes) throws MyException, IOException {
		//Get connection
		StorageClient connection = getClientInfo();
		//Introduction to setting file
		NameValuePair[] pair = new NameValuePair[3];
		pair[0] = new NameValuePair("fileName",fileName);
		pair[1] = new NameValuePair("fileExt",ext);
		pair[2] = new NameValuePair("fileSize",String.valueOf(bytes.length));
		//Upload file
		String[] strings = connection.upload_file(bytes, ext, pair);
		//Return the specific required parameters
		return strings;
	}

	/**
     * This method is only applicable to local tests. Springboot method can use another method
     * @param path Picture path
     * @param ext file
     * @param fileName Document name
     * @return
     * <ul>
     *     <li>Result [0]: group name example: group1</li>
     *     <li>Result [1]: save the location of the server, for example: m01/00/00/rbntrggkx7aeg67qaaaaaiptlyq431.jpg</li>
     * </ul>
     * @throws MyException
     * @throws IOException
     */
	public static String[] upload(String path,String ext,String fileName) throws MyException, IOException {
		//Get connection
		StorageClient connection = getClientInfo();
		//Introduction to setting file
		NameValuePair[] pair = new NameValuePair[3];
		pair[0] = new NameValuePair("fileName",fileName);
		pair[1] = new NameValuePair("fileExt",ext);
		pair[2] = new NameValuePair("fileSize",Long.toBinaryString(new File(path).length()));
		//Upload file
		String[] strings = connection.upload_file(path, ext, pair);
		//Return the specific required parameters
		return strings;
	}

	/**
     * File download
     * @param groupName Group name, for example: group1
     * @param path Full path for example: M01/00/00/rBNTRGGKX7aEG67qAAAAAIpTlYQ431.jpg
     * @return File stream
     * @throws InterruptedException
     * @throws MyException
     * @throws IOException
     */
	public static byte[] download(String groupName,String path) throws InterruptedException, MyException, IOException {
		//Get connection
		StorageClient connection = getClientInfo();
		//Download files
		byte[] res = connection.download_file(groupName,path);
		//Return file stream
		return res;
	}

	/**
     * Delete file
     * @param groupName Group name, for example: group1
     * @param path Full path for example: M01/00/00/rBNTRGGKX7aEG67qAAAAAIpTlYQ431.jpg
     * @throws InterruptedException
     * @throws MyException
     * @throws IOException
     */
	public static void delete(String groupName,String path) throws  MyException, IOException {
		//Get connection
		StorageClient connection = getClientInfo();
		//Download files
		connection.delete_file(groupName,path);
	}
}

reference

  1. Install certificates on Nginx or Tengine servers
  2. Fastdfs uninstall
  3. Take you to install FastDFS and integrate with SpringBoot [recommended]
  4. Centos7.x builds FastDFS and configures http or https access through Nginx

Posted by MmmVomit on Sun, 24 Jul 2022 23:29:23 +0530