linux下使用nginx和ftp做图片服务器

一、搭建nginx

1.1 安装gcc、pcre、zlib、openssl环境

1.1.1  gcc

[plain] view plain copy
  1. yum install gcc-c++   

1.1.2  pcre

[plain] view plain copy
  1. yum install -y pcre pcre-devel  

1.1.3  zlib

[plain] view plain copy
  1. yum install -y zlib zlib-devel  

1.1.4 openssl

[plain] view plain copy
  1. yum install -y openssl openssl-devel  

1.2 安装nginx

1.2.1 上传nginx-1.8.0.tar.gz到指定目录,并解压

个人习惯: 上传至/usr/local/env/nginx路径下
[plain] view plain copy
  1. [root@localhost ~]# cd /usr/local/env/nginx/  
  2. [root@localhost nginx]# tar -zxvf nginx-1.8.0.tar.gz -C /usr/local/env/nginx/  

1.2.2 configure

[plain] view plain copy
  1. [root@localhost nginx]# cd nginx-1.8.0  
  2. [root@localhost nginx-1.8.0]# ./configure  

1.2.3 编译、安装

[plain] view plain copy
  1. [root@localhost nginx-1.8.0]# make  

此时会生成makefile文件,再install

[plain] view plain copy
  1. [root@localhost nginx-1.8.0]# make install  

此时,会在/usr/local/路径下生成nginx相关文件夹(注:此路径可以修改,可通过configure配置文件来修改生成位置)


1.2.4 启动并访问nginx主页

[plain] view plain copy
  1. [root@localhost nginx-1.8.0]# cd /usr/local/nginx/sbin/  
  2. [root@localhost sbin]# ./nginx -c /usr/local/nginx/conf/nginx.conf  

注: 可以通过-c来指定nginx启动的配置文件nginx.conf

默认的nginx.conf会通过配置,将访问路径/映射到自己的html文件夹下的index.html主页上


访问主页,如果看到欢迎页,说明安装nginx成功.


1.2.5 停止nginx

方式1,快速停止:此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。

[html] view plain copy
  1. [root@localhost sbin]# pwd  
  2. /usr/local/nginx/sbin  
  3. [root@localhost sbin]# ./nginx -s stop  
  4. [root@localhost sbin]#  

方式2,完整停止(建议使用)此方式停止步骤是待nginx进程处理任务完毕进行停止。

[html] view plain copy
  1. [root@localhost sbin]# pwd  
  2. /usr/local/nginx/sbin  
  3. [root@localhost sbin]# ./nginx -s quit  
  4. [root@localhost sbin]#  

1.2.6 重启nginx

[plain] view plain copy
  1. [root@localhost sbin]# ./nginx -s reload  

注意:此步骤有时会报pid找不到的错误:


可通过如下方式解决:

[plain] view plain copy
  1. [root@localhost sbin]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf  

此时,即可启动成功。


二、 搭建vsftpd

2.1 使用yum安装vsftpd

[plain] view plain copy
  1. [root@localhost /]# yum -y install vsftpd  

此时,会默认安装在/etc/vsftpd路径下


2.2 添加ftp用户ftpuser,并设置密码

[plain] view plain copy
  1. [root@localhost /]# useradd ftpuser  
  2. [root@localhost /]# passwd ftpuser  
  3. Changing password for user ftpuser.  
  4. New password:  
  5. BAD PASSWORD: it is too simplistic/systematic  
  6. BAD PASSWORD: is too simple  
  7. Retype new password:  
  8. passwd: all authentication tokens updated successfully.  
  9. [root@localhost /]#  

此时在/home/路径下会生成一个ftpuser文件夹


2.3 开启21号端口,防火墙默认是不开启的

修改/etc/sysconfig/iptables文件,将22那条记录复制,改为21即可

重启防火墙

[plain] view plain copy
  1. [root@localhost /]# service iptables restart  

2.4 设置vsftpd开机启动

[plain] view plain copy
  1. [root@localhost /]# chkconfig vsftpd on  

2.5 修改vsftp配置文件

默认vsftp的配置文件路径为: /etc/vsftpd/vsftpd.conf


2.5.1 关闭匿名访问

将anonymous_enable的YES改为NO


2.5.2 设置vsftp的根路径

在/home/ftpuser/路径下创建images文件夹作为vsftp文件根目录,并设置最大权限

[plain] view plain copy
  1. [root@localhost /]# mkdir /home/ftpuser/images  
  2. [root@localhost /]# ll /home/ftpuser/  
  3. total 4  
  4. drwxr-xr-x. 2 root root 4096 Mar 31 00:18 images  
  5. [root@localhost /]# chmod -R 777 /home/ftpuser/images  
  6. [root@localhost /]# ll /home/ftpuser/  
  7. total 4  
  8. drwxrwxrwx. 2 root root 4096 Mar 31 00:18 images  
  9. [root@localhost /]#  

在配置文件中添加根路径

注:local为远程用户访问的根路径,anon为匿名用户访问的根路径    注:等号俩边不要有空格

