Build a picture server based on docker container

   Recently, there is a need to store pictures in the server, so I am going to build my own picture server, based on the centos system, and install nginx for mapping. Before starting, it is recommended to update the yum source of centos

# update yum source
yum update
Create an image

  It’s time to build your own server. I don’t want to talk about docker’s instructions. Note that you need to put port 21 and your own external network access port. Port 21 is used to upload files, and the external network access port is used to pass the external network. access images

docker run -itd -p 8082:8082 -p 223:22 --privileged --restart=always --name imagesSource mycentos /usr/sbin/init
Install Nginx

   I am using Nginx, so I will not say much about the benefits of Nginx. First, I have to configure the environment where Ngnix is ​​installed.

# The environment required to install Nginx
yum install gcc-c++
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel

   Then go to the official website to download the source code of Nginx, compile and install ( portal)

# Compile and install Nginx
wget http://nginx.org/download/nginx-1.18.0.tar.gz
tar -zxvf nginx-1.18.0.tar.gz
cd nginx-1.18.0/

# The configuration of Nginx I use the default configuration, execute the configuration
./configure

# Use make to compile and install, if there is no make command, install make first
yum install -y make
make
make install
Start Nginx and set it to start automatically

  The installation of Nginx is completed, and then start Nginx

# Find the Nginx installation directory
whereis nginx
# Enter the installation directory
cd usr/local/nginx/
# start up
./nginx 
# stop
./nginx -s stop
# The stopping step of this method is to stop the nginx process after the processing task is completed.
./nginx -s quit
# This method is equivalent to first finding out the nginx process id and then using the kill command to forcibly kill the process
./nginx -s stop

# Set up to start automatically
vi /etc/rc.local
# Add the execution path of nginx
/usr/local/nginx/sbin/nginx
# Set execute permission
chmod 755 rc.local

  Nginx has been installed successfully. To test whether our configuration is successful, first restart the container. Through the "top" command, you can see that Nginx has started automatically.

   Then accessing nginx through the network is also successful, indicating that there is no problem with our default configuration, and Nginx is successfully installed

Configure Nginx

  After the Nginx installation is complete, start building the image server. The first is to access the images on the server. You need to add configuration to the configuration file. My image path is "/root/images"

