linux 安裝nginx ftp


1.安裝nginx

 

nginx-1.13.9

下載地址:http://nginx.org/download/nginx-1.13.9.tar.gz

[root@localhost ~]# wget http://nginx.org/download/nginx-1.13.9.tar.gz

解壓

[root@localhost ~]# tar -zxvf nginx-1.13.9.tar.gz 

進入目錄

[root@localhost ~]# cd  nginx-1.13.9

編譯

[root@localhost nginx-1.13.9]# ./configure

注意:這時候報錯了

checking for OS
 + Linux 3.10.0-693.el7.x86_64 x86_64
checking for C compiler ... not found

./configure: error: C compiler cc is not found

這時候我們就要百度一下了,百度得知我們少一個gcc編譯器 可以這么去安裝 yum -y install gcc-c++

[root@localhost nginx-1.13.9]# yum -y install gcc-c++

一大堆的提示,也看不懂,最后就安裝好了

再次編譯

./configure

提示錯誤,我們需要一些安裝庫

[root@localhost nginx-1.13.9]# yum install -y pcre pcre-devel
[root@localhost nginx-1.13.9]# yum install -y zlib zlib-devel
[root@localhost nginx-1.13.9]# yum install -y openssl openssl-devel

 

再次編譯

./configure

 

 

安裝

[root@localhost nginx-1.13.9]# make && make install

 

 

設置開機自啟動

[root@localhost nginx-1.13.9]# vi /etc/rc.local

添加一句

/usr/local/nginx/sbin/nginx

[root@localhost nginx-1.13.9]# chmod 755  /etc/rc.local 

進入啟目錄

[root@localhost nginx-1.13.9]# cd  /usr/local/nginx/sbin/
[root@localhost sbin]# ./nginx 

啟動

運行192.168.88.12,看看效果

 

看到這個圖表示成功了.

 修改配置支持ftp

user  root;