[plain] view plain copy
  1. local_root=/home/ftpuser/images/  
  2. anon_root=/home/ftpuser/images/  

2.5.3 设置是否限制远程用户访问路径

限制远程用户只能访问ftp根路径及其子路径,不能向上访问。默认为可以访问,为NO,如果需要限制,改为YES即可。

[plain] view plain copy
  1. chroot_local_user=NO  

注: 此处如果设置为YES,则使用FtpClient链接时会有问题,改为NO,就可以了,原因待解决。

2.5.4 重启vsftp

[plain] view plain copy
  1. [root@localhost /]# service vsftpd restart  

2.6 修改selinux

使用远程终端连接的时候,无法获取目录列表,需要使用如下俩个命令修改俩个状态:

[plain] view plain copy
  1. [root@localhost ~]# setsebool -P allow_ftpd_full_access on  
  2. [root@localhost ~]# setsebool -P ftp_home_dir on  

注: 这俩个命令都会执行俩分钟左右,请耐心等待



2.7 访问ftp服务器

先关闭服务器防火墙,否则用终端工具链接不上ftp服务器

[plain] view plain copy
  1. [root@localhost /]# service iptables stop  

先通过远程终端工具(推荐使用MobaXterm)链接ftp服务器:


登陆之后上传一张图片到主目录下,然后使用ftp协议访问:


ftp搭建成功。


三、 配置nginx.conf的ftp服务器ip和用户

3.1 修改用户为root


3.2 添加server指向自己的服务器IP和ftp根路径

[plain] view plain copy
  1. server{  
  2.         listen   80;  
  3.         server_name  ftp所在服务器的ip;  
  4.         location / {  
  5.             root /home/ftpuser;  
  6.         }  
  7.     }  

注: 此时在访问服务器IP/路径的时候会自动将/映射为/home/ftpuser/路径

3.3 重启nginx

[plain] view plain copy
  1. [root@localhost sbin]# cd /usr/local/nginx/sbin/  
  2. [root@localhost sbin]# ./nginx -s reload  

3.4 使用http协议访问ftp服务器图片


四.  FtpUtil

4.1 pom.xml


4.2 测试是否可以成功上传

[java] view plain copy
  1. public class FTPTest {  
  2.       
  3.     @Test  
  4.     public void testFtpClient() throws Exception {  
  5.         // 创建一个FtpClent对象  
  6.         FTPClient ftpClient = new FTPClient();  
  7.         // 创建ftp连接,安装ftp服务的虚拟机ip,默认端口为21  
  8.         ftpClient.connect("xxx.xxx.xxx.xxx",21);  
  9.         // 使用用户名和密码登录ftp服务器  
  10.         ftpClient.login("xxxxxx""xxxxxx");  
  11.           
  12.         // 读取本地要上传文件,以流的形式创建  
  13.         FileInputStream inputStream = new FileInputStream(new File("F:\\temp\\1.png"));  
  14.         // 设置上传的路径  
  15.         ftpClient.changeWorkingDirectory("/home/ftpuser/images");  
  16.         // 修改上传文件的格式  
  17.         ftpClient.setFileType(FTP.BINARY_FILE_TYPE);  
  18.         // 上传文件  
  19.         // 第一个参数为: 服务器端的文件名  
  20.         // 第二个参数为: 上传文件用的输入流  
  21.         ftpClient.storeFile("hello.png", inputStream);  
  22.         // 关闭连接  
  23.         ftpClient.logout();  
  24.     }  
  25.       
  26. }  

注: 有时候会提示soket失败,可通过如下方式解决:

    eclipse->windows->preferences->java->install JREs --> edit --> Default VM arguments

    添加  -Djava.net.preferIPv4Stack=true 即可


4.3 FtpUtil