# Add the following configuration to the configuration file
server {
   listen       8082; # listening port
   server_name  localhost;# Service Name
   location /images/ { # Location, accessed as ip + /images/ + filename
       root   /root;	# The location of the file /root/images
       autoindex_exact_size off; # Displays the exact size of the file, the default is on
       autoindex_localtime on; # The file time displayed is the server time of the file, and the GMT time is displayed when it is off.
       charset utf-8,gbk; # encoding settings
   }

  Restart after the configuration is complete, and then access the image through "ip + port + /images/ + file name", note that if there is a 403 error, you need to change the user in the configuration file to root, and then restart

  The picture access is here, then start the picture upload, pass the picture to the back end through the front end, and then upload the back end to the server through the ftp service, so the next step is to install the ftp service, and integrate ftp in the java code to execute the upload function

File Upload

  I use two ways to upload files, one is scp upload, the other is ftp upload
First upload through scp, you need to open port 22, open the sshd service, and check whether the sshd service is enabled through the "top" command

# start sshd service
systemctl start sshd
# Install the password plugin
yum install -y passwd
# Change the password, enter the same password twice in a row
passwd 

Then do java code integration

# import jar package
<dependency>
    <groupId>ch.ethz.ganymed</groupId>
    <artifactId>ganymed-ssh2</artifactId>
    <version>262</version>
</dependency>

For convenience, it is directly packaged into a tool. I have passed the external network test. It takes about 1 minute to upload 100M, and about 40 seconds to download 100M. The speed is quite fast.

import ch.ethz.ssh2.*;

/**
 * upload file to server
 * @param host       server host address
 * @param port       server port
 * @param filePath   local file path
 * @param username   username
 * @param password   password
 * @param remotePath server save address
 */
private void uploadFile(String host, Integer port, String filePath, String remotePath, String username, String password) {
    FileInputStream stream = null;
    Connection conn = null;
    SCPOutputStream put = null;
    try {//It is recommended to use try-catch-finally, even if it ends abnormally, you can close the stream in finally
        File file = new File(filePath);//get file
        stream = new FileInputStream(file);//get file stream
        conn = new Connection(host, port);//Create a connection
        conn.connect();//connect to the server
        boolean isAuth = conn.authenticateWithPassword(username, password);//Authenticate user
        if (!isAuth) {
            System.out.println("Connection failed");
            return;
        }
        //Determine whether the server file path exists, and create it if it does not exist
        SFTPv3Client ftp = new SFTPv3Client(conn);
        boolean isDir = ftp.lstat(remotePath).isDirectory();
        if (!isDir) {
            ftp.mkdir(remotePath, 0600);
        }
        SCPClient client = new SCPClient(conn);//scp object
        //File name, file size, server path to copy to server
        put = client.put(file.getName(), file.length(), remotePath, null);
        byte[] b = new byte[102400000];
        int i;
        while ((i = stream.read(b)) != -1) {//Uploaded in binary stream, if the file already exists, it will be overwritten by default
            put.write(b, 0, i);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (put != null)
                put.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            if (stream != null)
                stream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (conn != null)
            conn.close();
    }
}

/**
 * Download server files to local
 * @param name       file name
 * @param host       host
 * @param port       Duan Ou
 * @param filePath   local path
 * @param remotePath the path to the server
 * @param username   username
 * @param password   password
 */
private void downloadFile(String name, String host, Integer port, String filePath, String remotePath, String username, String password) {
    FileOutputStream stream = null;
    Connection conn = null;
    SCPInputStream put = null;
    try {//It is recommended to use try-catch-finally, even if it ends abnormally, you can close the stream in finally
        File file = new File(filePath);
        if (!file.exists()) {
            file.mkdirs();
        }
        file = new File(filePath + name);
        stream = new FileOutputStream(file);//get file stream
        conn = new Connection(host, port);//Create a connection
        conn.connect();//connect to the server
        boolean isAuth = conn.authenticateWithPassword(username, password);//Authenticate user
        if (!isAuth) {
            System.out.println("Connection failed");
            return;
        }
        SCPClient client = new SCPClient(conn);//scp object
        //File name, file size, server path to copy to server
        put = client.get(remotePath + name);
        byte[] b = new byte[409600000];
        int i;
        while ((i = put.read(b)) != -1) {//Uploaded in binary stream
            stream.write(b, 0, i);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (stream != null) {
            try {
                stream.flush();
                stream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            if (put != null)
                put.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (conn != null)
            conn.close();
    }
}

The second way, upload through ftp, first install and open the ftp server in the server

# install ftp service
yum install -y vsftpd
# run service
systemctl start vsftpd
# set startup
chkconfig vsftpd on
# Edit configuration
vim /etc/vsftpd/vsftpd.conf
# Modify the configuration
	anonymous_enable=NO # Settings do not allow anonymous access
	local_enable=YES # Set local users can access
	chroot_list_enable=YES # Make users unable to leave home directory
	ascii_upload_enable=YES
	ascii_download_enable=YES # Set upload and download functions that support ASCII mode
# Add the following configuration
guest_enable=YES #Set to enable virtual user function
guest_username=ftp # Specifies the host user of the virtual user
user_config_dir=/etc/vsftpd/vuser_conf # Set the storage path of the CentOSFTP service file of the virtual user personal vsftp
# Create account password text Single line is user name, double line is password
vim /etc/vsftpd/vuser_passwd.txt
# Generate the db file of the virtual user account password and associate the db file
db_load -T -t hash -f /etc/vsftpd/vuser_passwd.txt /etc/vsftpd/vuser_passwd.db
# Modify the authentication method, edit the authentication file /etc/pam.d/vsftpd, comment out all the original sentences, and add the following two sentences
auth required pam_userdb.so db=/etc/vsftpd/vuser_passwd
account required pam_userdb.so db=/etc/vsftpd/vuser_passwd
# Create a virtual user management path
mkdir /etc/vsftpd/vuser_conf
# Edit User Corresponding File
vim /etc/vsftpd/vuser_conf/test
# Add the following
local_root=/ftp
write_enable=YES
anon_umask=022
anon_world_readable_only=NO
anon_upload_enable=YES
anon_mkdir_write_enable=YES
anon_other_write_enable=YES
# start the service
systemctl start vsftpd.service
# Out of service
systemctl stop vsftpd.service
# restart the service
systemctl restart vsftpd.service
# View service status
systemctl status vsftpd.service

Then integrate the java code for testing

# import dependencies
<dependency>
    <groupId>commons-net</groupId>
    <artifactId>commons-net</artifactId>
    <version>3.7</version>
</dependency>

Since I forgot to map port 21, this part of the code is updated later

  The image resource server is built, and supports uploading and downloading operations. In addition to uploading and downloading through tools, you can also write a project yourself to upload and download. This part will not be demonstrated.

Tags: Java CentOS Docker

Posted by ShaileshD on Thu, 19 May 2022 22:43:21 +0300