Amazon AWS: 使用 S3 作为后端的 EC2 FTP 服务器





0/5 (0投票)
如何在 EC2 实例上设置 FTP 服务器,使用 S3 进行存储
引言
阅读本文后,您将能够在一台 EC2 实例上设置一个 FTP 服务器,该服务器直接将内容上传/下载到/从 S3。
背景
为了减少运营开销,我正在寻找一种利用 Amazon Web Services 创建 FTP 服务器的解决方案,该服务器将 S3 用作存储后端。目前的托管设施“捆绑”了服务,其中带宽利用率是成本最高的。由于将数据传输到 S3 不收取费用,因此选择此方案是合乎逻辑的。但是,在花了好几天时间寻找有关如何正确设置此配置的详细解决方案后,我决定自己动手并解决了这个问题。以下是我采取的步骤:
步骤 1:设置 EC2 实例
配置 EC2 安全组
在启动 EC2 实例之前,我建议创建一个新的安全组。使用以下设置:
TCP 端口范围 | IP 范围 | 注释 |
20-21 | 0.0.0.0/0 | FTP 端口 |
15393 - 15592 | 0.0.0.0/0 | 被动端口范围 |
22 | <您的访问 IP> | SSH 端口(稍后我们会将其删除) |
1234546 | <您的访问 IP> | 您将更改 SSH 连接到的端口 |
80 | 0.0.0.0/0 | 如果您正在运行 Web 服务器 |
注意:我喜欢设置一个非默认的 SSH 端口(在本例中为 123456),以便在系统设置完成后使用。由于服务器将对公众开放,更改 SSH 端口将有助于加固系统并防止某些入侵攻击。完成设置后,我们将从安全组中删除端口 22。
还可以添加任何其他监控 ICMP 或 UDP 端口。
启动 EC2 实例
使用上面新创建的安全组启动一个 Ubuntu 12.04 LTS 实例。
步骤 2:将 S3 挂载到实例
我们将需要一些第三方工具来正确地将 S3 存储桶挂载到服务器。我从多个站点以及我自己的反复试验中整理了这些信息。我必须感谢
http://code.google.com/p/s3fs/wiki/FuseOverAmazon
and
http://michaelaldridge.info/post/12086788604/mounting-s3-within-an-ec2-instance
没有这些网站,我就无法完成这项工作!
更新 apt 存储库
连接到您新启动的 EC2 实例,切换到 root 用户,然后运行以下命令
root@ip-xxx-xxx-xxx-xxx:~# echo 'deb http://us.archive.ubuntu.com/ubuntu/ lucid universe' >> /etc/apt/sources.list
root@ip-xxx-xxx-xxx-xxx:~# echo 'deb-src http://us.archive.ubuntu.com/ubuntu/ lucid universe' >> /etc/apt/sources.list
root@ip-xxx-xxx-xxx-xxx:~# echo 'deb http://us.archive.ubuntu.com/ubuntu/ lucid-updates universe' >> /etc/apt/sources.list
root@ip-xxx-xxx-xxx-xxx:~# echo 'deb-src http://us.archive.ubuntu.com/ubuntu/ lucid-updates universe' >> /etc/apt/sources.list
root@ip-xxx-xxx-xxx-xxx:~# echo 'deb http://us.archive.ubuntu.com/ubuntu/ lucid multiverse' >> /etc/apt/sources.list
root@ip-xxx-xxx-xxx-xxx:~# echo 'deb-src http://us.archive.ubuntu.com/ubuntu/ lucid multiverse' >> /etc/apt/sources.list
root@ip-xxx-xxx-xxx-xxx:~# echo 'deb http://us.archive.ubuntu.com/ubuntu/ lucid-updates multiverse' >> /etc/apt/sources.list
root@ip-xxx-xxx-xxx-xxx:~# echo 'deb-src http://us.archive.ubuntu.com/ubuntu/ lucid-updates multiverse' >> /etc/apt/sources.list
更新 apt
root@ip-xxx-xxx-xxx-xxx:~# sudo apt-get update
安装所需的依赖项
root@ip-xxx-xxx-xxx-xxx:~# sudo apt-get -y install build-essential
libfuse-dev fuse-utils libcurl4-openssl-dev libxml2-dev mime-support make
获取并安装 s3fs
root@ip-xxx-xxx-xxx-xxx:~# mkdir /software
root@ip-xxx-xxx-xxx-xxx:~# cd /software
root@ip-xxx-xxx-xxx-xxx:~# wget http://s3fs.googlecode.com/files/s3fs-1.61.tar.gz
root@ip-xxx-xxx-xxx-xxx:~# tar xvzf s3fs-1.61.tar.gz
root@ip-xxx-xxx-xxx-xxx:~# cd s3fs-1.61
root@ip-xxx-xxx-xxx-xxx:~# ./configure --prefix=/usr
root@ip-xxx-xxx-xxx-xxx:~# make
root@ip-xxx-xxx-xxx-xxx:~# make install
root@ip-xxx-xxx-xxx-xxx:~# touch /etc/passwd-s3fs && chmod 640 /etc/passwd-s3fs && echo 'AccessKey:SecretKey' > /etc/passwd-s3fs
注意:请相应地替换 AccessKey:SecretKey 为您的 Amazon AWS 密钥。
创建挂载点,并挂载 S3 存储桶
root@ip-xxx-xxx-xxx-xxx:~# mkdir -p /mnt/ftp root@ip-xxx-xxx-xxx-xxx:~# /usr/bin/s3fs -o allow_other -o default_acl="public-read" -o use_rrs=1 <your_bucket_to_be_mounted> /str/ftp
- 注意 1:挂载前存储桶必须存在。
- 注意 2: default_acl 设置将所有内容设置为公共可读。有关 acl 选项,请参阅 s3fs man page。
- 注意 3: use_rss 将所有上传的文件设置为 reduced_redundancy(这将为您节省一些钱!)。
- 注意 4:您可能需要 chmod 目录等(我的目录已经设置好了,我不记得我做了什么!)。
步骤 3:设置 vsftpd
现在我们需要安装和配置 vsftpd(这归功于 Chris Hough 在http://askubuntu.com/questions/239239/ubuntu-12-04-500-oops-vsftpd-refusing-to-run-with-writable-root-inside-chroot)上的回复。
更新存储库列表和 apt
root@ip-xxx-xxx-xxx-xxx:~# add-apt-repository ppa:thefrontiergroup/vsftpd root@ip-xxx-xxx-xxx-xxx:~# apt-get update
安装 vsftpd
root@ip-xxx-xxx-xxx-xxx:~# apt-get -y install vsftpd
配置 vsftpd.conf
root@ip-xxx-xxx-xxx-xxx:~# cp vsftpd.conf vsftpd.conf.ORIGINAL (just to make a backup incase you need it.) root@ip-xxx-xxx-xxx-xxx:~# vim vsftpd.conf
使用以下设置:
listen=YES anonymous_enable=NO local_enable=YES write_enable=YES dirmessage_enable=YES dirmessage_enable=YES xferlog_enable=YES connect_from_port_20=YES chroot_local_user=YES secure_chroot_dir=/var/run/vsftpd/empty pam_service_name=vsftpd sa_cert_file=/etc/ssl/private/vsftpd.pem
并将以下内容添加到文件末尾
# Passive support
pasv_enable=yes
pasv_min_port=15393 # The start port range configured in the security group
pasv_max_port=15592 # The end port range configured int he security group
pasv_address=xxx.xxx.xxx.xxx # the public IP address of the FTP serv
如果要将用户限制在其主目录(推荐),则还应添加以下内容
# Keep non-chroot listed users jailed allow_writeable_chroot=YES
重启 vsftpd
root@ip-xxx-xxx-xxx-xxx:~# service vsftpd restart
创建本地用户以访问 FTP
现在一切都已设置好,我们需要创建一些用户来访问 FTP 服务器。这些用户将在本地 EC2 实例上创建,但他们的主目录将设置为挂载的 S3 存储桶(或存储桶下的目录)。建议在挂载的 S3 存储桶下创建子目录,并使用这些子目录作为主目录,这样他们将无法访问其他用户的目录。
创建本地用户
root@ip-xxx-xxx-xxx-xxx:~# useradd -d /str/ftp/<directory> -s /sbin/nologin <ftp_user_name> root@ip-xxx-xxx-xxx-xxx:~# passwd <ftp_user_name> Enter new UNIX password: Retype new UNIX password: passwd: password updated successfully
更新 /etc/shells
root@ip-xxx-xxx-xxx-xxx:~# echo '/sbin/nologin' >> /etc/shells
恭喜!您现在可以使用第 4 步中创建的任何用户/密码通过 FTP 连接到此服务器!
更改 SSH 端口
为了提供额外的安全性,我们将更改 SSH 端口并从安全组中删除它。
更新 sshd_config 文件
root@ip-xxx-xxx-xxx-xxx:~# vim /etc/ssh/sshd_config
- 将端口更改为您在安全组中配置的非默认端口。
- 如果需要,将
PasswordAuthentication
更改为 yes。 - 重启 ssh
- 从安全组中删除端口 22 访问规则。
root@ip-xxx-xxx-xxx-xxx:~# service ssh restart
关注点
这是我第一次在 CodeProject 发表文章,请温柔些!希望这对您有所帮助!
历史
- 2012 年 2 月 15 日 - 首次发布。