[java] view plain copy
  1. package com.taota.common.utils;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileNotFoundException;  
  6. import java.io.FileOutputStream;  
  7. import java.io.IOException;  
  8. import java.io.InputStream;  
  9. import java.io.OutputStream;  
  10.   
  11. import org.apache.commons.net.ftp.FTP;  
  12. import org.apache.commons.net.ftp.FTPClient;  
  13. import org.apache.commons.net.ftp.FTPFile;  
  14. import org.apache.commons.net.ftp.FTPReply;  
  15.   
  16. /** 
  17.  * ftp上传下载工具类 
  18.  * <p>Title: FtpUtil</p> 
  19.  * <p>Description: </p> 
  20.  * @version 1.0 
  21.  */  
  22. public class FtpUtil {  
  23.   
  24.     /**  
  25.      * Description: 向FTP服务器上传文件  
  26.      * @param host FTP服务器hostname  
  27.      * @param port FTP服务器端口  
  28.      * @param username FTP登录账号  
  29.      * @param password FTP登录密码  
  30.      * @param basePath FTP服务器基础目录 
  31.      * @param filePath FTP服务器文件存放路径。例如分日期存放:/2015/01/01。文件的路径为basePath+filePath 
  32.      * @param filename 上传到FTP服务器上的文件名  
  33.      * @param input 输入流  
  34.      * @return 成功返回true,否则返回false  
  35.      */    
  36.     public static boolean uploadFile(String host, int port, String username, String password, String basePath,  
  37.             String filePath, String filename, InputStream input) {  
  38.         boolean result = false;  
  39.         FTPClient ftp = new FTPClient();  
  40.         try {  
  41.             int reply;  
  42.             ftp.connect(host, port);// 连接FTP服务器  
  43.             // 如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器  
  44.             ftp.login(username, password);// 登录  
  45.             reply = ftp.getReplyCode();  
  46.             if (!FTPReply.isPositiveCompletion(reply)) {  
  47.                 ftp.disconnect();  
  48.                 return result;  
  49.             }  
  50.             //切换到上传目录  
  51.             if (!ftp.changeWorkingDirectory(basePath+filePath)) {  
  52.                 //如果目录不存在创建目录  
  53.                 String[] dirs = filePath.split("/");  
  54.                 String tempPath = basePath;  
  55.                 for (String dir : dirs) {  
  56.                     if (null == dir || "".equals(dir)) continue;  
  57.                     tempPath += "/" + dir;  
  58.                     if (!ftp.changeWorkingDirectory(tempPath)) {  
  59.                         if (!ftp.makeDirectory(tempPath)) {  
  60.                             return result;  
  61.                         } else {  
  62.                             ftp.changeWorkingDirectory(tempPath);  
  63.                         }  
  64.                     }  
  65.                 }  
  66.             }  
  67.             //设置上传文件的类型为二进制类型  
  68.             ftp.setFileType(FTP.BINARY_FILE_TYPE);  
  69.             //上传文件  
  70.             if (!ftp.storeFile(filename, input)) {  
  71.                 return result;  
  72.             }  
  73.             input.close();  
  74.             ftp.logout();  
  75.             result = true;  
  76.         } catch (IOException e) {  
  77.             e.printStackTrace();  
  78.         } finally {  
  79.             if (ftp.isConnected()) {  
  80.                 try {  
  81.                     ftp.disconnect();  
  82.                 } catch (IOException ioe) {  
  83.                 }  
  84.             }  
  85.         }  
  86.         return result;  
  87.     }  
  88.       
  89.     /**  
  90.      * Description: 从FTP服务器下载文件  
  91.      * @param host FTP服务器hostname  
  92.      * @param port FTP服务器端口  
  93.      * @param username FTP登录账号  
  94.      * @param password FTP登录密码  
  95.      * @param remotePath FTP服务器上的相对路径  
  96.      * @param fileName 要下载的文件名  
  97.      * @param localPath 下载后保存到本地的路径  
  98.      * @return  
  99.      */    
  100.     public static boolean downloadFile(String host, int port, String username, String password, String remotePath,  
  101.             String fileName, String localPath) {  
  102.         boolean result = false;  
  103.         FTPClient ftp = new FTPClient();  
  104.         try {  
  105.             int reply;  
  106.             ftp.connect(host, port);  
  107.             // 如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器  
  108.             ftp.login(username, password);// 登录  
  109.             reply = ftp.getReplyCode();  
  110.             if (!FTPReply.isPositiveCompletion(reply)) {  
  111.                 ftp.disconnect();  
  112.                 return result;  
  113.             }  
  114.             ftp.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录  
  115.             FTPFile[] fs = ftp.listFiles();  
  116.             for (FTPFile ff : fs) {  
  117.                 if (ff.getName().equals(fileName)) {  
  118.                     File localFile = new File(localPath + "/" + ff.getName());  
  119.   
  120.                     OutputStream is = new FileOutputStream(localFile);  
  121.                     ftp.retrieveFile(ff.getName(), is);  
  122.                     is.close();  
  123.                 }  
  124.             }  
  125.   
  126.             ftp.logout();  
  127.             result = true;  
  128.         } catch (IOException e) {  
  129.             e.printStackTrace();  
  130.         } finally {  
  131.             if (ftp.isConnected()) {  
  132.                 try {  
  133.                     ftp.disconnect();  
  134.                 } catch (IOException ioe) {  
  135.                 }  
  136.             }  
  137.         }  
  138.         return result;  
  139.     }  
  140.       
  141.       
  142.     // 工具类的使用测试  
  143.     public static void main(String[] args) {  
  144.         try {    
  145.             FileInputStream in=new FileInputStream(new File("F:\\temp\\1.png"));    
  146.             boolean flag = uploadFile("xxx.xxx.xxx.xxx"21"xxx""xxx""/home/ftpuser/images","/2016/09/21""hello.png", in);    
  147.             System.out.println(flag);    
  148.         } catch (FileNotFoundException e) {    
  149.             e.printStackTrace();    
  150.         }    
  151.     }  
  152.       
  153. }  


发布了28 篇原创文章 · 获赞 50 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/qq_21873747/article/details/79912341
今日推荐