一、安装ftp
1、安装ftp组件
yum -y install vsftpd
2、添加ftp用户并设置密码
命令:添加一个用户名为ftpuser
useradd ftpuser
命令:进行设置该用户的密码
passwd ftpuser
3、防火墙记得开启21端口
简单就是:修改iptables文件
vim /etc/sysconfig/iptables
在文件中有22 -j ACCEPT 下面另起一行输入跟那行差不多的,只是把22换成21,然后:wq保存。
还要运行下,重启iptables
service iptables restart
4、关闭匿名访问
修改/etc/vsftpd/vsftpd.conf文件:
vim /etc/vsftpd/vsftpd.conf
其中anonymous_enable=NO
5、设置开机启动
chkconfig vsftpd on
二、安装nginx
1、下载nginx
官方网站:http://nginx.org/en/download.html
2、安装的依赖环境准备
1、需要安装gcc的环境。
yum install gcc-c++
2、第三方的开发包。
PCRE
PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括perl 兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安装pcre库。
yum install -y pcre pcre-devel
注:pcre-devel是使用pcre开发的一个二次开发库。nginx也需要此库
zlib
zlib库提供了很多种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip,所以需要在linux上安装zlib库。
yum install -y zlib zlib-devel
openssl
OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。
nginx不仅支持http协议,还支持https(即在ssl协议上传输http),所以需要在linux安装openssl库。
yum install -y openssl openssl-devel
3、nginx安装步骤
第一步:把nginx的源码包上传到linux系统
第二步:解压缩
[root@localhost ~]# tar -zxvf nginx-1.18.0.tar.gz
第三步:使用configure命令创建一makeFile文件。
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
第四步:执行安装命令:
make & make install
第五步:启动nginx
进入 /usr/local/nginx/sbin
有个nginx可执行文件
./nginx 即开启了服务器
通过进程查看是否启动
ps -aux | grep nginx
ok!nginx到此安装成功!
附:nginx 配置说明书
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
# 这个server配置表示访问localhost:8081地址 将请求转发到这个server下定义的location位置上
server {
listen 8081;
server_name localhost;
# 这个location表示将localhost:8081地址转发到html文件夹下的index2.html首页上
location / {
root html;
index index2.html index.htm;
}
}
# 这个server表示访问localhost:80地址 将请求转发到这个server下定义的location位置上
server {
listen 80;
server_name localhost;
# 这个location表示将localhost:80请求 转发到index.html文件上
location / {
root html;
index index.html index.htm;
}
#这个location表示将localhost:80/demo/开头的请求 转发到http://localhost:8080/服务上
location /demo {
proxy_pass http://localhost:8080/;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
# 在做请求转发时,主要注意proxy_pass后/符号的添加,不加/表示访问 http://localhost:8080/demo+ 加/表示访问http://localhost:8080/+
若搭建过程中遇到如下错误nginx: [emerg] the "ssl" parameter requires ngx_http_ssl_module,建议参考下方url:
https://blog.csdn.net/duyusean/article/details/79348613 或者 https://www.cnblogs.com/ghjbk/p/6744131.html
三、通过nginx访问ftp中静态文件
想实现的效果:
图片通过ftp服务上传到/home/ftpuser/images目录下,我想通过访问Nginx服务器来访问ftp目录下的图片文件,该url为http://192.168.1.243/images/x.jpg。即使用http请求访问原本需要使用ftp请求才能访问到的资源文件。
1、修改配置文件
修改nginx/conf/nginx.conf在默认的server里再添加一个location并指定实际路径
location /images/ {
root /home/ftpuser/images;
autoindex on;
}
参数说明:
1)root则是将images映射到/home/ftpuser/images/
2)autoindex on便是打开浏览功能。
配置完成后,重启nginx,进入到/usr/local/nginx/sbin目录执行
./nginx -s reload
2、修改ftp用户权限
#chown -R ftpuser /home/ftpuser/images
至此通过nginx访问ftp服务器静态文件搭建完成!