server {
listen 80;
server_name 192.168.88.12;

#charset koi8-r;

#access_log logs/host.access.log main;

location ~* \.(mp4)$ {
expires 30d;
valid_referers 127.0.0.1;
if ($invalid_referer) {
return 404;
}
root /ftpvideo/video;
}

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

 

 

安裝  vsftpd

#查詢是否安裝
[root@localhost conf]# vsftpd -version

直接安裝

[root@localhost conf]# yum install -y vsftpd

安裝完成后創建一個目錄,作為上傳目錄,最好和nginx 的目錄一致這里我們配置為nginx里面配置的那個/ftpvideo/video

 

[root@localhost /]# mkdir  -p  /ftpvideo/video

添加一個系統賬戶

[root@localhost /]# useradd -d /ftpvideo/video -s /bin/bash ftpadmin

-d指定 用戶的家目錄,以后用做ftp上傳的目錄.-s指定是否可以登錄  ,最后是用戶名稱.

設置密碼

可以使用passwd命令設置,但是密碼強度要求較高,我是自己的虛擬機,所以簡單點.

[root@localhost /]# echo 123456|passwd --stdin ftpadmin

 

可能要修改防火牆,這里我是關閉了防火牆,所以不用設置,具體的我也不知道怎么設置,百度吧.

為了安全,我們要設置用戶只能訪問自己的家目錄,也就是我們配置的那個/ftpvideo/video

修改/etc/vsftpd.conf

[root@localhost vsftpd]# cd  /etc/vsftpd/
[root@localhost vsftpd]# vi vsftpd.conf

把下面這幾個改為NO,關閉匿名訪問

anonymous_enable=NO

local_enable=NO

write_enable=NO

取消chroot_local_user=YES的注釋

ascii_upload_enable=YES
ascii_download_enable=YES

 

在最后添加

allow_writeable_chroot=YES

 

設置開機啟動

[root@localhost vsftpd]# systemctl enable vsftpd.service

啟動ftp

[root@localhost vsftpd]# systemctl start  vsftpd.service

設置不能登錄linux

[root@localhost vsftpd]# usermod -s /usr/sbin/nologin ftpadmin

鏈接測試 

可以登錄,但是無法上傳文件,最后分析是權限問題

chmod -R 777 /ftpvideo

參考:

原文出處:csdn -> http://blog.csdn.net/wangkai_123456/article/details/78612656

https://www.centos.bz/2017/12/centos7%E9%83%A8%E7%BD%B2vsftpd%E6%9C%8D%E5%8A%A1/

 最后提供一個Java操作ftp的類

import org.apache.commons.net.ftp.FTPClient;

import java.io.*;
import java.util.logging.Logger;



/**
 * 含義:ftp的上傳下載處理對象
 */
public class FtpFileTransfer {
    private FTPClient ftpClient = null;//ftp客戶端對象
    private static String hostname = null;//FTP主機名稱
    private static Integer port = 0;    //FTP服務端口
    private static String userName = null;//FTP服務器登錄用戶名
    private static String passwd=null;//FTP服務器登錄密碼
    private final String SPAPRATE_TOEKN = "/";
    private Logger logger = Logger.getLogger(this.getClass().getName());

    /**
     * 從配置文件中獲取配置值
     */
    public FtpFileTransfer(String hostname,int port,String userName,String passwd){
        this.hostname = hostname;
        this.port = port;
        this.userName = userName;
        this.passwd = passwd;
    }
    /**
     * 方法描述:上傳文件
     * @param srcPath 源文件路徑
     * @param ftpPath FTP端存放路徑
     * @param targetName FTP端存放名稱
     * @return 操作是否成功
     */
    public boolean uploadFile(String srcPath,String ftpPath, String targetName){
        boolean ret = false;
        File file = new File(srcPath);
        ret = uploadFile(file, ftpPath, targetName);
        return ret;
    }

    /**
     * 方法描述:上傳文件
     * @param file 待上傳的文件
     * @param ftpPath 目標文件路徑
     * @param targetName 目標名稱
     * @return
     */
    public boolean uploadFile(File file,String ftpPath,String targetName){
        if(file == null){
            logger.info("File is null");
            return false;
        }
        try {
            InputStream is = new FileInputStream(file);
            return uploadFileStream(is, ftpPath, targetName);
        } catch (FileNotFoundException e) {
            return false;
        }
    }
    /**
     * 方法描述:上傳文件
     * @param is 源文件流
     * @param ftpPath 放置在服務器上的位置
     * @param targetName 放置在服務器上的名稱
     * @return 操作是否成功
     */
    public boolean uploadFileStream(InputStream is,String ftpPath, String targetName)  {
        boolean ret = false;
        if(is == null){
            logger.info("File is null");
            return ret;
        }
        ret = this.connect2Ftp();
        if(!ret){
            logger.info("connect to ftp server failure");
            this.disconnect2Ftp();
            return ret;
        }
        try {
            boolean mkdir = this.makeDir(ftpPath);
            if(ftpPath.startsWith(SPAPRATE_TOEKN)){
                ftpPath = ftpPath.substring(ftpPath.indexOf(SPAPRATE_TOEKN)+SPAPRATE_TOEKN.length());
            }
            ret = ftpClient.changeWorkingDirectory(ftpPath);
            if(ret){
                ftpClient.setBufferSize(1024);
                ftpClient.setControlEncoding("utf-8");
                ftpClient.enterLocalPassiveMode();
                ret = ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
                if(ret){
                    ret = ftpClient.storeFile(targetName, is);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("FTP客戶端出錯!", e);
        } finally {
            if(is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            disconnect2Ftp();
        }
        return ret;
    }


    /**
     * 方法描述:下載文件
     * @param remoteFileName FTP上的文件名稱,含路徑
     * @param localFilePath 取到文件到本地后文件的存放位置
     * @return 操作是否成功
     */
    public boolean downloadFile(String remoteFileName, String localFilePath){
        boolean ret = false;
        remoteFileName = remoteFileName.trim();
        if(remoteFileName.startsWith(SPAPRATE_TOEKN)){
            remoteFileName = remoteFileName.substring(remoteFileName.indexOf(SPAPRATE_TOEKN)
                    +SPAPRATE_TOEKN.length());
        }else if(remoteFileName.startsWith("\\")){
            remoteFileName = remoteFileName.substring(remoteFileName.indexOf("\\")
                    +"\\".length());
        }

        String path = null;
        if(!remoteFileName.contains("//")){
            if(remoteFileName.contains("/")){
                remoteFileName=remoteFileName.replace("/", "\\");
            }
        }
        path = remoteFileName.substring(0,remoteFileName.lastIndexOf("\\"));
        String fileName = remoteFileName.substring(path.length()+2);
        FileOutputStream fos = null;
        try {
            ret = connect2Ftp();
            if(!ret){
                disconnect2Ftp();
                return ret;
            }
            ret = ftpClient.changeWorkingDirectory(path);
            fos = new FileOutputStream(localFilePath);
            ftpClient.setBufferSize(1024);
            ftpClient.setControlEncoding("utf-8");
            //設置文件類型(二進制)
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
            ret = ftpClient.retrieveFile(fileName, fos);
        } catch (IOException e) {
            throw new RuntimeException("FTP客戶端出錯!", e);
        } finally {
            if(fos!=null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            disconnect2Ftp();
        }
        return ret;
    }

    /**
     * 方法描述:刪除FTP服務器上的文件
     * @param filePath 文件名稱
     * @return 刪除成功/失敗
     */
    public boolean deleteFile(String filePath){
        boolean ret = false;
        if(filePath == null || filePath.isEmpty() || filePath.equals("")){
            return false;
        }
        try {
            connect2Ftp();
            ftpClient.changeWorkingDirectory("\\");
            ret = ftpClient.deleteFile(filePath);
        } catch (IOException e) {
        }finally{
            disconnect2Ftp();
        }
        return ret;
    }

    /**
     * 方法描述:刪除文件夾
     * @param dirPath 文件夾路徑
     * @param force 是否強制刪除
     * @return 是否刪除成功
     */
    public boolean deleteDirs(String dirPath, boolean force){
        boolean ret = false;
        if(dirPath.startsWith(SPAPRATE_TOEKN)){
            dirPath = dirPath.substring(dirPath.indexOf(SPAPRATE_TOEKN)+SPAPRATE_TOEKN.length());
        }
        String path = dirPath;
        try {
            ret = ftpClient.changeWorkingDirectory(dirPath);
            String[] names = ftpClient.listNames();
            if(force){
                for(String name:names){
                    if(dirPath.endsWith(SPAPRATE_TOEKN)){
                        ret = deleteFile(dirPath+name);
                    }else{
                        ret = deleteFile(dirPath+SPAPRATE_TOEKN+name);
                    }
                }
            }
            ret = ftpClient.changeWorkingDirectory("//");
            while(true){
                ret = ftpClient.removeDirectory(path);
                if(path.contains(SPAPRATE_TOEKN)){
                    path = path.substring(0, path.lastIndexOf(SPAPRATE_TOEKN));
                }else{
                    break;
                }
            }
        } catch (Exception e) {
        }
        return ret;
    }
    /**
     * 方法描述:連接到遠端的FTP服務器
     * @return 是否連接成功
     */
    private boolean connect2Ftp(){
        boolean ret = false;
        ftpClient = new FTPClient();
        try {
            ftpClient.connect(hostname, port);
            ret = ftpClient.login(userName, passwd);
            logger.info("Finished login the ftp server, result="+ret);
        } catch (Exception e) {
        }
        return ret;
    }

    /**
     * 方法描述:斷開ftp鏈接
     */
    private void disconnect2Ftp(){
        if(ftpClient != null){
            try {
                ftpClient.disconnect();
            } catch (IOException e) {
            }
        }
    }

    /**
     * 方法描述:根據目錄要求創建ftp端的文件夾路徑
     * @param dir 文件夾路徑,如:絕對路徑,/ab/cde/ef/hg,相對路徑, ab/cd
     * @return 是否創建成功
     * @throws IOException
     */
    private boolean makeDir(String dir) throws IOException{
        boolean ret = false;
        int i = 0;
        if(dir != null && !dir.isEmpty()){
            String[] dirs = null;
            int len = 0;
            if(dir.contains("//")){
                dir = dir.replace("//", SPAPRATE_TOEKN);
                i = 1;
            }
            if(dir.contains(SPAPRATE_TOEKN)){
                dirs = dir.split(SPAPRATE_TOEKN);
                len = dirs.length;
                StringBuffer sb = new StringBuffer();
                for(;i<len;i++){
                    sb.append(dirs[i]);
                    sb.append(SPAPRATE_TOEKN);
                    ret = ftpClient.makeDirectory(sb.toString());
                }
            }else{
                ret = ftpClient.makeDirectory(dir);
            }
        }
        return ret;
    }

    public boolean isFileExist(String filePath) {
        connect2Ftp();
        boolean ret = false;
        try {
            String rt = ftpClient.getModificationTime(filePath);
            if (rt != null && !rt.isEmpty()) {
                ret = true;
            }
        } catch (IOException e) {
        } finally {
            disconnect2Ftp();
        }
        return ret;
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM