nginx+vsftp搭建图片服务器

一、nginx

Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。以上是百度百科的介绍,我们目前只需要知道nginx是一个服务器就行了,类似于tomcat的服务器,只不过我们把它用来保存图片。

二、vsftp

VSFTP是一个基于GPL发布的类Unix系统上使用的FTP服务器软件,它有安全、高速、稳定等特点。我们暂且这样理解:vsftp就是用来传输文件的一个服务,在linux系统中开启vsftp服务,然后在windows中就可以通过linux系统的ip、vsftp服务的端口、vsftp的用户名及密码连接vsftp服务,然后就可以方便的把windows中东西上传到linux中,也可以把linux中的东西下载到windows中。

三、nginx+vsftp

上面分别介绍了nginx和vsftp,那么这两个东西怎么组合起来用呢?怎么实现这个图片服务器呢?我们知道,tomcat安装好启动后,在浏览器输入localhost:8080,就会出现tomcat的欢迎页,nginx也一样。比如linux的ip是192.168.50.122,那么启动nginx后,在浏览器访问这个地址也会出现nginx的欢迎页,其实是因为它有个默认的访问页面,完整的地址应该是192.168.50.122/index.html,那么我们就可以根据这个,把它默认的访问页面改成我们上传的图片的保存路径,比如上传了一张pic.jpg图片到linux的/home/ftpuser/images中,如果我们把默认访问页面改成/home/ftpuser,那么在浏览器中输入192.168.50.122/images/pic.jpg,就可以访问到这张图片了。下面就来介绍nginx、vsftp的安装以及配置。

四、安装nginx

1.准备活动

