linux 安装nginx ftp

2018/03/02 10:06
阅读数 119

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

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部