linux下使用nginx和ftp做图片服务器
一、搭建nginx
1.1 安装gcc、pcre、zlib、openssl环境
1.1.1 gcc
- yum install gcc-c++
1.1.2 pcre
- yum install -y pcre pcre-devel
1.1.3 zlib
- yum install -y zlib zlib-devel
1.1.4 openssl
- yum install -y openssl openssl-devel
1.2 安装nginx
1.2.1 上传nginx-1.8.0.tar.gz到指定目录,并解压
- [[email protected] ~]# cd /usr/local/env/nginx/
- [[email protected] nginx]# tar -zxvf nginx-1.8.0.tar.gz -C /usr/local/env/nginx/
1.2.2 configure
- [[email protected] nginx]# cd nginx-1.8.0
- [[email protected] nginx-1.8.0]# ./configure
1.2.3 编译、安装
- [[email protected] nginx-1.8.0]# make
此时会生成makefile文件,再install
- [[email protected] nginx-1.8.0]# make install
此时,会在/usr/local/路径下生成nginx相关文件夹(注:此路径可以修改,可通过configure配置文件来修改生成位置)
1.2.4 启动并访问nginx主页
- [[email protected] nginx-1.8.0]# cd /usr/local/nginx/sbin/
- [[email protected] 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命令强制杀掉进程。
- [[email protected] sbin]# pwd
- /usr/local/nginx/sbin
- [[email protected] sbin]# ./nginx -s stop
- [[email protected] sbin]#
方式2,完整停止(建议使用):此方式停止步骤是待nginx进程处理任务完毕进行停止。
- [[email protected] sbin]# pwd
- /usr/local/nginx/sbin
- [[email protected] sbin]# ./nginx -s quit
- [[email protected] sbin]#
1.2.6 重启nginx
- [[email protected] sbin]# ./nginx -s reload
注意:此步骤有时会报pid找不到的错误:
可通过如下方式解决:
- [[email protected] sbin]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
此时,即可启动成功。
二、 搭建vsftpd
2.1 使用yum安装vsftpd
- [[email protected] /]# yum -y install vsftpd
此时,会默认安装在/etc/vsftpd路径下
2.2 添加ftp用户ftpuser,并设置密码
- [[email protected] /]# useradd ftpuser
- [[email protected] /]# passwd ftpuser
- Changing password for user ftpuser.
- New password:
- BAD PASSWORD: it is too simplistic/systematic
- BAD PASSWORD: is too simple
- Retype new password:
- passwd: all authentication tokens updated successfully.
- [[email protected] /]#
此时在/home/路径下会生成一个ftpuser文件夹
2.3 开启21号端口,防火墙默认是不开启的
修改/etc/sysconfig/iptables文件,将22那条记录复制,改为21即可
重启防火墙
- [[email protected] /]# service iptables restart
2.4 设置vsftpd开机启动
- [[email protected] /]# 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文件根目录,并设置最大权限
- [[email protected] /]# mkdir /home/ftpuser/images
- [[email protected] /]# ll /home/ftpuser/
- total 4
- drwxr-xr-x. 2 root root 4096 Mar 31 00:18 images
- [[email protected] /]# chmod -R 777 /home/ftpuser/images
- [[email protected] /]# ll /home/ftpuser/
- total 4
- drwxrwxrwx. 2 root root 4096 Mar 31 00:18 images
- [[email protected] /]#
在配置文件中添加根路径
注:local为远程用户访问的根路径,anon为匿名用户访问的根路径 注:等号俩边不要有空格
- local_root=/home/ftpuser/images/
- anon_root=/home/ftpuser/images/
2.5.3 设置是否限制远程用户访问路径
限制远程用户只能访问ftp根路径及其子路径,不能向上访问。默认为可以访问,为NO,如果需要限制,改为YES即可。
- chroot_local_user=NO
注: 此处如果设置为YES,则使用FtpClient链接时会有问题,改为NO,就可以了,原因待解决。
2.5.4 重启vsftp
- [[email protected] /]# service vsftpd restart
2.6 修改selinux
使用远程终端连接的时候,无法获取目录列表,需要使用如下俩个命令修改俩个状态:
- [[email protected] ~]# setsebool -P allow_ftpd_full_access on
- [[email protected] ~]# setsebool -P ftp_home_dir on
注: 这俩个命令都会执行俩分钟左右,请耐心等待
2.7 访问ftp服务器
先关闭服务器防火墙,否则用终端工具链接不上ftp服务器
- [[email protected] /]# service iptables stop
先通过远程终端工具(推荐使用MobaXterm)链接ftp服务器:
登陆之后上传一张图片到主目录下,然后使用ftp协议访问:
ftp搭建成功。
三、 配置nginx.conf的ftp服务器ip和用户
3.1 修改用户为root
3.2 添加server指向自己的服务器IP和ftp根路径
- server{
- listen 80;
- server_name ftp所在服务器的ip;
- location / {
- root /home/ftpuser;
- }
- }
注: 此时在访问服务器IP/路径的时候会自动将/映射为/home/ftpuser/路径
3.3 重启nginx
- [[email protected] sbin]# cd /usr/local/nginx/sbin/
- [[email protected] sbin]# ./nginx -s reload
3.4 使用http协议访问ftp服务器图片
四. FtpUtil
4.1 pom.xml
4.2 测试是否可以成功上传
- public class FTPTest {
- @Test
- public void testFtpClient() throws Exception {
- // 创建一个FtpClent对象
- FTPClient ftpClient = new FTPClient();
- // 创建ftp连接,安装ftp服务的虚拟机ip,默认端口为21
- ftpClient.connect("xxx.xxx.xxx.xxx",21);
- // 使用用户名和密码登录ftp服务器
- ftpClient.login("xxxxxx", "xxxxxx");
- // 读取本地要上传文件,以流的形式创建
- FileInputStream inputStream = new FileInputStream(new File("F:\\temp\\1.png"));
- // 设置上传的路径
- ftpClient.changeWorkingDirectory("/home/ftpuser/images");
- // 修改上传文件的格式
- ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
- // 上传文件
- // 第一个参数为: 服务器端的文件名
- // 第二个参数为: 上传文件用的输入流
- ftpClient.storeFile("hello.png", inputStream);
- // 关闭连接
- ftpClient.logout();
- }
- }
注: 有时候会提示soket失败,可通过如下方式解决:
eclipse->windows->preferences->java->install JREs --> edit --> Default VM arguments
添加 -Djava.net.preferIPv4Stack=true 即可
4.3 FtpUtil
- package com.taota.common.utils;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import org.apache.commons.net.ftp.FTP;
- import org.apache.commons.net.ftp.FTPClient;
- import org.apache.commons.net.ftp.FTPFile;
- import org.apache.commons.net.ftp.FTPReply;
- /**
- * ftp上传下载工具类
- * <p>Title: FtpUtil</p>
- * <p>Description: </p>
- * @version 1.0
- */
- public class FtpUtil {
- /**
- * Description: 向FTP服务器上传文件
- * @param host FTP服务器hostname
- * @param port FTP服务器端口
- * @param username FTP登录账号
- * @param password FTP登录密码
- * @param basePath FTP服务器基础目录
- * @param filePath FTP服务器文件存放路径。例如分日期存放:/2015/01/01。文件的路径为basePath+filePath
- * @param filename 上传到FTP服务器上的文件名
- * @param input 输入流
- * @return 成功返回true,否则返回false
- */
- public static boolean uploadFile(String host, int port, String username, String password, String basePath,
- String filePath, String filename, InputStream input) {
- boolean result = false;
- FTPClient ftp = new FTPClient();
- try {
- int reply;
- ftp.connect(host, port);// 连接FTP服务器
- // 如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器
- ftp.login(username, password);// 登录
- reply = ftp.getReplyCode();
- if (!FTPReply.isPositiveCompletion(reply)) {
- ftp.disconnect();
- return result;
- }
- //切换到上传目录
- if (!ftp.changeWorkingDirectory(basePath+filePath)) {
- //如果目录不存在创建目录
- String[] dirs = filePath.split("/");
- String tempPath = basePath;
- for (String dir : dirs) {
- if (null == dir || "".equals(dir)) continue;
- tempPath += "/" + dir;
- if (!ftp.changeWorkingDirectory(tempPath)) {
- if (!ftp.makeDirectory(tempPath)) {
- return result;
- } else {
- ftp.changeWorkingDirectory(tempPath);
- }
- }
- }
- }
- //设置上传文件的类型为二进制类型
- ftp.setFileType(FTP.BINARY_FILE_TYPE);
- //上传文件
- if (!ftp.storeFile(filename, input)) {
- return result;
- }
- input.close();
- ftp.logout();
- result = true;
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if (ftp.isConnected()) {
- try {
- ftp.disconnect();
- } catch (IOException ioe) {
- }
- }
- }
- return result;
- }
- /**
- * Description: 从FTP服务器下载文件
- * @param host FTP服务器hostname
- * @param port FTP服务器端口
- * @param username FTP登录账号
- * @param password FTP登录密码
- * @param remotePath FTP服务器上的相对路径
- * @param fileName 要下载的文件名
- * @param localPath 下载后保存到本地的路径
- * @return
- */
- public static boolean downloadFile(String host, int port, String username, String password, String remotePath,
- String fileName, String localPath) {
- boolean result = false;
- FTPClient ftp = new FTPClient();
- try {
- int reply;
- ftp.connect(host, port);
- // 如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器
- ftp.login(username, password);// 登录
- reply = ftp.getReplyCode();
- if (!FTPReply.isPositiveCompletion(reply)) {
- ftp.disconnect();
- return result;
- }
- ftp.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录
- FTPFile[] fs = ftp.listFiles();
- for (FTPFile ff : fs) {
- if (ff.getName().equals(fileName)) {
- File localFile = new File(localPath + "/" + ff.getName());
- OutputStream is = new FileOutputStream(localFile);
- ftp.retrieveFile(ff.getName(), is);
- is.close();
- }
- }
- ftp.logout();
- result = true;
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if (ftp.isConnected()) {
- try {
- ftp.disconnect();
- } catch (IOException ioe) {
- }
- }
- }
- return result;
- }
- // 工具类的使用测试
- public static void main(String[] args) {
- try {
- FileInputStream in=new FileInputStream(new File("F:\\temp\\1.png"));
- boolean flag = uploadFile("xxx.xxx.xxx.xxx", 21, "xxx", "xxx", "/home/ftpuser/images","/2016/09/21", "hello.png", in);
- System.out.println(flag);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- }
- }