1
2
3
4
5
6
##下载
wget http://nginx.org/download/nginx-1.14.0.tar.gz
##解压
tar zxvf nginx-1.14.0.tar.gz
##设置vim编辑nginx配置文件语法高亮(可选,如果不想用了直接删除~/.vim/目录即可)
mkdir ~/.vim && cp -r nginx-1.14.0/contrib/vim/* ~/.vim/

2. 安装编译工具及依赖库文件

1
yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel pcre pcre-devel

3.编译安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#进入解压好的源码目录
cd nginx-1.14.0

#生成MakeFile
./configure \
--prefix=/usr/local/nginx \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_sub_module \
--with-http_gzip_static_module

#根据MakeFile编译
make

#安装
make install

4.启动

1
/usr/local/nginx/sbin/nginx

查看是否启动命令: ps aux | grep nginx

5.停止

1
2
3
/usr/local/nginx/sbin/nginx -s stop
#或者
/usr/local/nginx/sbin/nginx -s quit

6 动态加载

1
/usr/local/nginx/sbin/nginx -s reload
  • 区别:

    nginx -s quit:此方式停止步骤是待nginx进程处理任务完毕进行停止。

    nginx -s stop:此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。

    nginx -s reload:动态加载,当配置文件nginx.conf有变化时执行该命令动态加载。

7 测试配置文件nginx.conf正确性

1
/usr/local/nginx/sbin/nginx  -t

8.其他常用命令

1
2
#查看编译加载的模块
/usr/local/nginx/sbin/nginx -V

五、安装vsftp

1. 安装

1
yum -y install vsftpd

2. 关闭SELinux

  • 命令行设置selinux
1
2
setenforce 0   # 设置SELinux 成为permissive模式  (临时关闭SELinux)
setenforce 1 # 设置SELinux 成为enforcing模式 (临时开启SELinux)
  • 或者修改selinux配置文件(永久)
1
2
3
4
5
6
7
vi /etc/selinux/config
# SELINUX=enforcing # 注释掉
# SELINUXTYPE=targeted # 注释掉
SELINUX=disabled # 增加

:wq! #保存退出
setenforce 0
  • 或者设置SELinux
1
2
3
4
5
6
getsebool -a | grep ftp
setsebool -P ftpd_full_access on
systemctl stop firewalld.service
#停止firewall
systemctl disable firewalld.service
#禁止firewall开机启动

3. 防火墙设置

关闭防火墙,如果你不愿意关闭防火墙,需要防火墙添加FTP服务。

1
2
firewall-cmd --permanent --zone=public --add-service=ftp
firewall-cmd --reload

4. 设置开机启动

1
[root@localhost ~]# chkconfig vsftpd on

5. 配合vsftp

1
vim /etc/vsftpd/vsftpd.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# Example config file /etc/vsftpd/vsftpd.conf
#
# The default compiled in settings are fairly paranoid. This sample file
# loosens things up a bit, to make the ftp daemon more usable.
# Please see vsftpd.conf.5 for all compiled in defaults.
#
# READ THIS: This example file is NOT an exhaustive list of vsftpd options.
# Please read the vsftpd.conf.5 manual page to get a full idea of vsftpd's
# capabilities.
#
# Allow anonymous FTP? (Beware - allowed by default if you comment this out).
anonymous_enable=YES
#
# Uncomment this to allow local users to log in.
# When SELinux is enforcing check for SE bool ftp_home_dir
local_enable=YES
#
# Uncomment this to enable any form of FTP write command.
write_enable=YES
#
# Default umask for local users is 077. You may wish to change this to 022,
# if your users expect that (022 is used by most other ftpd's)
local_umask=022
#
# Uncomment this to allow the anonymous FTP user to upload files. This only
# has an effect if the above global write enable is activated. Also, you will
# obviously need to create a directory writable by the FTP user.
# When SELinux is enforcing check for SE bool allow_ftpd_anon_write, allow_ftpd_full_access
#anon_upload_enable=YES
#
# Uncomment this if you want the anonymous FTP user to be able to create
# new directories.
#anon_mkdir_write_enable=YES
#
# Activate directory messages - messages given to remote users when they
# go into a certain directory.
dirmessage_enable=YES
#
# Activate logging of uploads/downloads.
xferlog_enable=YES
#
# Make sure PORT transfer connections originate from port 20 (ftp-data).
connect_from_port_20=YES
#
# If you want, you can arrange for uploaded anonymous files to be owned by
# a different user. Note! Using "root" for uploaded files is not
# recommended!
#chown_uploads=YES
#chown_username=whoever
#
# You may override where the log file goes if you like. The default is shown
# below.
#xferlog_file=/var/log/xferlog
#
# If you want, you can have your log file in standard ftpd xferlog format.
# Note that the default log file location is /var/log/xferlog in this case.
xferlog_std_format=YES
#
# You may change the default value for timing out an idle session.
#idle_session_timeout=600
#
# You may change the default value for timing out a data connection.
#data_connection_timeout=120
#
# It is recommended that you define on your system a unique user which the
# ftp server can use as a totally isolated and unprivileged user.
#nopriv_user=ftpsecure
#
# Enable this and the server will recognise asynchronous ABOR requests. Not
# recommended for security (the code is non-trivial). Not enabling it,
# however, may confuse older FTP clients.
#async_abor_enable=YES
#
# By default the server will pretend to allow ASCII mode but in fact ignore
# the request. Turn on the below options to have the server actually do ASCII
# mangling on files when in ASCII mode. The vsftpd.conf(5) man page explains
# the behaviour when these options are disabled.
# Beware that on some FTP servers, ASCII support allows a denial of service
# attack (DoS) via the command "SIZE /big/file" in ASCII mode. vsftpd
# predicted this attack and has always been safe, reporting the size of the
# raw file.
# ASCII mangling is a horrible feature of the protocol.
#ascii_upload_enable=YES
#ascii_download_enable=YES
#
# You may fully customise the login banner string:
#ftpd_banner=Welcome to blah FTP service.
#
# You may specify a file of disallowed anonymous e-mail addresses. Apparently
# useful for combatting certain DoS attacks.
#deny_email_enable=YES
# (default follows)
#banned_email_file=/etc/vsftpd/banned_emails
#
# You may specify an explicit list of local users to chroot() to their home
# directory. If chroot_local_user is YES, then this list becomes a list of
# users to NOT chroot().
# (Warning! chroot'ing can be very dangerous. If using chroot, make sure that
# the user does not have write access to the top level directory within the
# chroot)
chroot_local_user=YES
allow_writeable_chroot=YES
#chroot_list_enable=YES
# (default follows)
#chroot_list_file=/etc/vsftpd/chroot_list
#
# You may activate the "-R" option to the builtin ls. This is disabled by
# default to avoid remote users being able to cause excessive I/O on large
# sites. However, some broken FTP clients such as "ncftp" and "mirror" assume
# the presence of the "-R" option, so there is a strong case for enabling it.
#ls_recurse_enable=YES
#
# When "listen" directive is enabled, vsftpd runs in standalone mode and
# listens on IPv4 sockets. This directive cannot be used in conjunction
# with the listen_ipv6 directive.
listen=NO
#
# This directive enables listening on IPv6 sockets. By default, listening
# on the IPv6 "any" address (::) will accept connections from both IPv6
# and IPv4 clients. It is not necessary to listen on *both* IPv4 and IPv6
# sockets. If you want that (perhaps because you want to listen on specific
# addresses) then you must run two copies of vsftpd with two configuration
# files.
# Make sure, that one of the listen options is commented !!
listen_ipv6=YES

pam_service_name=vsftpd
userlist_enable=YES
tcp_wrappers=YES

6. 重启vsftpd

1
systemctl restart vsftpd.service

7. 添加ftp用户

1
useradd -m -d /home/ftpuser/ ftpuser

如果不让ftpuser登陆ssh,但是可以登录ftp可以选择下面的两条命令操作

1
2
3
4
5
6
7
#禁止用户ssh登录
useradd -m -d /home/ftpuser -s /sbin/nologin ftpuser

#允许禁止ssh登陆的用户ftp登录,释掉PAM模块对vsftp登录的过度验证配置
vim /etc/pam.d/vsftpd
##注释掉下面这行
#auth required pam_shells.so

8. 给ftp用户添加密码

1
passwd ftpuser

输入两次密码后修改密码。

9. 设置访问权限

1
chmod -R 755 /home/ftpuser

10. 登录vsftp测试

下载客户端工具,比如FTPRush

安装好ftp客户端后,连接服务器过程如图:

1562134418651

此时ftpuser用户访问到的根目录其实是服务器的/home/ftpuser目录。可在这个目录里实现简单的文件操作。

六、整合nginx和vsftp

其实就是将ftpuser用户的主目录定义为nginx的web根目录,如此就能实现基于http资源的图片系统。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
server {
listen 80;
server_name localhost;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {
#root html;
root /home/ftpuser; #配置web根目录
index index.html index.htm;
}

#其他内容省略
.......
.......
.......
}

nginx加载配置文件

1
/usr/local/nginx/sbin/nginx -s reload

访问测试

通过ftp客户端工具上传好图片后,比如先通过ftp客户端创建一个images目录,然后在目录里上传图片90c8013a-2bc4-491c-b74e-d0505b215dd6_logo.jpg

可通过访问地址访问到图片。

http://101.132.45.132/images/90c8013a-2bc4-491c-b74e-d0505b215dd6_logo.jpg

1562136021596



----------- 本文结束 -----------




如果你觉得我的文章对你有帮助,你可以打赏我哦~
0%